我正在對一組評論進行情緒分析-> 根據文本評論預測評級 (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”(最后兩個對我來說沒有意義,但是反正我試過了)。請指教!
uj5u.com熱心網友回復:
您當前的 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408854.html
標籤:
下一篇:"ValueError:Input0oflayer"sequential"isincompatiblewiththelayer"在預測中
