错误:输入 0 与层 lstm_1 不兼容:预期 ndim=3,发现 ndim=2

数据挖掘 机器学习 Python 喀拉斯
2022-03-01 19:03:25

我收到错误消息:

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

使用模型:

X = np.reshape(x_train_tfidf.shape[0], 1, x_train_tfidf.shape[1])
print(X.shape)
model.add(LSTM(30, return_sequences=True,
               input_shape=X))

model.add(Flatten())
model.add(Dropout(0.25))
model.add(Dense(100,activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(1,activation='sigmoid'))
2个回答

大概是这样的..(不确定,试一试)

Iputs 需要重塑为 [样本、时间步长、特征]

所以

TrainX= np.reshape(TrainX,(TrainX.shape[0], 1, TrainX.shape[1]))

您还需要传递实际向量吗?

将 numpy 导入为 np np.expand_dims(X, axis = 0)

#这会在不更改矩阵值的情况下为您的数据添加一个额外的维度。