Keras - 当 returnSequence 为 True 时尺寸必须相等

Keras - Dimensions must be equal when returnSequence is True

我正在尝试构建一个模型以根据 3 个观察值的序列预测 4 个值,即:

如果下面是数据

+--------------------------------+
|feature |feature |feature |Value|
+--------------------------------+
|0.1     |0.1     |0.1     |1    |
+--------------------------------+
|0.2     |0.2     |0.2     |2    |
+--------------------------------+
|0.3     |0.3     |0.3     |3    |
+--------------------------------+
|0.4     |0.4     |0.4     |4    |
+--------+--------+--------+-----+

我想根据

预测[1,2,3,4]
+--------------------------+
|feature |feature |feature |
+--------------------------+
|0.1     |0.1     |0.1     |
+--------------------------+
|0.2     |0.2     |0.2     |
+--------------------------+
|0.3     |0.3     |0.3     |
+--------+--------+--------+

我的X,y造型如下(1228, 3, 19) (1228, 4, 1)


def get_model():
  model = Sequential()
  model.add(LSTM(32, activation='tanh', return_sequences=True, input_shape=(X.shape[1], X.shape[2]))),
  model.add(Dense(32, activation='relu')),
  model.add(Dense(4, activation='sigmoid'))

  model.compile(loss='mse', optimizer="adam", metrics=['mae', 'mse'])
  return model

My Model code:
Model: "sequential_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_17 (LSTM)               (None, 3, 32)             6656      
_________________________________________________________________
dense_34 (Dense)             (None, 3, 32)             1056      
_________________________________________________________________
dense_35 (Dense)             (None, 3, 4)              132       
=================================================================
Total params: 7,844
Trainable params: 7,844
Non-trainable params: 0
_________________________________________________________________

当我尝试拟合数据时:

history = model.fit(X_train, y_train, epochs=200, batch_size=64, validation_split=0.2, verbose=0, callbacks=[tensorboard_callback])

我收到以下错误:

ValueError: Dimensions must be equal, but are 3 and 4 for

我应该如何重塑我的数据才能让它工作,我应该填充缺失的序列吗?

如果理解正确,每个示例都有以下内容:

输入 -> (3,19) 输出 -> (4, 1)

您尝试根据 19 个值的 3 个序列回归 4 个值。如果这是正确的,那么您可以在模型中使用 return_sequences=False 并重塑输出 (y) 以具有 (4,) 的形状,而不是像 y=np.squeeze(y, -1) 那样的 (4,1)。或者如果你想保持顺序,使用 TimeDistributedGlobalAveragePooling1D 层并对输出执行相同的操作。它看起来像这样:

model = Sequential()
model.add(LSTM(32, activation='tanh', return_sequences=True, input_shape=(3, 19))),
model.add(TimeDistributed(Dense(32, activation='relu')))
model.add(GlobalAveragePooling1D())
model.add(Dense(4, activation='sigmoid'))

model.summary()

Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_9 (LSTM)                (None, 3, 32)             6656      
_________________________________________________________________
time_distributed_4 (TimeDist (None, 3, 32)             1056      
_________________________________________________________________
global_average_pooling1d_2 ( (None, 32)                0         
_________________________________________________________________
dense_16 (Dense)             (None, 4)                 132       
=================================================================
Total params: 7,844
Trainable params: 7,844
Non-trainable params: 0

编辑

您当前模型的问题在于它期望您的 targets/outputs 具有 (3,4) 的形状,而您的实际输出具有 (4,1)

的形状