Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: ValueError: No data provided for "input_1". Need data for each key

I am using the keras functional API with input images of dimension (224, 224, 3). I have the following model using the functional API, although a similar problem seems to arise with sequential models:

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'])

I am calling model.fit_generator where my generator has

yield ({'input_1': image}, {'output': classification}) 

image is the input (224, 224, 3) image and classification is in {-1,1}.

On fitting the model, I get an error

ValueError: No data provided for "dense_1". Need data for each key in: ['dense_1']

One strange thing is that if I switch the input_1 target of the dict to dense_1, the error switches to missing an input for input_1, but goes back to missing dense_1 if both keys are in the data generator.

This happens whether I call fit_generator or get batches from the generator and call train_on_batch.

Does anyone know what's going on? From what I can tell, this should be the same as given in the documentation although with a different input size.

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']
like image 701
Daniel Underwood Avatar asked Feb 21 '17 22:02

Daniel Underwood


1 Answers

I encountered this error on 3 cases (In R):

  1. The input data does not have the same dimension as was declared in the first layer
  2. The input data includes missing values
  3. The input data is not a matrix (for example, a data frame)

Please check all of the above.

Maybe this code in R can help:

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)
like image 103
Roee Anuar Avatar answered Sep 29 '22 01:09

Roee Anuar