Python - Tensorflow - LSTM- ValueError: Error when checking model target: expected dense_16 to have shape (None, 100) but got array with shape (16, 2)

Python - Tensorflow - LSTM- ValueError: Error when checking model target: expected dense_16 to have shape (None, 100) but got array with shape (16, 2)

谁能帮我理解这个错误是怎么回事?

model = Sequential()
model.add(Embedding(82, 100, weights=[embedding_matrix], input_length=1000))
model.add(LSTM(100))
model.add(Dense(100, activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs = 5, batch_size=64)

当我 运行 这个 LSTM 模型时,我得到一个错误

ValueError: Error when checking model target: expected dense_16 to have shape (None, 100) but got array with shape (16, 2)

我不确定以下信息有多大用处:

x_train.shape
Out[959]: (16, 1000)

y_train.shape
Out[962]: (16, 2)

如果您需要任何其他信息,我随时准备提供

你已经定义了密集层输入形状为 100。

model.add(Dense(100, activation = 'sigmoid'))

因此您需要确保输入的形状始终相同。 在你的情况下,使 x_train 和 y_train 形状相同。

试试 :

model = Sequential()
# here the batch dimension is None,
# which means any batch size will be accepted by the model.
model.add(Dense(32, batch_input_shape=(None, 500)))
model.add(Dense(32))

你最后一层的输出形状为 None,100

model.add(Dense(100, activation = 'sigmoid'))

但是您的数据 (y_train) 的形状为 (16,2)。应该是

model.add(Dense(2, activation = 'sigmoid'))