Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2021-09-25 SageMaker from Zero@機械学習の社会実装勉強会

Naka Masato
September 25, 2021
150

2021-09-25 SageMaker from Zero@機械学習の社会実装勉強会

Naka Masato

September 25, 2021
Tweet

More Decks by Naka Masato

Transcript

  1. Machine Learning Platform and Tools 選択肢はさまざま • Vertex AI •

    Auto ML • SagaMaker • Kubeflow • Argo Events/Workflow 課題 : どれがに誰に向いてるのか判断は難しい → 基本的な部分を一通り理解 → 今回 AWS SageMaker を紹介
  2. 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
  3. 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
  4. 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 の一部
  5. Notebook Instance 作成 1. Instance の名前をつける 2. Instance Type の選択

    3. IAM Role を作成 or 既存 Role 選択 4. Instance 作成
  6. 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から
  7. 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')
  8. SageMaker endpointへのアクセス SageMaker Endpoint ↔ Lambda Function ↔ API Gateway

    ↔ Client Dependencies • IAM role for Lambda Function ◦ SageMaker Endpoint を Invoke する Policy が必要
  9. Lambda → SageMaker Lambda Function 1. Endpoint を環境変数などで渡す 2. 今回の例では、

    input data は、 “data” をキーにして、 csv の値をバリューに渡 す実装 3. runtime.invoke_endpoint で runtime.sagemaker のエンドポイントを 呼ぶ 4. 最後に response の Body を返す
  10. まとめ 初心者でも 1. Jupyter Notebook のデプロイが数分で簡単にできた 2. データの準備、モデルの作成、学習、デプロイが Notebook 上でできる

    3. Lambda と API Gateway を連携すれば、デプロイしたモデルの API サーバーを作成 可能 かなり荒削りではあるものの、最初に体験する流れとしては十分な収穫では?
  11. 参考資料 • 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