情感分析:拟合模型导致值错误(形状不兼容?)

Sentiment Analysis: Fitting a model result in value error (shapes incompatible?)

我正在对一组评论进行情绪分析 --> 根据文本评论预测评分 (0-5)。我已经完成了文本预处理和分词。我正在使用预训练的词向量嵌入 (googlenews) 并创建了 embedding_matrix.

到目前为止我已经建立了模型:

#defining X (padded) and y and completing train/test split
X = pad_sequences(sequences, maxlen= 1000)
y = df['rating']
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.25, random_state = 1000)

y_train = to_categorical(y_train,6)

#building the model
sentiment_wv_model = Sequential()
embed_layer = Embedding(vocab_size, 100,weights = [embedding_matrix], input_length = 1000,trainable = True)

sentiment_wv_model.add(embed_layer)
sentiment_wv_model.add(Dense(100, activation = 'sigmoid'))
sentiment_wv_model.add(Dense(32, activation = 'sigmoid'))
sentiment_wv_model.add(Dense(1, activation='softmax'))


#compile model and fit to train data
sentiment_wv_model.compile(loss = 'categorical_crossentropy',optimizer = 'adam', metrics =['accuracy'])

sentiment_wv_model.summary
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 embedding_1 (Embedding)     (None, 1000, 100)         3631400   
                                                                 
 dense (Dense)               (None, 1000, 100)         10100     
                                                                 
 dense_1 (Dense)             (None, 1000, 32)          3232      
                                                                 
 dense_2 (Dense)             (None, 1000, 2)           66        
                                                                 
 dense_3 (Dense)             (None, 1000, 1)           3         
                                                                 
 dense_4 (Dense)             (None, 1000, 100)         200       
                                                                 
 dense_5 (Dense)             (None, 1000, 32)          3232      
                                                                 
 dense_6 (Dense)             (None, 1000, 1)           33        
                                                                 
=================================================================
Total params: 3,648,266
Trainable params: 3,648,266
Non-trainable params: 0
_________________________________________________________________


sentiment_wv_model.fit(X_train, y_train, batch_size = 32, epochs = 5, verbose =2)

运行 这个,我得到以下错误:

ValueError: in user code:

    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\engine\training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\engine\training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\engine\training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\engine\training.py", line 809, in train_step
        loss = self.compiled_loss(
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\losses.py", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\losses.py", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\losses.py", line 1664, in categorical_crossentropy
        return backend.categorical_crossentropy(
    File "C:\Users\tammy\Anaconda3\lib\site-packages\keras\backend.py", line 4994, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 6, 6) and (None, 1000, 1) are incompatible

我看到这类问题已经问过几次了,但我尝试了其他解决方案,例如将 y 设为 'to_categorical'、更改激活函数或切换为 'binary_crossentropy (最后两个对我来说没有意义,但我还是试过了)。请指教!

您当前的 y 值有一个稀疏张量:

y_train = to_categorical(y_train,6)

此灵魂具有 [1000,6] 的形状,您可以使用 y_train.shape() 进行检查。

应该起作用的一件事就是将输出层的大小更改为 6:

sentiment_wv_model.add(Dense(6, activation='softmax'))

[可选] 在此之后您还可以将损失更改为 sparse_categorical_crossentropy:

sentiment_wv_model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy,optimizer = 'adam', metrics =['accuracy'])

此外,您应该考虑在 Embedding 层之后展平您的数据,以便获得输出形状 (None, 6) 而不是 (None, 1000, 6)