python - Keras 二维输入到二维输出

标签 python machine-learning keras time-series forecasting

首先,我阅读了thisthis与我的名字相似的问题,但仍然没有答案。

我想建立一个用于序列预测的前馈网络。 (我意识到 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/

相关文章:

python - 对两个字典执行两个样本 Kolmogorov-Smirnov 检验

Python 枚举删除列表的最后一个元素

python - 传递给 `fit` 的模型只能将 `training` 和 `call` 中的第一个参数作为位置参数,发现

python - Tensorflow机器之间损失的主要差异

python - Keras:将 Seq 模型转换为功能 API

python - 无法在 OS X Yosemite 上显示 `pip install numpy`

python - 沿一列将 DataFrame 从长到宽 reshape

python - 从 keras 中的预训练模型加载权重进行微调时出现层错误

java - 如果一个或多个属性具有特定值,则删除 Weka 实例

machine-learning - 图像中的 Logo 识别