簡體   English   中英

Keras的多通道CNN-LSTM

[英]Multichannel CNN-LSTM in Keras

我有一個具有N個特征(維度)的多維時間序列數據集。 我正在構建一個CNN-LSTM模型,它有N個輸入通道(每個功能一個)。 首先,模型應對每個特征執行1D卷積,然后合並輸出並將其饋送到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() Flatten旨在將conv2D()層的輸出饋送到Dense()層,即將二維對象展平為一維向量。 但如果您已經在1D(使用conv1D )工作,則不需要Flatten()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM