Slide 1

Slide 1 text

SageMaker From Zero Naka Masato 2021/09/25

Slide 2

Slide 2 text

Machine Learning Platform and Tools 選択肢はさまざま ● Vertex AI ● Auto ML ● SagaMaker ● Kubeflow ● Argo Events/Workflow 課題 : どれがに誰に向いてるのか判断は難しい → 基本的な部分を一通り理解 → 今回 AWS SageMaker を紹介

Slide 3

Slide 3 text

What is AWS SageMaker? Managed ML platform. ● launched at the AWS re:Invent in 2017 ● Many features available ○ Supported managed TensorFlow and MXNet deep neural network ○ Supported recurrent neural network training, word2vec training, … ○ AWS Batch Transform ○ SageMaker Ground Truth ○ SageMaker Neo... ● Useful and attractive features for beginners ○ Jupyter Notebook ○ Deploy trained model with Jupyter Notebook

Slide 4

Slide 4 text

Contents Overview 1. Notebook Instance 作成 2. Notebook Instance 上で、データ取得、モ デルの学習 & 保存 3. Notebook Instance 上からモデルのデプロ イ (model endpoint) 4. Lambda と API Gateway を Model Endpoint へ連携 How to build, train, and deploy a machine learning model with Amazon SageMaker | AWS Call an Amazon SageMaker model endpoint using Amazon API Gateway and AWS Lambda | Amazon Web Services

Slide 5

Slide 5 text

Dependencies for the first tutorial How to build, train, and deploy a machine learning model with Amazon SageMaker | AWS Dependencies: ● IAM role with AmazonSageMakerFullAccess ● S3 bucket: e.g. naka-sagemaker-test これらは Tutorial の中で作成可能 AmazonSageMakerFullAccess の一部

Slide 6

Slide 6 text

Notebook Instance 作成 1. Instance の名前をつける 2. Instance Type の選択 3. IAM Role を作成 or 既存 Role 選択 4. Instance 作成

Slide 7

Slide 7 text

Jupyter Notebook上でデータ準備 1. Notebook を開いて conda_python3 を選択 2. 必要なライブラリを import a. import sagemaker … (SageMaker Python SDK https://sagemaker.readthedocs.io/en/stable/) 3. ( 必要に応じて ) S3 バケット作成 4. データを pandas の DataFrame に読み込み 5. 訓練データとテストデータに分ける a. boto3.Session().resource('s3').Bucket(bucket_name).Object(os.path.join(prefix, 'train/train.csv')).upload_file('train.csv') 訓練データは s3上にアップロード 6. 訓練データを準備 a. s3_input_train = sagemaker.inputs.TrainingInput(s3_data='s3://{}/{}/train'.format(bucket_name, prefix), content_type='csv') ← dataはS3から

Slide 8

Slide 8 text

Jupyter Notebook上で学習〜デプロイ 7. モデル準備 a. sess = sagemaker.Session() b. xgb = sagemaker.estimator.Estimator(xgboost_container,role, instance_count=1, instance_type='ml.m4.xlarge',output_path='s3://{}/{}/output'.format(bucket_name, prefix),sagemaker_session=sess) c. xgb.set_hyperparameters(xxxx) 8. モデル学習 a. xgb.fit({'train': s3_input_train}) 9. デプロイ a. xgb_predictor = xgb.deploy(initial_instance_count=1,instance_type='ml.m4.xlarge') 10. テストデータに対して推論 a. predictions = xgb_predictor.predict(test_data_array).decode('utf-8')

Slide 9

Slide 9 text

デプロイしたEndpointにアクセス このチュートリアルでは、このあと Clean Up ですが、せっかくなので、 Call an Amazon SageMaker model endpoint using Amazon API Gateway and AWS Lambda | Amazon Web Services こちらの一部も!

Slide 10

Slide 10 text

SageMaker endpointへのアクセス SageMaker Endpoint ↔ Lambda Function ↔ API Gateway ↔ Client Dependencies ● IAM role for Lambda Function ○ SageMaker Endpoint を Invoke する Policy が必要

Slide 11

Slide 11 text

Lambda → SageMaker Lambda Function 1. Endpoint を環境変数などで渡す 2. 今回の例では、 input data は、 “data” をキーにして、 csv の値をバリューに渡 す実装 3. runtime.invoke_endpoint で runtime.sagemaker のエンドポイントを 呼ぶ 4. 最後に response の Body を返す

Slide 12

Slide 12 text

Invoke lambda function 1. テストデータのラベルのフィールドを取 り除いて、 Event をセット 2. Lambda を Invoke 3. Prediction の値が返ってくる

Slide 13

Slide 13 text

API Gateway とLambdaのIntegration こちらは、 SageMaker 自体とは関係ないので今回割愛

Slide 14

Slide 14 text

まとめ 初心者でも 1. Jupyter Notebook のデプロイが数分で簡単にできた 2. データの準備、モデルの作成、学習、デプロイが Notebook 上でできる 3. Lambda と API Gateway を連携すれば、デプロイしたモデルの API サーバーを作成 可能 かなり荒削りではあるものの、最初に体験する流れとしては十分な収穫では?

Slide 15

Slide 15 text

Thank you!!

Slide 16

Slide 16 text

参考資料 ● Bank Marketing Data Set ● How to build, train, and deploy a machine learning model with Amazon SageMaker | AWS ● aws/amazon-sagemaker-examples: Example 📓 Jupyter notebooks that demonstrate how to build, train, and deploy machine learning models using 🧠 Amazon SageMaker. ● 機械学習基盤を作るのに Kubernetes か SageMaker で迷っている人へ ● Deploy a Model in Amazon SageMaker - Amazon SageMaker ● Call an Amazon SageMaker model endpoint using Amazon API Gateway and AWS Lambda | Amazon Web Services ● Get Started with Amazon SageMaker Notebook Instances - Amazon SageMaker