モデルは PyTorchで重回帰分析 で作成したものをそのまま利用する。
以下の構成のフォルダを作成する。
torch.save(model.state_dict(), 'sample_model/sample_model.pth')
TODO:
tar czfv sample_model.tar.gz sample_model
バケット作成
aws s3 mb s3://sagemaker-sample-アカウントID
s3にアップロード
aws s3api put-object --bucket 作成したバケット名 --key sample_model.tar.gz --body ./sample_model.tar.gz
ノートブックインスタンスから以下を実行する。
import numpy as np import sagemaker from sagemaker.pytorch.model import PyTorchModel from sklearn.preprocessing import StandardScaler import torch sagemaker_session = sagemaker.Session() role = get_execution_role() pytorch_model = PyTorchModel(model_data="s3://バケット名/sample_model.tar.gz", role=role, framework_version='1.3.1', entry_point="sample_model_endpoint.py") predictor = pytorch_model.deploy(instance_type='ml.c4.xlarge', endpoint_name='pytorch-sample-model', initial_instance_count=1)
エンドポイントの削除
import sagemaker sagemaker.Session().delete_endpoint(predictor.endpoint)