#author("2020-02-14T10:23:10+00:00","","")
#author("2020-02-16T13:10:02+00:00","","")
#mynavi(Amazon SageMakerを使ってみる)
#setlinebreak(on);

* 目次 [#e3986f6f]
#contents
- 関連
-- [[AWSメモ]]
-- [[Amazon SageMakerを使ってみる]]
-- [[PyTorchで重回帰分析]]
- 参考
-- https://docs.aws.amazon.com/ja_jp/sagemaker/latest/dg/pytorch.html
-- https://sagemaker.readthedocs.io/en/stable/using_pytorch.html#deploy-endpoints-from-model-data
-- https://aws.amazon.com/jp/blogs/news/building-training-and-deploying-fastai-models-with-amazon-sagemaker/
-- https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/pytorch/README.rst
-- https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/chainer_sentiment_analysis/src/sentiment_analysis.py

* 概要 [#h894648f]
#html(<div class="pl10">)
#TODO
#html(</div>)

* モデルデータを作成しS3バケットに上げる [#a47349e0]
#html(<div class="pl10">)

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

** アップロードするフォルダの構成 [#fdc2f419]
#html(<div class="pl10">)

以下の構成のフォルダを作成する。
#html(){{
<div style="padding: 10px; border: 1px solid #333; display: inline-block;">
sample_model<br />
 └ sample_model.pth    ....   エクスポートした訓練済みモデル<br />
 └  entry_point.py          ....   エントリポイントとなるスクリプト(後述)<br />
</div>
}}

#html(</div>)

** モデルをエクスポートする [#g78a4bf4]
#html(<div class="pl10">)
#mycode2(){{
torch.save(model.state_dict(), 'sample_model/sample_model.pth')
}}
#html(</div>)

** entry_point.py の作成 [#cf6141ad]
#html(<div class="pl10">)
#mycode2(){{
TODO: 
}}
#html(</div>)


** tar.gz にする [#sf75e15d]
#html(<div class="pl10">)
#myterm2(){{
tar czfv sample_model.tar.gz sample_model
}}
#html(</div>)

** S3にアップロード [#ka1b62de]
#html(<div class="pl10">)

バケット作成
#myterm2(){{
aws s3 mb s3://sagemaker-sample-アカウントID
}}

s3にアップロード
#myterm2(){{
aws s3api put-object --bucket 作成したバケット名 --key sample_model.tar.gz --body ./sample_model.tar.gz
}}
#html(</div>)

#html(</div>)

* ノートブックインスタンスの作成 [#p7d249a7]
#html(<div class="pl10">)
[[Amazon SageMakerを使ってみる]] を参照。
#html(</div>)

* モデルのデプロイ [#hb0614f9]
#html(<div class="pl10">)

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

#mycode2(){{
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

#html(</div>)


* 後片付け [#j5efc1d5]
#html(<div class="pl10">)

エンドポイントの削除

#mycode2(python){{
import sagemaker
sagemaker.Session().delete_endpoint(predictor.endpoint)
}}

#html(</div>)


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