我有一个形状为(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
网络,但这是我的第一个时间序列模型,因此,如果这里缺少明显的答案,我深表歉意。
答案 0 :(得分:1)
TimeDistributed
和Conv1D
中删除MaxPooling1D
;支持3D输入Flatten()
,因为它破坏了timesteps
-channels
的关系TimeDistributed
添加到最后一个Dense
层,因为Dense
不支持3D
输入(由LSTM(return_sequences=True)
返回;或者,使用{{1} })