Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: Model Predict, Error When Checking Input Shape

Tags:

python

keras

I have a Keras model and a numpy array that I want to call predict on. Specifically, I have:

a numpy.ndarray called test that looks like this:

array([    0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0,     0,     0,     0,     0,     0,     0,
       0,     0,     0, 12920, 11891,  4605,  2425,  6780,  5096,
   13821,  4405, 10345,  4468,  5910, 11891, 10906, 14994, 12073,
    8581,  3544, 13846,  3110,  2425,  3407,  9631, 13846,  4479,
    9964,  2556,  4479,  2686,  8895, 10959,  1531, 11891,  1494,
   10376, 13846, 12856, 13846,  3110,  2425,  3407,  3267,   181,
    4479, 14842,  4639,  7723, 11891, 11449,  2425,  5662,  2282,
    5129,  2518, 13846,  4479,  4780,  2598,  4926,   543,  7304,
   12020,  8143, 10998, 13846, 12853, 13846, 12856, 11891,  3785,
    9131,  7448, 13846, 10376, 13846,  8245,  3788, 12211,  2425,
   13614, 10049,  2556,  8245,  1406,  6423,  3110,  2425,  3407,
    5726,  2619,  1494, 13694,  7434, 12086,  7304,  3267,  9184])

When I do test.shape, I see:

(180,)

When I do model.predict(test), I get back:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...python2.7/site-packages/keras/engine/training.py", line 1152, in predict
    x, _, _ = self._standardize_user_data(x)
  File "...python2.7/site-packages/keras/engine/training.py", line 754, in _standardize_user_data
    exception_prefix='input')
  File "...python2.7/site-packages/keras/engine/training_utils.py", line 136, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected sequence to have shape (180,) but got array with shape (1,)

Any idea what I'm doing incorrectly?

Thanks!

like image 475
anon_swe Avatar asked Feb 19 '26 10:02

anon_swe


1 Answers

Keras wants batched data, so here are some tips about the shapes of data you should know:

The shapes of data should be:

  • for vector data: (samples, features)
  • for time series data: (samples, timesteps, features)
  • for image data: (samples, height, width, channels) or (samples, channels, height, width)
  • for video data: (samples, frames, height, width, channels) or (samples, frames, channels, height, width)

So your test data should have the shape of (1, 180). Just reshape your data:

test.reshape(1,-1)

Another thing that you should know is that when Keras wants a specific shape, it means the shape of one sample, not including the first dimension of your real input data: (samples,). But your data should have the samples dimension. And Keras will handle the first dimension which is the number of samples by itself.

like image 74
Tianxin Xie Avatar answered Feb 21 '26 22:02

Tianxin Xie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!