【问题标题】:How to correctly shape my CNN-LSTM input layer如何正确塑造我的 CNN-LSTM 输入层
【发布时间】:2019-09-21 21:59:10
【问题描述】:

我有一个形状为 (3340, 6) 的数据集。我想使用 CNN-LSTM 读取 30 行的序列并预测下一行的 (6) 元素。根据我的阅读,这被认为是一个多平行时间序列。我主要关注此machine learning mastery tutorial,但在为多并行时间序列实现 CNN-LSTM 架构时遇到了麻烦。

我已使用此功能将数据拆分为 30 天的时间步长帧

# split a multivariate sequence into samples
def split_sequences(sequences, n_steps):
    X, y = list(), list()
    for i in range(len(sequences)):
        # find the end of this pattern
        end_ix = i + n_steps
        # check if we are beyond the dataset
        if end_ix > len(sequences)-1:
            break
        # gather input and output parts of the pattern
        seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :]
        X.append(seq_x)
        y.append(seq_y)
    return array(X), array(y)

以下是上述函数生成的数据帧示例。

   # 30 Time Step Input Frame X[0], X.shape = (3310, 30, 6)
   [4.951e-02, 8.585e-02, 5.941e-02, 8.584e-02, 8.584e-02, 5.000e+00],
   [8.584e-02, 9.307e-02, 7.723e-02, 8.080e-02, 8.080e-02, 4.900e+01],
   [8.080e-02, 8.181e-02, 7.426e-02, 7.474e-02, 7.474e-02, 2.000e+01],
   [7.474e-02, 7.921e-02, 6.634e-02, 7.921e-02, 7.921e-02, 4.200e+01],
   ...

   # 1 Time Step Output Array y[0], y.shape = (3310, 6)
   [6.550e-02, 7.690e-02, 6.243e-02, 7.000e-02, 7.000e-02, 9.150e+02]

这是我正在使用的以下模型:

model = Sequential()
model.add(TimeDistributed(Conv1D(64, 1, activation='relu'), input_shape=(None, 30, 6)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(50, activation='relu', return_sequences=True))
model.add(Dense(6))
model.compile(optimizer='adam', loss='mse')

当我运行 model.fit 时,我收到以下错误:

ValueError: Error when checking input: expected time_distributed_59_input to have 
4 dimensions, but got array with shape (3310, 30, 6)

我不知道如何正确塑造我的输入层,以便我可以学习这个模型。我过去做过几个Conv2D 网络,但这是我的第一个时间序列模型,所以如果这里有一个明显的答案,我很抱歉。

【问题讨论】:

  • Conv1D 和 Max Pooling1D 层不需要 TimeDistributed() 包装器。这是本质上完成的。我也不明白你为什么要使用 Flatten 层。

标签: python keras


【解决方案1】:
  • Conv1DMaxPooling1D中删除TimeDistributed;支持 3D 输入
  • 删除 Flatten(),因为它会破坏 timesteps-channels 关系
  • TimeDistributed 添加到最后一个Dense 层,因为Dense 不支持3D 输入(由LSTM(return_sequences=True) 返回;或者,使用return_sequences=False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2020-04-11
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多