LSTM层不接受CNN层输出的输入形状

时间:2020-05-14 22:59:02

标签: python conv-neural-network lstm sequential cnn

我正在尝试创建CNN + LSTM网络,但是LSTM层不接受输入形状。我有什么可以做的吗?

model = Sequential()
model.add(Conv2D(128, (2,2), padding = 'same', input_shape=(30, 216, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))

model.add(Conv2D(256, (2,2), padding = 'same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))

model.add(LSTM(512, input_shape = (7, 54, 256,)))
model.add(Flatten())
model.add(Dense(7, activation='softmax'))

ValueError:lstm_21层的输入0与该层不兼容:预期ndim = 3,找到的ndim = 4。收到完整的图形:[None,7,54,256]

1 个答案:

答案 0 :(得分:0)

Keras的LSTM层希望此格式作为输入:

inputs: A 3D tensor with shape [batch, timesteps, feature].

因此,您不能直接传递非循环图层。首先,Flatten()之前的图层,然后将该图层包装为TimeDistributed图层,如下所示:

model.add(TimeDistributed(Flatten()))
model.add(LSTM(8))

TimeDistributed允许将层应用于输入的每个时间切片。这是一个完整的示例:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, LSTM, \
    Dense, Flatten, Dropout, MaxPooling2D, Activation, TimeDistributed
import numpy as np

X = np.random.rand(100, 30, 216, 1)
y = np.random.randint(0, 7, 100)

model = Sequential()
model.add(Conv2D(16, (2,2), padding = 'same', input_shape=(30, 216, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (2,2), padding = 'same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(8))
model.add(Dense(7, activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
history = model.fit(X, y)