Python ValueError:形状在LSTM模型中不兼容
我正在基于以下参数创建一个LSTM模型Python ValueError:形状在LSTM模型中不兼容,python,tensorflow,keras,lstm,sentiment-analysis,Python,Tensorflow,Keras,Lstm,Sentiment Analysis,我正在基于以下参数创建一个LSTM模型 embed_dim = 128 lstm_out = 200 batch_size = 32 model = Sequential() model.add(Embedding(2500, embed_dim,input_length = X.shape[1])) model.add(Dropout(0.2)) model.add(LSTM(lstm_out)) model.add(Dense(2,activation='sigmoid')) model.
embed_dim = 128
lstm_out = 200
batch_size = 32
model = Sequential()
model.add(Embedding(2500, embed_dim,input_length = X.shape[1]))
model.add(Dropout(0.2))
model.add(LSTM(lstm_out))
model.add(Dense(2,activation='sigmoid'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
print(model.summary())
Xtrain, Xtest, ytrain, ytest = train_test_split(X, train['target'], test_size = 0.2, shuffle=True)
print(Xtrain.shape, ytrain.shape)
print(Xtest.shape, ytest.shape)
model.fit(Xtrain, ytrain, batch_size =batch_size, epochs = 1, verbose = 5)
但我收到以下错误
ValueError: Shapes (32, 1) and (32, 2) are incompatible
你能帮我解决这个错误吗?你的
y\u序列
来自熊猫数据帧的一列,这是一列。如果您的分类问题是二进制分类0/1问题,则此选项适用。那么在输出层只需要一个神经元
model = Sequential()
model.add(Embedding(2500, embed_dim,input_length = X.shape[1]))
model.add(Dropout(0.2))
model.add(LSTM(lstm_out))
# Only one neuron in the output layer
model.add(Dense(1,activation='sigmoid'))
您的
y\u序列
来自熊猫数据帧的一列,这是一列。如果您的分类问题是二进制分类0/1问题,则此选项适用。那么在输出层只需要一个神经元
model = Sequential()
model.add(Embedding(2500, embed_dim,input_length = X.shape[1]))
model.add(Dropout(0.2))
model.add(LSTM(lstm_out))
# Only one neuron in the output layer
model.add(Dense(1,activation='sigmoid'))
非常感谢你的帮助。我将“评级”列更改为二进制,它成功了!非常感谢你的帮助。我将“评级”列更改为二进制,它成功了!