对Keras有所了解,并构建了具有两个密集层的神经网络。内存中存储的数据太多,因此我正在使用fit_generator函数,但收到错误ValueError: No data provided for "dense_2". Need data for each key in: ['dense_2']
。下面的小例子:
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)
编辑:忘记了编译行
答案 0 :(得分:0)
您不需要在输入中指定层,并且显然不需要将数据传递到第二个密集层。请注意,最好使用Keras生成器,您可以创建一个自定义生成器,例如this或use a standard one。
您还需要编译模型。
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)
顺便将(1924800,)
的向量馈送到模型是否正常?