

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

# Créez une tâche de formation à l'aide de l'API AWS CLI, du SageMaker SDK
<a name="use-training-plan-for-training-jobs-using-api-cli-sdk"></a>

Pour utiliser SageMaker des plans de SageMaker formation pour votre tâche de formation, spécifiez le `TrainingPlanArn` paramètre du plan souhaité `ResourceConfig` lors de l'appel de l'opération [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)API. Vous pouvez utiliser un et un seul plan par tâche.

**Important**  
Le champ `InstanceType` défini dans la section `ResourceConfig` de la demande `CreateTrainingJob` doit correspondre à l’élément `InstanceType` de votre plan d’entraînement.

## Exécution d’une tâche d’entraînement sur un plan à l’aide de l’interface de ligne de commande
<a name="training-job-cli"></a>

L'exemple suivant montre comment créer une tâche de SageMaker formation et l'associer à un plan de formation fourni à l'aide de l'`TrainingPlanArn`attribut de la `create-training-job` AWS CLI commande. 

Pour plus d'informations sur la création d'une tâche de formation à l'aide de la AWS CLI [CreateTrainingJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)commande, consultez [https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-training-job.html](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-training-job.html).

```
# 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"
        }
    }' \
    ...
```

Cet AWS CLI exemple de commande crée une nouvelle tâche de formation en SageMaker IA en utilisant un plan de formation dans l'`--resource-config`argument.

```
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
```

Après avoir créé la tâche d’entraînement, vous pouvez vérifier qu’elle a été correctement attribuée au plan d’entraînement en appelant l’API `DescribeTrainingJob`.

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

## Exécutez une tâche de formation sur un plan à l'aide du SDK SageMaker AI Python
<a name="training-job-sdk"></a>

Vous pouvez également créer une tâche de formation associée à un plan de formation à l'aide du [SDK SageMaker Python](https://sagemaker.readthedocs.io/en/stable/v2.html).

Si vous utilisez le SDK SageMaker Python depuis JupyterLab Studio pour créer une tâche de formation, assurez-vous que le rôle d'exécution utilisé par l'espace exécutant votre JupyterLab application dispose des autorisations requises pour utiliser les plans de SageMaker formation. Pour en savoir plus sur les autorisations requises pour utiliser les plans de SageMaker formation, consultez[IAM pour les plans de SageMaker formation](training-plan-iam-permissions.md).

L'exemple suivant montre comment créer une tâche de SageMaker formation et l'associer à un plan de formation fourni à l'aide de l'`training_plan`attribut de l'`Estimator`objet lors de l'utilisation du SDK SageMaker Python.

Pour plus d'informations sur l' SageMaker estimateur, voir [Utiliser un SageMaker estimateur pour exécuter une tâche de formation](https://docs.aws.amazon.com/sagemaker/latest/dg/docker-containers-adapt-your-own-private-registry-estimator.html).

```
import sagemaker
import boto3
from sagemaker import get_execution_role
from sagemaker.estimator import Estimator
from sagemaker.inputs import TrainingInput

# 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 = TrainingInput(
    s3_data='s3://input-path',
    distribution='ShardedByS3Key',
    s3_data_type='S3Prefix'
)

estimator = Estimator(
    entry_point='train.py',
    image_uri="123456789123.dkr.ecr.{}.amazonaws.com/image:tag",
    role=role,
    instance_count=4,
    instance_type='ml.p5.48xlarge',
    training_plan="training-plan-arn",
    volume_size=20,
    max_run=3600,
    sagemaker_session=sagemaker_session,
    output_path="s3://output-path"
)

# Create the training job
estimator.fit(inputs=trainingInput, job_name=job_name)
```

Après avoir créé la tâche d’entraînement, vous pouvez vérifier qu’elle a été correctement attribuée au plan d’entraînement en appelant l’API `DescribeTrainingJob`.

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