如何使用预先训练的模型定义SageMaker估计器对象,然后进行部署?

时间:2020-08-10 12:35:10

标签: amazon-sagemaker

我有一个预先训练的模型,其工件保存在S3存储桶中。我试图弄清楚如何定义一个查看文档的估计量:https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html

但是,当我尝试部署估计器时,下面的代码引发了错误“ ValueError:估计器与培训工作无关”。我不熟悉SageMaker,因此找不到解决方法。这是我的代码:

my_model_uri = Path_to_model_artifacts   # 's3://..../model.tar.gz'

my_estimator = sagemaker.estimator.Estimator(
container, 
role,   
train_instance_count=1,                                    
train_instance_type='ml.m4.xlarge',                                    
sagemaker_session=session,
model_uri=my_model_uri)

my_predictor = my_estimator.deploy(initial_instance_count = 1, instance_type = 'ml.m4.xlarge')

以下是错误输出:

ValueError                                Traceback (most recent call last)
<ipython-input-24-151bc6602c5a> in <module>
     43 
     44 #model_uri = model_uri(SM_MODEL_DIR)
---> 45 my_predictor = my_estimator.deploy(initial_instance_count = 1, instance_type = 'ml.m4.xlarge')
     46 
     47 #path_to_model_artifacts = os.environ[SM_MODEL_DIR]

~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/estimator.py in deploy(self, initial_instance_count, instance_type, accelerator_type, endpoint_name, use_compiled_model, update_endpoint, wait, model_name, kms_key, data_capture_config, tags, **kwargs)
    693                 endpoint and obtain inferences.
    694         """
--> 695         self._ensure_latest_training_job()
    696         endpoint_name = endpoint_name or self.latest_training_job.name
    697         model_name = model_name or self.latest_training_job.name

~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/estimator.py in _ensure_latest_training_job(self, error_message)
    982         """
    983         if self.latest_training_job is None:
--> 984             raise ValueError(error_message)
    985 
    986 

ValueError: Estimator is not associated with a training job

如果您可以在代码中指定缺少的部分,我将不胜感激。

2 个答案:

答案 0 :(得分:5)

我找到了一种创建与现有培训工作关联的Estimator对象的简单方法。那就是使用类“ sagemaker.estimator.Estimator”的attach()方法:https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html

这是我编写的将先前的训练作业附加到Estimator对象并进行部署的代码。我认为它之所以有用,是因为我在AWS SageMaker中训练了模型。

my_estimator = sagemaker.estimator.Estimator.attach(TrainingJobName)
my_predictor = my_estimator.deploy(initial_instance_count = 1, instance_type = 'ml.m4.xlarge')

答案 1 :(得分:1)

如果要在AWS SageMaker之外进行训练有素的模型,通常将要使用官方的SageMaker推理Docker映像之一来部署模型。

model.tar.gz中工件数据的格式是什么?您正在使用什么框架?

https://sagemaker.readthedocs.io/en/stable/overview.html#using-models-trained-outside-of-amazon-sagemaker是一个很好的文档起点。