Keras: Expected Shape error even when shape appears to match

Created on 4 Jul 2018  路  3Comments  路  Source: keras-team/keras

Hi there,

I'm getting the following error when trying to predict values based on my presaved model.
It complains that my input shape doesn't match what's expected, but I've verified that the shape matches.

Am I doing something wrong here? Any help is much appreciated.

Cheers

Using Tensorflow 1.8 on Python 2.7 and 3.6 on macOS High Sierra.
Also checked with keras 2.1.6.


Model File

https://www.dropbox.com/s/15r9s26l1owaoqn/joint1.hdf5?dl=0

Python snippet

from tensorflow.contrib.keras.api.keras.models import load_model
import numpy as np

path = '/Users/dhruv/Library/Preferences/Autodesk/maya/training/joint1.hdf5'
values = [0.4962550475880582, 0.5037171108304482, 0.4962550475880582, 0.5037171108304486, 0.0, -3.288135593220338, 0.0]
array = np.array(values)
print("Array Shape is {}".format(array.shape))
model = load_model(path)
model.predict(array)

Exception

Array Shape is (7,)
2018-07-04 13:49:05.996674: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "/Users/dhruv/Library/Preferences/PyCharm2018.1/scratches/scratch.py", line 10, in <module>
    model.predict(array)
  File "/Users/dhruv/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 1327, in predict
    x, _, _ = self._standardize_user_data(x)
  File "/Users/dhruv/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 765, in _standardize_user_data
    exception_prefix='input')
  File "/Users/dhruv/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training_utils.py", line 192, in standardize_input_data
    ' but got array with shape ' + str(data_shape))
ValueError: Error when checking input: expected input_main_input to have shape (7,) but got array with shape (1,)

Most helpful comment

@talhasaruhan Good idea.
So it turns out I needed to do this:

array = array.reshape(7,1).T

For whatever reason it needed the array to be reshaped into 7,1 and also transposed.

All 3 comments

Not sure but maybe you can try reshaping array into (7, 1). (7, 1) != (7,)

@talhasaruhan Good idea.
So it turns out I needed to do this:

array = array.reshape(7,1).T

For whatever reason it needed the array to be reshaped into 7,1 and also transposed.

I printed it out the array and it looked like that:

data = [1,2,3,4,5]
data2 = data.reshape((5,1).T 
print(data2)

it prints = [[1,2,3,4,5]]
so it is the same as [data]

so you can predict like that

model.predict([[data1],[data2]], verbose=1)

this should also match your shape
took me a while to figure that out (new to python :) )

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylemcdonald picture kylemcdonald  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments

KeironO picture KeironO  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments