python - 如何在 Azure ML 工作区的一个 pickle 文件中转储和利用多个 ML 算法对象?
问题描述
我正在尝试使用 Jupyter 笔记本在 Azure ML 工作区中创建 ML 模型。我没有使用 Azure 提供的 AutoML 功能或设计器,并且希望运行本地准备的完整代码。
我的 ML 模型中使用了 3 种不同的算法。我很困惑如何将所有对象保存在一个泡菜文件中,以便稍后在“推理配置”和“Score.py”文件中使用?此外,一旦保存,我如何在“Score.py”文件(这是包含驱动程序代码的主文件)中访问它们?
目前我正在使用以下方法:
import pickle
f= 'prediction.pkl'
all_models=[Error_Message_countvector, ErrorMessage_tfidf_fit, model_naive]
with open(f, 'wb') as files:
pickle.dump(all_models, files)
并访问对象:
cv_output = loaded_model[0].transform(input_series)
tfidf_output = loaded_model[1].transform(cv_output)
loaded_model_prediction = loaded_model[2].predict(tfidf_output)
不知何故,当我在与整个代码相同的单元格中运行时,此方法可以正常工作。但是当我部署完整的模型时它会抛出错误。
我的“Score.py”文件看起来像这样:
import json
from azureml.core.model import Model
import joblib
import pandas as pd
def init():
global prediction_model
prediction_model_path = Model.get_model_path("prediction")
prediction_model = joblib.load(prediction_model_path)
def run(data):
try:
data = json.loads(data)
input_string= str(data['errorMsg']).strip()
input_series=pd.Series(input_string)
cv_output= prediction_model[0].transform(input_series)
tfidf_output = prediction_model[1].transform(cv_output)
result = prediction_model[2].predict(tfidf_output)
return {'response' : result }
except Exception as e:
error = str(e)
return {'response' : error }
部署时收到的错误是:
Error:
{
"code": "AciDeploymentFailed",
"statusCode": 400,
"message": "Aci Deployment failed with exception: Error in entry script, AttributeError: module '__main__' has no attribute 'text_cleaning', please run print(service.get_logs()) to get details.",
"details": [
{
"code": "CrashLoopBackOff",
"message": "Error in entry script, AttributeError: module '__main__' has no attribute 'text_cleaning', please run print(service.get_logs()) to get details."
}
]
}
任何人都可以帮助我理解问题或找出代码中是否缺少/错误?
将多个算法对象保存在一个泡菜文件中的正确方法是什么?
解决方案
> 谁能帮我理解问题或找出代码中是否有遗漏/错误?
从您的错误消息中:
“入口脚本中的错误,AttributeError:模块' main '没有属性'text_cleaning' ......
您的第一个似乎是试图调用一个尚未由您的评分脚本导入cv_output
的函数。prediction_model
text_cleaning
> 将多个算法对象保存在一个 pickle 文件中的正确方法是什么?
如果您想保留一系列转换,例如您的示例中的转换,最佳实践是使用Pipeline
来自的类sklearn
:
https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html
推荐阅读
- python - 在 django.db.models.Q 搜索字符串中同时包含字符串和整数时出现 ValueError
- sql-server - 如何对 SSIS 进行编程以在目录中顺序读取一堆文件?
- javascript - 如何在第一次运行时跳过运行 useEffect 是严格模式?
- google-chrome - 谷歌表单响应
- jenkins - 如何使用 Jenkins 管道更新另一台服务器上的网站?
- java - 在 Java/jena API 中打开文件
- openlayers - OpenLayers 6 添加制造商标签或文本
- php - 使用 register_post_type 时显示类别和标签
- html - 如何从 R 中的 URL 链接中提取每个主题的表格 - Webscraping
- prolog - 如何在Prolog中“编写一个返回与文字相反的谓词”?