首先,我阅读了this和 this与我的名字相似的问题,但仍然没有答案。
我想建立一个用于序列预测的前馈网络。 (我意识到 RNN 更适合这个任务,但我有我的理由)。序列的长度为 128,每个元素是一个包含 2 个条目的向量,因此每个批处理的形状应为 (batch_size, 128, 2)
并且目标是序列中的下一步,因此目标张量的形状应为 (batch_size, 1, 2)
。
网络架构是这样的:
model = Sequential()
model.add(Dense(50, batch_input_shape=(None, 128, 2), kernel_initializer="he_normal" ,activation="relu"))
model.add(Dense(20, kernel_initializer="he_normal", activation="relu"))
model.add(Dense(5, kernel_initializer="he_normal", activation="relu"))
model.add(Dense(2))
但尝试训练时出现以下错误:
ValueError: Error when checking target: expected dense_4 to have shape (128, 2) but got array with shape (1, 2)
我尝试过以下变体:
model.add(Dense(50, input_shape=(128, 2), kernel_initializer="he_normal" ,activation="relu"))
但得到同样的错误。
最佳答案
如果您查看 model.summary()
输出,您会发现问题所在:
Layer (type) Output Shape Param #
=================================================================
dense_13 (Dense) (None, 128, 50) 150
_________________________________________________________________
dense_14 (Dense) (None, 128, 20) 1020
_________________________________________________________________
dense_15 (Dense) (None, 128, 5) 105
_________________________________________________________________
dense_16 (Dense) (None, 128, 2) 12
=================================================================
Total params: 1,287
Trainable params: 1,287
Non-trainable params: 0
_________________________________________________________________
如您所见,模型的输出是 (None, 128,2)
而不是 (None, 1, 2)
(或 (无,2)
) 如您所料。所以,你可能知道也可能不知道 Dense layer is applied on the last axis of its input array结果,正如您在上面看到的,时间轴和维度一直保留到最后。
如何解决?您提到您不想使用 RNN 层,因此您有两个选择:您需要在模型中的某处使用 Flatten
层,或者您也可以使用一些 Conv1D + Pooling1D 层,甚至是全局池化层。例如(这些只是为了演示,你可以做不同的):
使用Flatten
层
model = models.Sequential()
model.add(Dense(50, batch_input_shape=(None, 128, 2), kernel_initializer="he_normal" ,activation="relu"))
model.add(Dense(20, kernel_initializer="he_normal", activation="relu"))
model.add(Dense(5, kernel_initializer="he_normal", activation="relu"))
model.add(Flatten())
model.add(Dense(2))
model.summary()
模型总结:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_17 (Dense) (None, 128, 50) 150
_________________________________________________________________
dense_18 (Dense) (None, 128, 20) 1020
_________________________________________________________________
dense_19 (Dense) (None, 128, 5) 105
_________________________________________________________________
flatten_1 (Flatten) (None, 640) 0
_________________________________________________________________
dense_20 (Dense) (None, 2) 1282
=================================================================
Total params: 2,557
Trainable params: 2,557
Non-trainable params: 0
_________________________________________________________________
使用 GlobalAveragePooling1D
层
model = models.Sequential()
model.add(Dense(50, batch_input_shape=(None, 128, 2), kernel_initializer="he_normal" ,activation="relu"))
model.add(Dense(20, kernel_initializer="he_normal", activation="relu"))
model.add(GlobalAveragePooling1D())
model.add(Dense(5, kernel_initializer="he_normal", activation="relu"))
model.add(Dense(2))
model.summary()
模型总结:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_21 (Dense) (None, 128, 50) 150
_________________________________________________________________
dense_22 (Dense) (None, 128, 20) 1020
_________________________________________________________________
global_average_pooling1d_2 ( (None, 20) 0
_________________________________________________________________
dense_23 (Dense) (None, 5) 105
_________________________________________________________________
dense_24 (Dense) (None, 2) 12
=================================================================
Total params: 1,287
Trainable params: 1,287
Non-trainable params: 0
_________________________________________________________________
请注意,在上述两种情况下,您都需要将标签(即目标)数组 reshape 为 (n_samples, 2)
(或者您可能想要使用 Reshape
层最后)。
关于python - Keras 二维输入到二维输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681601/