翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
SageMaker AI ModelTrainer を使用してトレーニングジョブを実行する
SageMaker Python SDK の ModelTrainer
-
次のコード例に示すように、必要なライブラリと依存関係をインポートします。
import boto3 import sagemaker from sagemaker.train import ModelTrainer from sagemaker.train.configs import Compute from sagemaker.core.helper.session_helper import get_execution_role, Session session = Session() role = get_execution_role() -
次のコード例に示すように、トレーニングイメージ、セキュリティグループ、トレーニングジョブの 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 の ModelTrainersセクションにある適切なパラメータの説明を参照してください。 注記
SageMaker AI は VPC 内のネットワーク接続を使用して Docker レジストリ内のイメージにアクセスします。Docker レジストリ内のイメージをトレーニングに使用するには、レジストリがアカウントの Amazon VPC からアクセス可能である必要があります。
-
必要に応じて、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」を参照してください。
-
次のコード例に示すように、前のステップのコード例を使用して ModelTrainer を設定します。
# 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" model_trainer = ModelTrainer( training_image=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 compute=Compute(instance_type=instance_type, instance_count=instance_count), output_path=output_path, max_run=max_run_time ) -
次のコード例に示すように、ジョブ名と入力パスをパラメータとして使用して
model_trainer.trainを呼び出し、トレーニングジョブを開始します。input_path = "s3://your-input-bucket/your-input-path" job_name = "your-job-name" model_trainer.train( input_data_config=input_path, job_name=job_name )