4
0

More than 1 year has passed since last update.

オンプレミス環境でAmazon SageMakerを動かすための環境構築

Posted at

オンプレミス環境でAmazon SageMakerを動かすための環境構築をまとめました。

環境

  • macOS Big Sur 11.1
  • Python 3.7.0

事前準備

  • Docker
    Amazon SageMakerが提供するコンテナーを起動する際に使⽤します。macOS で Dockerを使うには、Docker アプリ(Docker Desktop)をインストールするか、Homebrewでインストールします。

  • Anaconda
    機械学習に必要なPythonライブラリは充実しているため、Anacondaの仮想環境にAmazon SageMakerをインストールします。Anacondaのインストール⽅法は公式マニュアルに開設されています。

Amazon SageMaker⽤仮想環境の作成

condaで仮想環境(localsm)を作成します。

$ conda create -n localsm 

作成した仮想環境(localsm)に切り替えます。

$ conda activate localsm 

仮想環境に、機械学習に必要なPythonパッケージ(pandas, tensorflow, keras, pytorchなど)を追加します。

$ conda install pip pandas tensotflow keras pytorch

仮想環境に、sagemakerをインストールします。

$ conda pip install sagemaker

Jyupter notebookの環境を構築します。

$ conda install ipykernel 
$ python -m ipykernel install --user --name localsm --display-name "Python (localsm)"

Jupyter Notebookを起動します。

$ Jupyter notebook& 

Webブラウザが起動し、Jupyter Notebookのページが問題なく表示されたら、環境構築は完了です。

ローカルモードでSageMakerの使用

まず、Amazon SageMakerを実⾏する為のIAMロールを確認します。AWS CLIが使⽤できる場合は、以下のコマンドで確認できます。

$ aws iam list-roles | grep SageMaker-Execution 

以下のような結果が表示されます。

"RoleName": "AmazonSageMaker-ExecutionRole-xxxxxxxxxxxxxx",
"Arn": "arn:aws:iam::602166551063xxxxxxxxxxxxxxxxxxxxxxxxxxxx",

このArnの結果は、コンテナを呼び出すときのrole関数で使います。例えば、Dockerが起動している状態で、SageMakerが提供するScikit learnのコンテナイメージSKLearnを使⽤し、機械学習を実⾏する場合は、以下のように記述します。


from sagemaker.sklearn.estimator import SKLearn

role = 'arn:aws:iam::602166551063xxxxxxxxxxxxxxxxxxxxxxxxxxxx'


# instance_typeは"local"で指定します。
# script_pathは機械学習を実行するスクリプトですです。
sklearn = SKLearn(
    entry_point=script_path,
    framework_version=FRAMEWORK_VERSION,
    instance_type="local",
    role=role, 
)


無事にSageMakerが起動できれば、ローカルモードの環境設定は完了です。

参考

Train with Amazon SageMaker on your local machine(https://youtu.be/K3ngZKF31mc)

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0