Keras: Model Predict, Error When Checking Input Shape

Viewed 1615

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!

2 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.

Keras is expecting a set of samples to make a prediction on — it wants of group of shape (180,) arrays. It looks like you are passing one sample, which it is interpreting as 180 samples of shape (1,) You can try wrapping this one sample in an array or use test.reshape(1, -1) creating a group of one. In other words pass it an array of (1, 180) and it will return an array of one prediction.

Related