【发布时间】:2019-10-22 01:28:56
【问题描述】:
我有一个具有 N 个特征(维度)的多维时间序列数据集。我正在构建一个具有 N 个输入通道(每个特征一个)的 CNN-LSTM 模型。首先,模型应该对每个特征执行一维卷积,然后合并输出并将其馈送到 LSTM 层。但是,我遇到了维度问题(我怀疑这是根本问题),即合并的输出维度不是预期的。
我在每个功能上都尝试过 Flatten(),但它返回 (?, ?),并且 Reshape() 似乎也没有成功。
# Init the multichannel CNN-LSTM proto.
def mccnn_lstm(steps=window, feats=features, dim=1, f=filters, k=kernel, p=pool):
channels, convs = [], []
# Multichannel CNN layer
for i in range(feats):
chan = Input(shape=(steps, dim))
conv = Conv1D(filters=f, kernel_size=k, activation="tanh")(chan)
maxpool = MaxPooling1D(pool_size=p, strides=1)(conv) # Has shape (?, 8, 64)
flat = Flatten()(maxpool) # Returns (?, ?), not (?, 8*64) as expected
channels.append(chan)
convs.append(flat)
merged = concatenate(convs) # Returns (?, ?), would expect a tensor like (?, 8*64, num of channels)
# LSTM layer
lstm = TimeDistributed(merged)
lstm = LSTM(64)(merged) # This line raises the error
dense = Dense(1, activation="sigmoid")(lstm)
return Model(inputs=channels, outputs=dense)
model = mccnn_lstm()
错误信息:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2
我希望多通道层的合并输出具有尺寸(?、8*64、通道数)或类似的尺寸,然后将作为 LSTM 层的输入。
【问题讨论】:
标签: python keras neural-network conv-neural-network lstm