【问题标题】:How to define a SageMaker estimator object using a pre-trained model and then deploy it?如何使用预训练模型定义 SageMaker 估计器对象,然后部署它?
【发布时间】:2020-08-16 00:05:20
【问题描述】:

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

但是,当我尝试部署估算器时,下面的代码引发了错误“ValueError: Estimator is not associated with a training job”。我不熟悉 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

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

【问题讨论】:

    标签: amazon-sagemaker


    【解决方案1】:

    我找到了一种直接的方法来创建与现有训练作业关联的 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')
    

    【讨论】:

      【解决方案2】:

      如果您要使用在 AWS SageMaker 之外训练的模型,您通常会想要使用官方 SageMaker 推理 Docker 映像之一来部署您的模型。

      model.tar.gz 中的工件数据的格式是什么?你用的是什么框架?

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

      【讨论】:

      • 我使用其估算器对象在 AWS SageMaker 中训练了模型,然后关闭了笔记本。现在,我想在新的笔记本会话中使用它来进行预测。基于预训练模型创建一个新的估计器对象将使部署模型和进行预测变得容易。
      猜你喜欢
      • 1970-01-01
      • 2022-07-30
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2020-02-08
      • 1970-01-01
      相关资源
      最近更新 更多