首页 > 解决方案 > 如何为句子级嵌入设置 LSTM 的输入形状

问题描述

我正在使用带有句子级嵌入的 LSTM。我有评论文本,我将评论的最大长度句子设置为 58,并将评论中的每个句子转换为 1024 维度的嵌入。转换评论中的所有句子后,我得到每个评论的 58*1024=59392 维数组。我的数据包括 1430 条评论。

数据的形状是 X.shape (1430,59392) Y.shape (1430,)

这是我的 LSTM 模型

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2,random_state=42)
x_train=x_train.reshape(x_train.shape[0],58,1024)
x_test=x_test.reshape(x_test.shape[0],58,1024)

model = Sequential()
model.add(LSTM(32, input_shape=(58, 1024)))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32)


y_pred= model.predict_classes(x_test)
f1= f1_score(y_test, y_pred)

但是,计算 f1_score ValueError 时出现错误:分类指标无法处理二进制和未知目标的混合

当我检查我的 y_pred.shape 它是 (286,58,1) 和我的 y_test.shape (268,1)

有人可以帮忙吗?

标签: pythonkeraslstmembeddingsentence

解决方案


推荐阅读