Keras的多频道CNN-LSTM



我有一个具有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 层的输入。

你在用 Keras 吗?在这种情况下,您没有创建任何Sequential()模型。这很可能是导致错误的原因。请让我知道。

--

编辑

我认为这个模型不需要Flatten()。展平旨在将conv2D()层的输出馈送到Dense()层中,即将二维对象展平为一维向量。但是,如果您已经在1D(使用conv1D(中工作,则不需要Flatten()

最新更新