Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 637
Denis P. Avatar asked Sep 06 '18 18:09

Denis P.


1 Answers

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)
like image 53
TayTay Avatar answered Oct 07 '22 08:10

TayTay