Keras 2D输入到2D输出

时间:2018-10-06 17:28:23

标签: python machine-learning keras time-series forecasting

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

我想建立一个用于序列预测的前馈网络。 (我意识到RNN更适合此任务,但我有自己的理由)。序列的长度为128,每个元素是具有2个条目的向量,因此每批的形状应为(batch_size, 128, 2),目标是序列中的下一步,因此目标张量的形状应为{{1 }}。

网络体系结构是这样的:

(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)

但出现相同的错误。

1 个答案:

答案 0 :(得分:3)

如果您查看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)(或(None, 2))。因此,您可能知道或不知道Dense layer is applied on the last axis of its input array,因此,如上所见,时间轴和维度一直保留到最后。

该如何解决?您提到您不想使用RNN图层,因此有两个选择:您需要在模型中的某个地方使用Flatten图层,或者还可以使用某些Conv1D + Pooling1D图层甚至是GlobalPooling图层。例如(这些只是用于演示,您可能会做不同的事情):

使用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
_________________________________________________________________

请注意,在上述两种情况下,您都需要将标签(即目标)数组的形状调整为(n_samples, 2)(或者您可能希望在最后使用Reshape层)。