Keras:ValueError:没有为"input_1"提供数据.需要每个密钥的数据

dan*_*ood 9 machine-learning neural-network keras

我正在使用keras功能API和维度(224,224,3)的输入图像.我使用功能API有以下模型,尽管顺序模型似乎也出现了类似的问题:

input = Input(shape=(224, 224, 3,))
shared_layers = Dense(16)(input)
model = KerasModel(input=input, output=shared_layers)
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy'])
Run Code Online (Sandbox Code Playgroud)

我打电话给model.fit_generator我的发电机

yield ({'input_1': image}, {'output': classification}) 
Run Code Online (Sandbox Code Playgroud)

image是输入(224,224,3)图像并且classification在{-1,1}中.

在拟合模型时,我得到一个错误

ValueError: No data provided for "dense_1". Need data for each key in: ['dense_1']
Run Code Online (Sandbox Code Playgroud)

一个奇怪的事情是,如果我input_1将dict 的目标切换到dense_1,则错误切换为缺少输入input_1,但dense_1如果两个键都在数据生成器中,则返回丢失.

无论我是fit_generator从发电机呼叫还是从批次中获取电话,都会发生这种情况train_on_batch.

有谁知道发生了什么?据我所知,这应该与文档中给出的相同,尽管输入大小不同.

Full traceback:
Traceback (most recent call last):
  File "pymask.py", line 303, in <module>
    main(sys.argv)
  File "pymask.py", line 285, in main
    keras.callbacks.ProgbarLogger()
  File "/home/danielunderwood/virtualenvs/keras/lib/python3.6/site-packages/keras/engine/training.py", line 1557, in fit_generator
    class_weight=class_weight)
  File "/home/danielunderwood/virtualenvs/keras/lib/python3.6/site-packages/keras/engine/training.py", line 1314, in train_on_batch
    check_batch_axis=True)
  File "/home/danielunderwood/virtualenvs/keras/lib/python3.6/site-packages/keras/engine/training.py", line 1029, in _standardize_user_data
    exception_prefix='model input')
  File "/home/danielunderwood/virtualenvs/keras/lib/python3.6/site-packages/keras/engine/training.py", line 52, in standardize_input_data
    str(names))
ValueError: No data provided for "input_1". Need data for each key in: ['input_1']
Run Code Online (Sandbox Code Playgroud)

Roe*_*uar 9

我在3个案例中遇到了这个错误(在R中):

  1. 输入数据与第一层中声明的维度不同
  2. 输入数据包括缺失值
  3. 输入数据不是矩阵(例如,数据帧)

请检查以上所有内容.

也许R中的这段代码可以帮助:

library(keras)

#The network should identify the rule that a row sum greater than 1.5 should yield an output of 1

my_x=matrix(data=runif(30000), nrow=10000, ncol=3)
my_y=ifelse(rowSums(my_x)>1.5,1,0)
my_y=to_categorical(my_y, 2)

model = keras_model_sequential()
layer_dense(model,units = 2000, activation = "relu", input_shape = c(3))
layer_dropout(model,rate = 0.4)
layer_dense(model,units = 50, activation = "relu")
layer_dropout(model,rate = 0.3)
layer_dense(model,units = 2, activation = "softmax")

compile(model,loss = "categorical_crossentropy",optimizer = optimizer_rmsprop(),metrics = c("accuracy"))

history <- fit(model,  my_x, my_y, epochs = 5, batch_size = 128, validation_split = 0.2)

evaluate(model,my_x, my_y,verbose = 0)

predict_classes(model,my_x)
Run Code Online (Sandbox Code Playgroud)

  • 对我来说这是#3 * facepalm * (2认同)

Den*_*nis 5

我也遇到过这个问题,上面提到的答案都没有奏效。根据 keras文档,您可以将参数作为字典传递,如下所示:

model.fit({'main_input': headline_data, 'aux_input': additional_data},
      {'main_output': labels, 'aux_output': labels},
      epochs=50, batch_size=32)
Run Code Online (Sandbox Code Playgroud)

或作为这样的列表:

model.fit([headline_data, additional_data], [labels, labels],
      epochs=50, batch_size=32)
Run Code Online (Sandbox Code Playgroud)

使用 keras 2.0.9 版时,字典版本对我不起作用。我现在使用列表版本作为解决方法。


dan*_*ood 4

这是由于我误解了 keras 输出的工作原理。output参数指定的层Model需要数据的输出。我误解了output数据字典中的键会自动转到参数指定的层output

  • @AntonioSesto 我不想这么说,但我也不完全确定我的答案意味着什么。我回顾了 git 和 slack 日志,但没有找到太多。我认为问题是我使用“output”作为我认为应该存在的键,但键必须是输出的层,而输出层将具有键“shared_layers”。无论如何,由于本例中我的特定模型存在问题,我最终也切换到带有张量层的张量流而不是 keras。 (4认同)