因此,我使用y_label = {0:"negative",1:"neutral,2:"positive"). 當我嘗試預測一個句子時,它回傳一個陣列,如何更改它以便它根據預測結果回傳字串?根據我的理解,預測值是否是陣列的最大值?
sentence = ["kurir keren"]
sequences = tokenizer.texts_to_sequences(sentence)
padded = pad_sequences(sequences,maxlen=max_length,padding=padding_type,truncating=trunc_type)
predic = np.rint(model.predict(padded))
predic2 = model.predict(padded)
print(predic)
print(predic2)
輸出
[[0. 0. 1.]]
[[0.3224687 0.45956263 0.5116373 ]]
uj5u.com熱心網友回復:
似乎您正在使用單熱編碼標簽,其中[1. 0. 0.]代表negative ,[0. 1. 0.]代表neutral和[0. 0. 1.]代表positive,因此可能只是用于np.argmax獲取最高預測的索引并使用該整數來獲取相應的字串:
import numpy as np
y_label = {0:"negative",1:"neutral",2:"positive"}
prediction = np.array([[0.3224687, 0.45956263, 0.5116373 ]])
print(y_label[np.argmax(prediction)])
positive
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/352523.html
