View a markdown version of this page

API、SageMaker SDK AWS CLIを使用してトレーニングジョブを作成する - Amazon SageMaker AI

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

API、SageMaker SDK AWS CLIを使用してトレーニングジョブを作成する

SageMakerトレーニングジョブに SageMaker トレーニングプランを使用するには、CreateTrainingJob APIオペレーションを呼び出す際に、ResourceConfig で必要なプランの TrainingPlanArn パラメータを指定します。ジョブごとに 1 つのプランのみを使用できます。

重要

CreateTrainingJob リクエストの ResourceConfig セクションで設定された InstanceType フィールドは、トレーニングプランの InstanceType と一致する必要があります。

CLI を使用してプランでトレーニングジョブを実行する

次の例は、 create-training-job AWS CLI コマンドの TrainingPlanArn 属性を使用して SageMaker トレーニングジョブを作成し、提供されたトレーニングプランに関連付ける方法を示しています。

AWS CLI CreateTrainingJob コマンドを使用してトレーニングジョブを作成する方法の詳細については、「」を参照してくださいcreate-training-job

# Create a training job aws sagemaker create-training-job \ --training-job-name training-job-name \ ... --resource-config '{ "InstanceType": "ml.p5.48xlarge", "InstanceCount": 8, "VolumeSizeInGB": 10, "TrainingPlanArn": "training-plan-arn" } }' \ ...

このコマンド AWS CLI 例では、SageMaker AI で新しいトレーニングジョブを作成し、 --resource-config引数でトレーニングプランを渡します。

aws sagemaker create-training-job \ --training-job-name job-name \ --role-arn arn:aws:iam::111122223333:role/DataAndAPIAccessRole \ --algorithm-specification '{"TrainingInputMode": "File","TrainingImage": "111122223333.dkr.ecr.us-east-1.amazonaws.com/algo-image:tag", "ContainerArguments": [" "]}' \ --input-data-config '[{"ChannelName":"training","DataSource":{"S3DataSource":{"S3DataType":"S3Prefix","S3Uri":"s3://bucketname/input","S3DataDistributionType":"ShardedByS3Key"}}}]' \ --output-data-config '{"S3OutputPath": "s3://bucketname/output"}' \ --resource-config '{"VolumeSizeInGB":10,"InstanceCount":4,"InstanceType":"ml.p5.48xlarge", "TrainingPlanArn" : "arn:aws:sagemaker:us-east-1:111122223333:training-plan/plan-name"}' \ --stopping-condition '{"MaxRuntimeInSeconds": 1800}' \ --region us-east-1

トレーニングジョブを作成したら、DescribeTrainingJob API を呼び出して、そのジョブがトレーニングプランに適切に割り当てられていることを検証できます。

aws sagemaker describe-training-job --training-job-name training-job-name

SageMaker AI Python SDK を使用してプランでトレーニングジョブを実行する

または、SageMaker Python SDK を使用してトレーニングプランに関連付けられたトレーニングジョブを作成することもできます。

Studio で JupyterLab の SageMaker Python SDK を使用してトレーニングジョブを作成する場合は、JupyterLab アプリケーションを実行するスペースで使用される実行ロールに、SageMaker トレーニングプランを使用するために必要なアクセス許可があることを確認します。SageMakerトレーニングプランを使用するために必要なアクセス許可の詳細については、「SageMaker トレーニングプラン向け IAM」を参照してください。

次の例は、SageMaker Python SDK を使用する際に、ModelTrainer オブジェクトの training_plan 属性を使用して SageMaker トレーニングジョブを作成し、提供されたトレーニングプランに関連付ける方法を説明しています。

SageMaker ModelTrainer の詳細については、SageMaker ModelTrainer を使用してトレーニングジョブを実行する」を参照してください。

import boto3 from sagemaker.core.helper.session_helper import get_execution_role from sagemaker.train import ModelTrainer from sagemaker.train.configs import InputData from sagemaker.train.configs import Compute, SourceCode from sagemaker.core.shapes import OutputDataConfig # Set up the session and SageMaker client session = boto3.Session() region = session.region_name sagemaker_session = session.client('sagemaker') # Get the execution role for the training job role = get_execution_role() # Define the input data configuration trainingInput = InputData( channel_name='training', data_source='s3://input-path' ) model_trainer = ModelTrainer( training_image="123456789123.dkr.ecr.{}.amazonaws.com/image:tag", source_code=SourceCode(entry_script='train.py'), role=role, compute=Compute( instance_type='ml.p5.48xlarge', instance_count=4, volume_size_in_gb=20, training_plan_arn="training-plan-arn" ), output_data_config=OutputDataConfig(s3_output_path="s3://output-path") ) # Create the training job model_trainer.train(input_data_config=[trainingInput])

トレーニングジョブを作成したら、DescribeTrainingJob API を呼び出して、そのジョブがトレーニングプランに適切に割り当てられていることを検証できます。

# Check job details sagemaker_session.describe_training_job(TrainingJobName=job_name)