ValueError: Input 0 is incompatible with layer lstm_15: expected ndim=3, found ndim=2

ValueError: Input 0 is incompatible with layer lstm_15: expected ndim=3, found ndim=2

我想将 CNN 层的输出馈送到 LSTM 层,但出现错误 ValueError: Input 0 is incompatible with layer lstm_15: expected ndim=3, found ndim=2,代码如下:

inp = Input(shape = (max_length,))
xe = Embedding(vocabulary_size, 300, weights = [embedding_matrix], trainable = False)(inp)
x = Conv1D(512,kernel_size = 2, activation='relu',kernel_initializer = "he_uniform")(xe)
x = GlobalMaxPooling1D()(x)
x = LSTM(128)(x)
x = Dense(11, activation = "sigmoid")(x)

输入形状:

embedding_matrix: (26441, 300)
inp : TensorShape([Dimension(None), Dimension(3146)])
X_train :(1432, 3146)
Y_train: (1432, 11)
vocabulary_size: 26441
max_length: 3146

有人可以帮助我

这是因为当您应用 GlobalMaxPooling1D 时,它 returns 具有形状 (batch_size, 512) 的张量。如 docs, this layer downsamples the input representation by taking the maximum value over the time dimension. You have two options either not to use GlobalMaxPool1D (you can instead use local pooling layers such as MaxPool1D) or you can use RepeatVector 中所述,将 GlobalMaxPool1D 的输出形状从 (batch_size, 512) 更改为 (batch_size, n, 512),其中 nRepeatVector 定义的参数,您希望序列重复多少次。