Dr_Bunsen
Dr_Bunsen

Reputation: 125

Keras shape issue only when predicting

So I am at a loss here. I trained my model, everything works; But when I try to use the prediction method I get the following error:

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

Which I find very strange, since the input I am giving is (64,), I even returned the shape in cli like this

    print(type(test_x[0]))
    print(test_x[0].shape)

Which returns

<class 'numpy.ndarray'>
(64,)

Which, in my mind, should work when I use

print(str(np.argmax(model.predict(test_x[0]))))

Can anyone please point out what I am doing wrong?

Full error output:

File "/home/drbunsen/Downloads/code/neural/random/neuralPlaying.py", line 115, in
main()
File "/home/drbunsen/Downloads/code/neural/random/neuralPlaying.py", line 110, in main
print(np.argmax(model.predict(train_x[0])))
File "/home/drbunsen/.local/lib/python3.7/site-packages/keras/engine/training.py", line 1441, in predict
x, _, _ = self._standardize_user_data(x)
File "/home/drbunsen/.local/lib/python3.7/site-packages/keras/engine/training.py", line 579, in _standardize_user_data
exception_prefix='input')
File "/home/drbunsen/.local/lib/python3.7/site-packages/keras/engine/training_utils.py", line 145, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected dense_1_input to have shape (64,) but got array with shape (1,)

Upvotes: 0

Views: 244

Answers (1)

Kaveh
Kaveh

Reputation: 4990

The model expects input shape as: (number_of_samples,number_of_features).

If you want to pass 1 sample and each sample has 64 features, then shape of input should be like: (1,64).
Since you have trained your model with 64 features, the input always should have (N,64) shape.

If you pass an array with shape (64,), model considers it as 64 samples with 1 feature, which is incompatible with 64 expected features.

To resolve your issue pass input like:

print(str(np.argmax(model.predict(test_x[0].reshape(1,-1))))) # add first dimension

Upvotes: 1

Related Questions