将经过训练的模型加载到 SageMaker Estimator

时间:2021-07-15 03:24:19

标签: amazon-web-services amazon-sagemaker

我已经基于 PyTorch estimator 在 sagemaker 上训练了一个自定义模型。
训练已经完成,我验证了模型工件已经保存到 s3 位置。

我想将经过训练的模型加载到我的 sagemaker 笔记本中,以便我可以执行分析/推理等......

我做了如下,但我不确定这是否是正确的方法,因为它要求实例类型,据我所知,如果我要加载已经训练好的估算器,我需要声明哪种类型开始部署模型进行推理后使用的计算实例。

estimator = PyTorch(
        model_data = ModelArtifact_S3_LOCATION,
        entry_point ='train.py',
        source_dir = 'code',
        role = role,
        framework_version = '1.5.0',
        py_version = 'py3',)

1 个答案:

答案 0 :(得分:1)

如果训练已经完成并且您想要设置推理,那么您想要指向您的 tar.gz 模型工件文件以创建端点或直接使用您的训练估计器。以下代码块是您在训练、推理和预测时要遵循的一般流程。

# Train my estimator
pytorch_estimator = PyTorch(entry_point='train_and_deploy.py',
                            instance_type='ml.p3.2xlarge',
                            instance_count=1,
                            framework_version='1.8.0',
                            py_version='py3')
pytorch_estimator.fit('s3://my_bucket/my_training_data/')

# Deploy my estimator to a SageMaker Endpoint and get a Predictor
predictor = pytorch_estimator.deploy(instance_type='ml.m4.xlarge',
                                     initial_instance_count=1)

# `data` is a NumPy array or a Python list.
# `response` is a NumPy array.
response = predictor.predict(data)

有关在 SageMaker 上部署 PyTorch 模型的更多信息,请查看以下链接。 https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#deploy-pytorch-models

我为 AWS 工作,我的意见是我自己的