ValueError:检查目标时发生错误:预期density_2具有形状(1,),但形状为(500,)的数组[情感分析]

时间:2018-10-05 23:40:06

标签: python-3.x numpy tensorflow keras sentiment-analysis

在询问此问题之前已完成研究: Error when checking target: expected dense_2 to have shape (None, 256) but got array with shape (16210, 4096)

ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (10000, 1)

Error when checking target: expected dense_3 to have shape (2,) but got array with shape (1,)

几天来我一直在寻找解决此问题的方法。请帮我解决这个问题。

vocab_size = 5000
dim = 32 
input_length_var = 500
model = Sequential() 
model.add(Embedding(vocab_size, dim, input_length=input_length_var)) 
model.add(LSTM(100)) 
model.add(Dense(1, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy']) 
print(model.summary()) 

上面的代码是我的模型。我现在将打印上述模型的摘要:

Layer (type)                 Output Shape              Param #
=================================================================
embedding_1 (Embedding)      (None, 500, 1)            500
_________________________________________________________________
lstm_1 (LSTM)                (None, 100)               40800
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 101
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 2
=================================================================
Total params: 41,403
Trainable params: 41,403
Non-trainable params: 0

最后,我将向您展示np.shape()的结果:

(1117228, 500)
(1117228, 500)

我尝试了从Reshape()到将input_shape添加到密集层的所有操作,但结果始终相同。我在做什么错以及如何解决这个问题?我的任务是情绪分析。

编辑:有人告诉我,输出的尺寸需要为(1117228,1),我需要在train_test_split中为标签设置情绪得分。我的csv的前一半是负面情绪,另一半是正面情绪。我将如何使用呢?

0 个答案:

没有答案