the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s)

Viewed 1137

Getting that error in Keras. Scenario: Input:

  1. Images with train shape as (50000, 32, 32, 3)
  2. Aux input with shape (50000, 1)
  3. Ground truth: (50000, 1)

This is model fit code

x_train_input = Input(shape=(32,32,3))

aux_rand_input = Input(shape=(1,))

out = model_inst.build_model(x_train_input, aux_rand_input)

model = Model(inputs=[x_train_input, aux_rand_input], outputs=[out])

model.fit(x=[x_train, aux_input], y=y_train, batch_size=batch_size, steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=(x_test, y_test), callbacks=[reduce_lr, tensorboard], verbose=2)

Got this error when running this.

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays:

And this is how build_model's last few layers look like.

    flatten = Flatten()(drop_5)
    # aux_input = Input(shape=(1,))
    concat = Concatenate(axis=1)([flatten, aux_input])

    fc1 = Dense(512, kernel_regularizer=regularizers.l2(weight_decay))(concat)
    fc1 = Activation('relu')(fc1)
    fc1 = BatchNormalization()(fc1)

    fc1_drop = Dropout(0.5)(fc1)
    fc2 = Dense(self.num_classes)(fc1_drop)
    out = Activation('softmax')(fc2)
    return out
1 Answers

For validation data you are passing only one array for inputs.

model.fit(x=[x_train, aux_input], y=y_train, batch_size=batch_size, steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=(x_test, y_test), callbacks=[reduce_lr, tensorboard], verbose=2)

You should pass values for both aux_rand_input and x_train_input. If you have aux_test variable to hold test data for aux_rand_input, then this can be done as follows

model.fit(x=[x_train, aux_input], y=y_train, batch_size=batch_size, steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=([x_test, aux_test], y_test), callbacks=[reduce_lr, tensorboard], verbose=2)

Edit:

To use the model.fit_generator method the generator must yield tuple or list of two elements with the first element consisting two arrays. E.g.

def generator(x, aux, y):
   ## part of the code...
   yield [batch_x, batch_aux], batch_y
Related