

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

# SageMaker AI 推定器を使用したトレーニングジョブの実行
<a name="docker-containers-adapt-your-own-private-registry-estimator"></a>

SageMaker Python SDK の[推定器](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html)を使用して、SageMaker トレーニングジョブの設定と実行を処理することもできます。以下のコード例は、プライベート Docker レジストリからのイメージを使用して推定器を設定および実行する方法を示しています。

1. 次のコード例に示すように、必要なライブラリと依存関係をインポートします。

   ```
   import boto3
   import sagemaker
   from sagemaker.estimator import Estimator
   
   session = sagemaker.Session()
   
   role = sagemaker.get_execution_role()
   ```

1. 次のコード例に示すように、トレーニングイメージ、セキュリティグループ、トレーニングジョブの VPC 設定用サブネットに Uniform Resource Identifier (URI) を指定します。

   ```
   image_uri = "myteam.myorg.com/docker-local/my-training-image:<IMAGE-TAG>"
   
   security_groups = ["sg-0123456789abcdef0"]
   subnets = ["subnet-0123456789abcdef0", "subnet-0123456789abcdef0"]
   ```

   `security_group_ids` および `subnets` の詳細については、SageMaker Python SDK の「[推定器](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html)」セクションにある該当するパラメータの説明を参照してください。
**注記**  
SageMaker AI は VPC 内のネットワーク接続を使用して Docker レジストリ内のイメージにアクセスします。Docker レジストリ内のイメージをトレーニングに使用するには、レジストリがアカウントの Amazon VPC からアクセス可能である必要があります。

1. 必要に応じて、Docker レジストリで認証が必要な場合は、SageMaker AI にアクセス認証情報を提供する AWS Lambda 関数の Amazon リソースネーム (ARN) も指定する必要があります。次のコード例は、ARN を指定する方法を示しています。

   ```
   training_repository_credentials_provider_arn = "arn:aws:lambda:us-west-2:1234567890:function:test"
   ```

   認証が必要な Docker レジストリでイメージを使用する方法の詳細については、以下の「**Use a Docker registry that requires authentication for training**」を参照してください。

1. 次のコード例に示すように、前のステップのコード例を使用して推定器を設定します。

   ```
   # The training repository access mode must be 'Vpc' for private docker registry jobs 
   training_repository_access_mode = "Vpc"
   
   # Specify the instance type, instance count you want to use
   instance_type="ml.m5.xlarge"
   instance_count=1
   
   # Specify the maximum number of seconds that a model training job can run
   max_run_time = 1800
   
   # Specify the output path for the model artifacts
   output_path = "s3://your-output-bucket/your-output-path"
   
   estimator = Estimator(
       image_uri=image_uri,
       role=role,
       subnets=subnets,
       security_group_ids=security_groups,
       training_repository_access_mode=training_repository_access_mode,
       training_repository_credentials_provider_arn=training_repository_credentials_provider_arn,  # remove this line if auth is not needed
       instance_type=instance_type,
       instance_count=instance_count,
       output_path=output_path,
       max_run=max_run_time
   )
   ```

1. 次のコード例に示すように、ジョブ名と入力パスをパラメータとして使用して `estimator.fit` を呼び出し、トレーニングジョブを開始します。

   ```
   input_path = "s3://your-input-bucket/your-input-path"
   job_name = "your-job-name"
   
   estimator.fit(
       inputs=input_path,
       job_name=job_name
   )
   ```