[英]How to predict using multiple saved model?
我正在嘗試從此筆記本下載的已保存模型中預測分數值
https://www.kaggle.com/paoloripamonti/twitter-sentiment-analysis/
它包含4個保存的模型,即:
我正在使用model.h5我的代碼是:
from keras.models import load_model
s_model = load_model('model.h5')
#predict the result
result = model.predict("HI my name is Mansi")
但這無法預測。
我認為該錯誤是因為我必須先對其進行標記化和編碼,但是我不知道如何使用多個保存的模型來執行此操作。
誰能指導我如何使用上面筆記本中提到的保存的模型預測值和分數。
在輸入模型之前,應先對文本進行預處理,然后是最小的工作腳本(改編自https://www.kaggle.com/paoloripamonti/twitter-sentiment-analysis/ ):
import time
import pickle
from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
model = load_model('model.h5')
tokenizer = pickle.load(open('tokenizer.pkl', "rb"))
SEQUENCE_LENGTH = 300
decode_map = {0: "NEGATIVE", 2: "NEUTRAL", 4: "POSITIVE"}
POSITIVE = "POSITIVE"
NEGATIVE = "NEGATIVE"
NEUTRAL = "NEUTRAL"
SENTIMENT_THRESHOLDS = (0.4, 0.7)
def decode_sentiment(score, include_neutral=True):
if include_neutral:
label = NEUTRAL
if score <= SENTIMENT_THRESHOLDS[0]:
label = NEGATIVE
elif score >= SENTIMENT_THRESHOLDS[1]:
label = POSITIVE
return label
else:
return NEGATIVE if score < 0.5 else POSITIVE
def predict(text, include_neutral=True):
start_at = time.time()
# Tokenize text
x_test = pad_sequences(tokenizer.texts_to_sequences([text]), maxlen=SEQUENCE_LENGTH)
# Predict
score = model.predict([x_test])[0]
# Decode sentiment
label = decode_sentiment(score, include_neutral=include_neutral)
return {"label": label, "score": float(score),
"elapsed_time": time.time()-start_at}
predict("hello")
測試:
predict("hello")
其輸出:
{'elapsed_time': 0.6313169002532959,
'label': 'POSITIVE',
'score': 0.9836862683296204}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.