保存加载和运行机器学习模型?

时间:2021-01-20 19:38:07

标签: python machine-learning keras data-science

嘿,我是数据科学的新手,我用 keras 创建了一个情绪分析模型。训练模型并最终对其进行测试需要花费大量时间。有没有办法保存预训练模型并使用来自用户输入的测试运行它?

我的程序基本上创建了情感分析模型,然后创建了一个用户命令行界面:

这是我创建模型后的代码:

SENTIMENT_THRESHOLDS = (0.4, 0.7) 
"""The function below will be used to give meaning to our trained model with scores. Label will be determined by the comparison
of score and thereshold values above. 
Label is:
    'Neutral' if 0.4<score<0.7 
    'Negative' if score<=0.4 
    'Positive' if score>=0.7      
"""

def decode_sentiment(score, include_neutral=True):
    label = 'Neutral'
    if score <= SENTIMENT_THRESHOLDS[0]:
        label = 'Negative'
    elif score >= SENTIMENT_THRESHOLDS[1]:
        label = "Positive"

    return label
    
"""This function takes text as input and determine if label of the text is Positive, Negative or Neutral"""    
def predict(text):
    # Tokenize text
    x_test = pad_sequences(tokenizer.texts_to_sequences([text]), maxlen=MAX_SEQUENCE_LENGTH)
    # Predict score
    score = model.predict([x_test])[0]
    # Decode sentiment
    label = decode_sentiment(score)

    return {"label": label, "score": float(score)}

scores = model.predict(x_test, verbose=1, batch_size=10000) 

y_pred_1d = [decode_sentiment(score) for score in scores]

#User interface to use the program; It takes a text input from the user and print its label

userInput=""
while(userInput!="q"):
    userInput=input("Enter a sentence: \n Press q to quit program ")
    if(userInput=="q"):
        time.sleep(2)
        print("Exiting program...")
    else:    
        print(predict(userInput))
    

编辑

训练文件中的代码:

model.save('pretrained_model.h5')   # in file train.py

测试文件中的代码:

loaded_model = keras.models.load_model('pretrained_model.h5') # in file test.py

错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-961c0ca89c78> in <module>
      4 from tensorflow import keras
      5 
----> 6 loaded_model = keras.models.load_model('pretrained_model.h5') # in file test.py

C:\Anaconda3\lib\site-packages\tensorflow\python\keras\saving\save.py in load_model(filepath, custom_objects, compile)
    182     if (h5py is not None and (
    183         isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 184       return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    185 
    186     if sys.version_info >= (3, 4) and isinstance(filepath, pathlib.Path):

C:\Anaconda3\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
    174     if model_config is None:
    175       raise ValueError('No model found in config file.')
--> 176     model_config = json.loads(model_config.decode('utf-8'))
    177     model = model_config_lib.model_from_config(model_config,
    178                                                custom_objects=custom_objects)

AttributeError: 'str' object has no attribute 'decode'

1 个答案:

答案 0 :(得分:1)

是的,有。您可以为此使用各种方法,但最简单的方法是在 keras 中使用 save(filename) 方法,例如:-

model.save('pretrained_model.h5')   # in file train.py

并加载它,您可以使用加载它,您只需使用 load(filename) 方法。

loaded_model = keras.models.load_model('pretrained_model.h5') # in file test.py

或者:-
您还可以使用以下命令将模型保存为 json:-

#from file train.py
with open('pretrained_model.json','w') as f:
    f.write(model.to_json())

并使用以下命令加载您的模型。

# from file test.py
reconstructed_model = keras.models.model_from_json(open('pretrained_model.json','r').read())