Keras Array Input Error

好久不见. 提交于 2020-02-04 05:28:28

问题


I get the following error:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 6 arrays but instead got the following list of 3 arrays: [array([[ 0,  0,  0, ..., 18, 12,  1],
       [ 0,  0,  0, ..., 18, 11,  1],
       [ 0,  0,  0, ..., 18,  9,  1],
       ...,
       [ 0,  0,  0, ..., 18, 15,  1],
       [ 0,  0,  0, ..., 18,  9,  ...

in my keras model.

I think the model is mistaking something?

This happens when I feed input to my model. The same input works perfectly well in another program.


回答1:


It's impossible to diagnose your exact problem without more information.

I usually specify the input_shape parameter of the first layer based on my training data X.

e.g.

model = Sequential()
model.add(Dense(32, input_shape=X.shape[0]))

I think you'll want X to look something like this:

   [
    [[ 0,  0,  0, ..., 18, 11,  1]],
    [[ 0,  0,  0, ..., 18,  9,  1]],
   ....
   ]

So you could try reshaping it with the following line:

X = np.array([[sample] for sample in X])



回答2:


The problem really comes from giving the wrong input to the network.

In my case the problem was that my custom image generator was passing the entire dataset as input rather than a certain pair of image-label. This is because I thought that generator.flow(x,y, batch_size) of Keras already has a yield structure inside, however the correct generator structure should be as follows(with a separate yield):

def generator(batch_size):

(images, labels) = utils.get_data(1000) # gets 1000 samples from dataset
labels = to_categorical(labels, 2)

generator = ImageDataGenerator(featurewise_center=True,
                 featurewise_std_normalization=True,
                 rotation_range=90.,
                 width_shift_range=0.1,
                 height_shift_range=0.1,
                 zoom_range=0.2)

generator.fit(images)

gen = generator.flow(images, labels, batch_size=32)

while 1:
    x_batch, y_batch = gen.next()
    yield ([x_batch, y_batch])

I realize the question is old but it might save some time for someone to find the issue.



来源:https://stackoverflow.com/questions/41248677/keras-array-input-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!