我正在使用CNN-LSTM网络进行图像分类。我的图像大小是(2242243),批量大小是90。当我将输入传递到LSTM层时,我得到了这个错误。以下是我的代码片段:
input1 = Input(shape=(224, 224,3))
x = Conv2D(8, (3,3), activation ="relu")(input1)
x = MaxPooling2D(2,2)(x)
x = Conv2D(16, (3,3), activation ="relu")(x)
x = MaxPooling2D(2,2)(x)
x = Conv2D(32, (3,3), activation ="relu")(x)
x = MaxPooling2D(2,2)(x)
x = Conv2D(64, (3,3), activation ="relu")(x)
x = Dropout(0.2)(x)
x = MaxPooling2D(2,2)(x)
x = LSTM(units= 64, activation= 'tanh', input_shape= [None, 144], return_sequences = True)(x)
error:
---> 10 x = LSTM(units= 64, activation= 'tanh', input_shape= [None, 144], return_sequences = True)(x)
ValueError: Input 0 of layer lstm_14 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 12, 12, 64]
如果有人能解决我的问题,谢谢。
根据文档页面,LSTM层输入应具有以下形状:[batch, timesteps, feature]
。您的LSTM层接收形状[None, 12, 12, 64]
的输入,这就是为什么您获得关于3D/4D形状的错误。你需要重塑你的张量:[None, 12, 12, 64] -> [None, 144, 64]
。为此,可以在上一个MaxPooling2D
和LSTM
层之间插入Reshape
层。