简体   繁体   中英

fit_generator expecting input for all layers of neural network?

Newish to Keras and constructing a neural net with two dense layers. There's too much data to hold in memory, so I'm using the fit_generator function, but get the error ValueError: No data provided for "dense_2". Need data for each key in: ['dense_2'] ValueError: No data provided for "dense_2". Need data for each key in: ['dense_2'] . Small example below:

from keras.models import Sequential
from keras.layers import Dense
import numpy as np

model = Sequential([
    Dense(100, input_shape=(1924800,), activation='relu'),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

def generate_arrays_from_files(path, batch_size=50):
    while True:
        # Do things....
        yield ({'dense_1_input': np.asarray(outdata)}, {'output': np.asarray(outlabels)})

model.fit_generator(generate_arrays_from_files(path), steps_per_epoch=5, epochs=10)

Edit: forgot the compile line

You don't need to specify the layer in the input, and you obviously won't need to pass data to the second dense layer. Note that it's better to use a Keras generator, you can create a custom one like this or use a standard one .

You also need to compile your model.

from keras.models import Sequential
from keras.layers import Dense
import numpy as np

model = Sequential([
    Dense(100, input_shape=(1924800,), activation='relu'),
    Dense(1, activation='sigmoid')
])

optimizer = keras.optimizers.Adam(lr=1e-3)
model.compile(loss='binary_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])

def generate_arrays_from_files(path, batch_size=50):
    while True:
        # Do things....
        yield np.asarray(outdata), np.asarray(outlabels)

model.fit_generator(generate_arrays_from_files(path), steps_per_epoch=5, epochs=10)

Is it normal to feed a vector of (1924800,) to the model by the way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM