简体   繁体   English

fit_generator 期望神经网络所有层的输入?

[英]fit_generator expecting input for all layers of neural network?

Newish to Keras and constructing a neural net with two dense layers. Keras 的新手并构建了一个具有两个密集层的神经网络。 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']内存中要保存的数据太多,所以我使用 fit_generator 函数,但得到错误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'] . 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 .请注意,最好使用 Keras 生成器,您可以像这样创建自定义生成器或使用标准生成器

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? (1924800,)给模型喂一个(1924800,)的向量正常吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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