数值错误: 输入0与卷积层conv1d_1不兼容:期望的ndim为3,而发现的ndim为4。

21

我正在使用Keras提供的conv1d层为序列数据构建预测模型。这是我的做法

model= Sequential()
model.add(Conv1D(60,32, strides=1, activation='relu',padding='causal',input_shape=(None,64,1)))
model.add(Conv1D(80,10, strides=1, activation='relu',padding='causal'))
model.add(Dropout(0.25))
model.add(Conv1D(100,5, strides=1, activation='relu',padding='causal'))
model.add(MaxPooling1D(1))
model.add(Dropout(0.25))
model.add(Dense(300,activation='relu'))
model.add(Dense(1,activation='relu'))
print(model.summary())

然而,调试信息仍然存在。

Traceback (most recent call last):
File "processing_2a_1.py", line 96, in <module>
model.add(Conv1D(60,32, strides=1, activation='relu',padding='causal',input_shape=(None,64,1)))
File "build/bdist.linux-x86_64/egg/keras/models.py", line 442, in add
File "build/bdist.linux-x86_64/egg/keras/engine/topology.py", line 558, in __call__
File "build/bdist.linux-x86_64/egg/keras/engine/topology.py", line 457, in assert_input_compatibility
ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4

训练数据和验证数据的形状如下:

('X_train shape ', (1496000, 64, 1))
('Y_train shape ', (1496000, 1))
('X_val shape ', (374000, 64, 1))
('Y_val shape ', (374000, 1))

我认为第一层中的input_shape没有正确设置。如何设置?


更新:使用input_shape=(64,1)后,尽管模型摘要运行通过,但我收到了以下错误消息:

________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv1d_1 (Conv1D)            (None, 64, 60)            1980
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 64, 80)            48080
_________________________________________________________________
dropout_1 (Dropout)          (None, 64, 80)            0
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 64, 100)           40100
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 64, 100)           0
_________________________________________________________________
dropout_2 (Dropout)          (None, 64, 100)           0
_________________________________________________________________
dense_1 (Dense)              (None, 64, 300)           30300
_________________________________________________________________
dense_2 (Dense)              (None, 64, 1)             301
=================================================================
Total params: 120,761
Trainable params: 120,761
Non-trainable params: 0
_________________________________________________________________
None
Traceback (most recent call last):
  File "processing_2a_1.py", line 125, in <module>
    history=model.fit(X_train, Y_train, batch_size=batch_size, validation_data=(X_val,Y_val), epochs=nr_of_epochs,verbose=2)
  File "build/bdist.linux-x86_64/egg/keras/models.py", line 871, in fit
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 1524, in fit
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 1382, in _standardize_user_data
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 132, in _standardize_input_data
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (1496000, 1)
3个回答

13

您应该将 input_shape 改为

input_shape=(64,1)

...或使用batch_input_shape:

batch_input_shape=(None, 64, 1)

这个讨论详细解释了keras中两者之间的区别。


嗨Maxim,我按照建议将其更改为(64,1),但出现了错误消息,请参见我上面编辑的帖子。batch_input_shape也有相同的错误消息。谢谢。 - user288609
是的。你想要比较什么?最好进行更多的最大池化操作,将张量从“64”降采样到最终的“1”。请注意,“MaxPooling1D(1)”不会产生任何效果。 - Maxim
1
我在最后一个dropout层和第一个dense层之间添加了model.add(flatten()),它可以正常工作。 - user288609

5

我曾经遇到过同样的问题。我通过使用tf.expand_dims来扩展输入数据的维度来解决了这个问题。

x = expand_dims(x, axis=-1)

0

在我的情况下,我想在单个20*32特征图上使用Conv2D,并执行以下操作:

print(kws_x_train.shape)                     # (8000,20,32)
model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(16, (3, 8), input_shape=(20,32)),
])
...
model.fit(kws_x_train, kws_y_train, epochs=15)

这将会得到expected ndim=4, found ndim=3. Full shape received: [None, 20, 32]。但是你需要告诉Conv2D只有一个特征图,并向输入向量添加一个额外的维度。这样做可以解决问题:

kws_x_train2 = kws_x_train.reshape(kws_x_train.shape + (1,))
print(kws_x_train2.shape)                     # (8000,20,32,1)
model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(16, (3, 8), input_shape=(20,32,1)),
])
...
model.fit(kws_x_train2, kws_y_train, epochs=15)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接