1

有人可以帮我理解这个错误是怎么回事吗?

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)

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

4

2 回答 2

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))
于 2017-08-11T10:26:03.877 回答
1

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

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

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

model.add(Dense(2, activation = 'sigmoid'))
于 2017-08-13T06:10:10.753 回答