目次

概要

TODO:

モデルデータを作成しS3バケットに上げる

モデルは PyTorchで重回帰分析 で作成したものをそのまま利用する。

アップロードするフォルダの構成

以下の構成のフォルダを作成する。

sample_model
 └ sample_model.pth .... エクスポートした訓練済みモデル
 └ entry_point.py .... エントリポイントとなるスクリプト(後述)

モデルをエクスポートする

torch.save(model.state_dict(), 'sample_model/sample_model.pth')

entry_point.py の作成

TODO: 

tar.gz にする

tar czfv sample_model.tar.gz sample_model

S3にアップロード

バケット作成

aws s3 mb s3://sagemaker-sample-アカウントID

s3にアップロード

aws s3api put-object --bucket 作成したバケット名 --key sample_model.tar.gz --body ./sample_model.tar.gz

ノートブックインスタンスの作成

Amazon SageMakerを使ってみる を参照。

モデルのデプロイ

ノートブックインスタンスから以下を実行する。

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)

https://sagemaker.readthedocs.io/en/stable/sagemaker.pytorch.html#sagemaker.pytorch.model.PyTorchModel

後片付け

エンドポイントの削除

import sagemaker
sagemaker.Session().delete_endpoint(predictor.endpoint)

トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS