首页 > 解决方案 > 如何在 SageMaker 中的 model.tar.gz 上创建模型?

问题描述

我想在我的模型工件 (s3:/bucket/output/model.tar.gz) 上创建一个模型,用于海滩变换和部署?我的模型是一个简单的随机森林,我使用 Python SDK 和训练脚本对其进行了训练。在我的火车脚本中,我只有 model_fn 函数和 main 函数。

现在我想使用以下方法为批量转换作业创建模型:

from sagemaker.image_uris import retrieve
image = retrieve(region= sagemaker.Session().boto_session.region_name, framework='sklearn', version='0.23-1' )
from sagemaker.model import Model
estimator =  model.deploy(initial_instance_count = 1 , instance_type = 'ml.p2.xlarge')

我收到了这个错误

Error hosting endpoint sagemaker-scikit-learn-2021-05-14-19-43-21-320: Failed. Reason:  The primary container for production variant AllTraffic did not pass the ping health check. Please check CloudWatch logs for this endpoint...

另外,我尝试进行转换工作,但我的工作一直在运行,并出现此错误

transformer = model.transformer(instance_count=1, instance_type="ml.m5.xlarge")
transformer.transform('address to s3 input')

错误 :

Traceback (most recent call last):
File "/miniconda3/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 55, in handle
self.handle_request(listener_name, req, client, addr)
File "/miniconda3/lib/python3.7/site-packages/gunicorn/workers/ggevent.py", line 143, in handle_request
super().handle_request(listener_name, req, sock, addr)
File "/miniconda3/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 106, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/miniconda3/lib/python3.7/site-packages/sagemaker_sklearn_container/serving.py", line 128, in main
serving_env.module_dir)
File "/miniconda3/lib/python3.7/site-packages/sagemaker_sklearn_container/serving.py", line 105, in import_module
user_module = importlib.import_module(module_name)
File "/miniconda3/lib/python3.7/importlib/__init__.py", line 118, in import_module
if name.startswith('.'):

AttributeError: 'NoneType' object has no attribute 'startswith'

标签: pythonamazon-web-servicesscikit-learnsdkamazon-sagemaker

解决方案


如果您已经将 S3 中的模型工件存储为 model.tar.gz。

你应该 -

  1. 创建一个 SKLearnModel
  2. 将您的 inference.py 文件(应该有 model_fn、predict_fn、input_fn)打包到名为source.tar.gz
  3. 将环境变量指定SAGEMAKER_SUBMIT_DIRECTORY为 SKLearnModel 对象中的路径source.tar.gz
  4. 将模型部署到端点
  5. 创建批量转换作业。

文档中的示例


推荐阅读