I am running the code mentioned at link of the code

Here is the code:

import numpy as np
from keras.models import Model
from keras.layers import Input, Dense, Dropout
#from keras.utils import to_categorical  # not working in google colab
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from keras.callbacks import EarlyStopping
import tensorflow as tf

# Create an input array of 50,000 samples of 20 random numbers each
x = np.random.randint(0, 100, size=(50000, 20))


# And a one-hot encoded target denoting the index of the maximum of the inputs
y = to_categorical(np.argmax(x, axis=1), num_classes=20)

# Split into training and testing datasets
x_train, x_test, y_train, y_test = train_test_split(x, y)

# Build a network, probaly needlessly complicated since it needs a lot of dropout to
# perform even reasonably well.

i = Input(shape=(20, ))
a = Dense(1024, activation='relu')(i)
b = Dense(512, activation='relu')(a)
ba = Dropout(0.3)(b)
c = Dense(256, activation='relu')(ba)
d = Dense(128, activation='relu')(c)
o = Dense(20, activation='softmax')(d)

model = Model(inputs=i, outputs=o)

es = EarlyStopping(monitor='val_loss', patience=3)

model.compile(optimizer='adam', loss='categorical_crossentropy')
#model.fit= tf.convert_to_tesnsor(model.fit)
model.fit(x_train, y_train, epochs=8, batch_size=8, validation_data=[x_test, y_test], callbacks=[es])

print(np.where(np.argmax(model.predict(x_test), axis=1) == np.argmax(y_test, axis=1), 1, 0).mean())


ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1298 test_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1282 run_step  *
        outputs = model.test_step(data)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1241 test_step  *
        y_pred = self(x, training=False)
    /usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:989 __call__  *
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py:197 assert_input_compatibility  *
        raise ValueError('Layer ' + layer_name + ' expects ' +

    ValueError: Layer model_11 expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 20) dtype=float32>]

Kindly help in solving this issue, I am not able to run it on Goolge Colab. Here is the Google Colab link: google colab link


1 Answer 1


From the Keras documentation:

Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. Thus, note the fact that the validation loss of data provided using validation_split or validation_data is not affected by regularization layers like noise and dropout. validation_data will override validation_split. validation_data could be: - A tuple (x_val, y_val) of Numpy arrays or tensors. - A tuple (x_val, y_val, val_sample_weights) of NumPy arrays. - A tf.data.Dataset. - A Python generator or keras.utils.Sequence

You have input validation_data=[x_test, y_test]

The list implies that there are two inputs in the model (Multi-input) and we are providing data for both the input. Hence, the error.

Change it to validation_data=(x_test, y_test)

  • $\begingroup$ Thanks for your help. Worked well $\endgroup$ Commented Jul 20, 2021 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.