【问题标题】:Keras - Dimensions must be equal when returnSequence is TrueKeras - 当 returnSequence 为 True 时,尺寸必须相等
【发布时间】:2020-10-01 09:15:21
【问题描述】:

我正在尝试建立一个模型来根据 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

我应该如何重塑我的数据以使其正常工作,我应该填充缺失的序列吗?

【问题讨论】:

  • 你的X怎么可能是3维的?如果您有 1228 个样本,每个样本有 3 个特征(如您展示的示例所示),则其形状应为 (1228, 3); 19的第三维是什么?同样,y 的形状应该只是 (1228,)(features 不应该是 value 的一部分)。
  • 1228 组,每组 3 天,每组 19 个功能。在我的问题中,我简化了数据集

标签: python machine-learning keras lstm


【解决方案1】:

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

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

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

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

编辑

您当前模型的问题在于,它希望您的目标/输出的形状为 (3,4),而您的实际输出的形状为 (4,1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2021-11-08
    • 1970-01-01
    • 2021-08-29
    • 2019-10-11
    • 2022-07-23
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多