Denis P.
Denis P.

Reputation: 312

Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

How to fix the input array to meet the input shape?

I tried to transpose the input array, as described here, but an error is the same.

ValueError: Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

import tensorflow as tf
import numpy as np

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(40, input_shape=(21,), activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(1, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

arrTest1 = np.array([0.1,0.1,0.1,0.1,0.1,0.5,0.1,0.0,0.1,0.6,0.1,0.1,0.0,0.0,0.0,0.1,0.0,0.0,0.1,0.0,0.0])
scores = model.predict(arrTest1)
print(scores)

Upvotes: 7

Views: 10203

Answers (1)

TayTay
TayTay

Reputation: 7170

Your test array, arrTest1, is a 1d vector of 21:

>>> arrTest1.ndim
1

What you are trying to feed your model is a row of 21 features. You simply need one more set of brackets:

arrTest1 = np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.1, 0., 0.1, 0.6, 0.1, 0.1, 0., 0., 0., 0.1, 0., 0., 0.1, 0., 0.]])

And now you have a row with 21 values:

>>> arrTest1.shape
(1, 21)

Upvotes: 8

Related Questions