Keras Input_shape 形状错误

Keras Input_shape shape error

当我运行以下代码时:

from keras import models
from keras import layers
from keras import optimizers
model = models.Sequential()
model.add(layers.Dense(256, activation='relu', input_shape = (4, 4, 512)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizers.RMSprop(lr=2e-5),
                loss='binary_crossentropy',
                metrics=['acc'])
model.summary()
history = model.fit(train_features, train_labels,
                    epochs=30,
                    batch_size=20,
                    validation_data=(validation_features, validation_labels))

我收到这个错误:

ValueError: Error when checking input: expected dense_40_input to have 2 dimensions, but got array with shape (2000, 4, 4, 512)

这是训练和验证数据的形状:

print(train_features.shape, train_labels.shape, validation_features.shape, validation_labels.shape)

输出:

(2000, 4, 4, 512) (2000,) (1000, 4, 4, 512) (1000,)

这里发生了什么?我的火车和验证形状应该与我刚刚指定的相同。即使我更改为 input_dim = 4*4*512,我仍然会收到错误消息。

model.summary():

的输出
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_42 (Dense)             (None, 4, 4, 256)         131328    
_________________________________________________________________
dropout_19 (Dropout)         (None, 4, 4, 256)         0         
_________________________________________________________________
dense_43 (Dense)             (None, 4, 4, 1)           257       
=================================================================
Total params: 131,585
Trainable params: 131,585
Non-trainable params: 0
_________________________________________________________________

我的 Keras 版本是 2.1.6.

正如您在模型摘要中看到的那样,最后一层的输出形状是 (None, 4, 4, 1),并且由于每个样本都有一个标签,因此最后一层的输出形状应该是 (None, 1) .因此,您必须在将训练数据馈送到网络之前对其进行整形,或者将第一个 Dense 层的输出展平(或者可能添加 Reshape 层作为第一层)。

方法 1) 重塑训练和验证数据:

train_features = train_features.reshape((2000, -1))
validation_features = validation_features.reshape((1000, -1))

model = models.Sequential()
model.add(layers.Dense(256, activation='relu', input_dim=train_features.shape[-1]))
# ... the rest is the same

方法 2) 添加 Flatten 层:

model = models.Sequential()
model.add(layers.Dense(256, activation='relu', input_shape = (4, 4, 512)))
model.add(layers.Flatten())
# ... the rest is the same

我推荐第一种方法(除非你有充分的理由选择第二种方法),因为根据 Dense layer documentation input of this layer with rank greater than 2 (i.e. 3D, 4D, etc.) is flattened before applying dot product. And considering that you apply another flatten operation in the second approach, this may be less efficient than feeding it with a 2D tensor directly (though, I have not confirmed this myself, it is just a wild guess!). It seems that the documentation is wrong 并且 Dense 层的输入没有被展平,而是应用于最后一个轴。


附带说明:您得到的错误有点奇怪。当 运行 你的代码在我的机器上时,我没有得到。相反,我收到一个错误,抱怨最后一层的输出形状与标签形状(我在上面提到的)不兼容。

在使用 Dense() 之前,您应该先调用 Flatten()。 错误很明显,Dense net 期望 2 dim,而你为它安装了 4 dim。

model = models.Sequential()
model.add(Flattern(input_shape = (4, 4, 512)))
model.add(layers.Dense(256, activation='relu'))