Keras 数组输入错误

Keras Array Input 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,  ...

在我的 keras 模型中。

我觉得模型误会了什么?

当我向我的模型提供输入时会发生这种情况。同样的输入在另一个程序中工作得很好。

如果没有更多信息,就无法准确诊断出您的问题。

我通常根据我的训练数据X指定第一层的input_shape参数。

例如

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

我想你会希望 X 看起来像这样:

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

因此您可以尝试使用以下行对其进行整形:

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

问题确实来自于给网络提供了错误的输入。

在我的例子中,问题是我的自定义图像生成器将整个数据集作为输入而不是某对图像标签传递。这是因为我认为Keras的 generator.flow(x,y, batch_size) 内部已经有一个yield结构,但是正确的生成器结构应该如下(有一个单独的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])

我知道这个问题很老,但它可能会节省一些时间让别人找到问题。