ValueError: Input 0 of layer lstm_28 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 50, 21, 8]

ValueError: Input 0 of layer lstm_28 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 50, 21, 8]

输入维度:

train_ohe :(95036, 50, 21)
y_train :(95036,)
val_ohe :(9911, 50, 21)
y_val :(9911,)

型号 :

x_input = Input(shape=(50,))
emb = Embedding(21, 8, input_length=50)(x_input)
bi_rnn = LSTM(50, input_shape=(50,20,50), return_sequences=False)(emb)
x = Dropout(0.3)(bi_rnn)
x_output = Dense(1, activation='relu')(x)
model1 = Model(inputs=x_input, outputs=x_output)
model1.compile(optimizer='adam', loss='mse', metrics=['mse'])

history1 = model1.fit(
    train_ohe, y_train,
    epochs=1, batch_size=256,validation_data=(val_ohe, y_val),callbacks=[es]
    )

备注

Vocab size=20, input_length=50 

我尝试将 return_sequences 更改为 True/False,还尝试从 (50, 20)/(50,20,50) 更改 LSTM 图层的维度,但仍然出现相同的错误。

看起来您正在将单热向量提供给嵌入层。您应该提供整数序列。形状应该是 (95036, 50).

如果您真的需要提供 one-hot 向量 - 您必须将 one-hot 转换为整数以将它们提供给嵌入层。尝试这样的事情:

x_input = Input(shape=(50, 21))
x = tf.argmax(x_input, axis=-1)
emb = Embedding(21, 8, input_length=50)(x)