View a markdown version of this page

モデルを Amazon EC2 にデプロイする - Amazon SageMaker AI

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

モデルを Amazon EC2 にデプロイする

予測を取得するには、Amazon SageMaker AI を使用して Amazon EC2 にモデルをデプロイします。

SageMaker AI ホスティングサービスにモデルをデプロイする

Amazon SageMaker AI を使用して Amazon EC2 を介してモデルをホストするには、 でトレーニングしたモデルをデプロイしますトレーニングジョブを作成して実行する

ModelBuilder クラスを使用してモデルを構築およびデプロイします。 ModelBuilderはリソースチェイニングをサポートしているため、トレーニング済みの ModelTrainerを直接渡してモデルを構築およびデプロイできます。

from sagemaker.serve import ModelBuilder # Build and deploy using resource chaining from the trained model model_builder = ModelBuilder( model=xgb_model_trainer, role_arn=role, instance_type='ml.t2.medium' ) # Build creates a SageMaker Model resource model = model_builder.build() # Deploy creates a SageMaker Endpoint resource endpoint = model_builder.deploy(endpoint_name="xgboost-endpoint")
  • model – トレーニングされたModelTrainerオブジェクト。 はトレーニング出力をデプロイに自動的にModelBuilder連鎖します。

  • instance_type (str) - デプロイされたモデルを操作するインスタンスのタイプ。

build() メソッドは SageMaker AI Model リソースを作成し、SageMaker AI Endpoint リソースdeploy()を作成します。詳細については、Amazon SageMaker Python SDK の「SageMaker AI ModelBuilder」を参照してください。 Amazon SageMaker エンドポイントの名前を取得するには、次のコードを実行します。

endpoint.endpoint_name

このエンドポイントは機械学習インスタンスでアクティブなままとなるため、後でシャットダウンしない限り、いつでも瞬時に予測を行うことができます。このエンドポイント名をコピーして保存しておけば、再利用して、SageMaker Studio または SageMaker AI ノートブックインスタンスの他の場所でリアルタイム予測を行うことができます。

ヒント

Amazon EC2 インスタンスやエッジデバイスにデプロイするのためのモデルのコンパイルと最適化の詳細については、「Neo でモデルをコンパイルしてデプロイする」を参照してください。

(オプション) 既存のエンドポイントを再利用または呼び出す

モデルをエンドポイントにデプロイした後、sagemaker-core の Endpoint クラスを使用して、他のノートブックまたはアプリケーションからモデルを呼び出すことができます。次のコード例は、既存のエンドポイントを取得し、予測を行う方法を示しています。上記のデプロイステップからエンドポイント名を再使用します。

from sagemaker.core.resources import Endpoint endpoint = Endpoint.get(endpoint_name="xgboost-endpoint") # Make a prediction response = endpoint.invoke( body=test_data, content_type="text/csv" ) result = response.body.read().decode('utf-8')

(オプション) バッチ変換を使用して予測を行う

本番環境でエンドポイントをホストする代わりに、SageMaker AI バッチ変換を使用して、テストデータセットで予測を行うための 1 回限りのバッチ推論ジョブを実行できます。モデルトレーニングが完了したら、バッチトランスフォーマーを使用して、指定された S3 バケットから入力データを読み取り、予測を行うことができます。

バッチ変換ジョブを実行するには
  1. 次のコードを実行して、テストデータセットの特徴列を CSV ファイルに変換し、S3 バケットにアップロードします。

    X_test.to_csv('test.csv', index=False, header=False) boto3.Session().resource('s3').Bucket(bucket).Object( os.path.join(prefix, 'test/test.csv')).upload_file('test.csv')
  2. バッチ変換ジョブの入力および出力の S3 バケット URI を、次に示すように指定します。

    # The location of the test dataset batch_input = 's3://{}/{}/test'.format(bucket, prefix) # The location to store the results of the batch transform job batch_output = 's3://{}/{}/batch-prediction'.format(bucket, prefix)
  3. バッチ変換ジョブを作成して実行します。

    # Build a model from the trained ModelTrainer model_builder = ModelBuilder(model=xgb_model_trainer, role_arn=role) model = model_builder.build(model_name="xgboost-batch-model") # Create and run the batch transform job from sagemaker.core.resources import TransformJob transform_job = TransformJob.create( model_name=model.model_name, transform_input={ "data_source": { "s3_data_source": { "s3_data_type": "S3Prefix", "s3_uri": batch_input } }, "content_type": "text/csv", "split_type": "Line" }, transform_output={ "s3_output_path": batch_output }, transform_resources={ "instance_type": "ml.m4.xlarge", "instance_count": 1 } ) transform_job.wait()
  4. バッチ変換ジョブが完了すると、SageMaker AI は test.csv.out 予測データを作成し、batch_output パスに保存します。このデータは、s3://sagemaker-<region>-111122223333/demo-sagemaker-xgboost-adult-income-prediction/batch-prediction という形式になります。バッチ変換ジョブの出力データをダウンロードする AWS CLI には、以下を実行します。

    ! aws s3 cp {batch_output} ./ --recursive

    これにより、現在の作業ディレクトリの下に test.csv.out ファイルが作成されます。XGBoost トレーニングジョブのロジスティック回帰に基づいて予測される浮動値を確認できます。