我接受了某種培訓,最終的 Jupyter 筆記本是這樣的:
https://colab.research.google.com/drive/1Lmh1b5Ge9NodxIrukCTJC3cpYQDn9VuM?usp=sharing
我了解整個代碼,以及模型是如何訓練的。
然而,最后我在測驗資料集中預測推文的情緒,如下所示:
i = random.randint(0, len(test_labels)-1)
print('Sentence:', test_tweets[i])
print('Emotion:', index_to_class[test_labels[i]])
p = model.predict(np.expand_dims(test_seq[i], axis=0))[0]
pred_class = index_to_class[np.argmax(p).astype('uint8')]
print('Predicted Emotion:', pred_class)
這作業得很好。
但是我想用隨機句子測驗模型預測,例如:
sentence = 'I love you more than ever'
print('Sentence:', sentence)
#print('Emotion:', index_to_class[test_labels[i]])
p = model.predict(np.expand_dims(sentence, axis=0))[0]
pred_class = index_to_class[np.argmax(p).astype('uint8')]
print('Predicted Emotion:', pred_class)
但我得到了這個錯誤:
Sentence: I love you more than ever
WARNING:tensorflow:Model was constructed with shape (None, 50) for input KerasTensor(type_spec=TensorSpec(shape=(None, 50), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None,).
我在這里想念什么?
uj5u.com熱心網友回復:
您的模型需要一個整數序列,而不是原始字串。嘗試先將句子轉換為其對應的整數序列:
sentence = 'I love you more than ever'
print('Sentence:', sentence)
#print('Emotion:', index_to_class[test_labels[i]])
sentence = get_sequences(tokenizer, np.expand_dims(sentence, axis=0))
p = model.predict(sentence)[0]
pred_class = index_to_class[np.argmax(p).astype('uint8')]
print('Predicted Emotion:', pred_class)
Sentence: I love you more than ever
Predicted Emotion: joy
uj5u.com熱心網友回復:
只是補充一點:
形狀
np.expand_dims(sentence).shape是(1,),不是(None, 50)。- 它應該擴大一維的
batch大小。
序列
Input你的模型是一個填充的數字序列,由分詞器轉換。- 它的長度應該是 50。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459065.html
