【问题标题】:Unable to use Machine Learning model saved as pickle file in flask app无法在烧瓶应用程序中使用保存为泡菜文件的机器学习模型
【发布时间】:2020-09-12 16:29:08
【问题描述】:

我正在尝试使用烧瓶构建 API,它将使用我保存的 ML 模型。 该模型是使用 sklearn、管道和辅助函数 (lemmatizer_preprocessing) 构建的,并使用 joblib 以 pickle 格式存储 现在,当我尝试使用该模型来构建我的烧瓶应用程序时,它给出了属性错误

AttributeError: module '__main__' has no attribute 'lemmatizer_preprocessing'

用于构建模型并保存的代码

def lemmatizer_preprocessing(mess):
    nopunc = [char for char in mess if char not in string.punctuation]
    nopunc = ''.join(nopunc)
    nopunc = [lemmatizer.lemmatize(word) for word in nopunc.split()]
    nopunc = [word for word in nopunc if word.lower() not in stopwords.words('english')]
    temp =  ' '.join(nopunc).strip()
    return re.sub(r'[^\w]', ' ', temp)
....
....
....
pipeline1 = Pipeline([
    ('bow', CountVectorizer(analyzer=lemmatizer_preprocessing)),
    ('classifier', MultinomialNB()),
    ...
])
....
....
....
joblib.dump(pipeline1, 'filename.pkl')

现在每当我尝试导入此模型时,它都会显示上述错误。我知道它显示错误,因为 joblib 需要函数 lemmatizer_preprocessing 才能正确反序列化模型,但由于某种原因,该函数没有被注册。 我正在使用两个文件来划分我的烧瓶应用程序 app.py 和 predictor.py 的代码 app.py的代码:

from flask import Flask, jsonify, request, make_response
from predictor import predict_jihad
app = Flask(__name__, instance_relative_config=True)
predict_jihad = predict_jihad()

@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(500)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 500)

@app.route('/')
def index():
    text = request.args.get('text')
    if type(text) is str and len(text)!=0:
        return jsonify({"probability":predict_jihad.get_prediction(text)})
    else:
        return jsonify({"error":"check passed value"})

app.run(debug=False)

predictor.py 的代码:

from nltk.corpus import stopwords
import string
from sklearn.feature_extraction.text import TfidfTransformer ,CountVectorizer
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
import re
from nltk.stem import WordNetLemmatizer
import joblib

class predict_jihad:
    def __init__(self):
        super().__init__()
        lemmatizer = WordNetLemmatizer()
        file = './filename.pkl'
    def deserialize(self):
        def lemmatizer_preprocessing(mess):
            lemmatizer = WordNetLemmatizer()
            nopunc = [char for char in mess if char not in string.punctuation]
            nopunc = ''.join(nopunc)
            nopunc = [self.lemmatizer.lemmatize(word) for word in nopunc.split()]
            nopunc = [word for word in nopunc if word.lower() not in stopwords.words('english')]
            temp =  ' '.join(nopunc).strip()
            return re.sub(r'[^\w]', ' ', temp)
        model = joblib.load(open('filename.pkl','rb'))
        return model

    def get_prediction(self,text):
        model = self.deserialize()
        return model.predict_proba([text])[0][1]

所有其他文件都在原地,并且没有注册其他错误。 请提供解决方案。

【问题讨论】:

    标签: python machine-learning flask scikit-learn


    【解决方案1】:

    Leaky Pipes 问题是我们在一个项目中定义了 lemmatizer_preprocessing 类,它不知道如何 在尝试加载时解释腌制管道的那些部分 模型。

    你可以这样做:

    if __name__ == "__main__":
        normalizer = lemmatizer_preprocessing()
        lemmatizer_preprocessing.__module__ = "model_maker"
        normalizer.save("normalizer.pkl")
    

    module-main-has-no-attribute

    python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing

    【讨论】:

    • 我必须在 app.py 或 predictor.py 中添加这些行。此外,我尝试在 app.py 中添加这些行,它显示错误,因为 lemmatizer_preprocessing 需要一个位置参数,它是文本类型。
    • 你应该在predictor.py中添加它,你没有使用import pickle吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2015-11-18
    相关资源
    最近更新 更多