SageMaker Training Basics
Six examples to get you launching and monitoring SageMaker training jobs - four basic and two intermediate.
Busque em todas as páginas da documentação
Six examples to get you launching and monitoring SageMaker training jobs - four basic and two intermediate.
Each example shows the same operation in Python (boto3) and TypeScript (AWS SDK v3), using a built-in algorithm (XGBoost) as the running example.
pip install boto3 (boto3 1.43.x, Python 3.10+). TypeScript: npm install @aws-sdk/client-sagemaker (Node.js 18+).aws configure, environment variables, or an IAM role - the SDK resolves them automatically.# --- Python (boto3) ---
import boto3
sm = boto3.client("sagemaker", region_name="us-east-1")// --- TypeScript (AWS SDK v3) ---
import { SageMakerClient } from "@aws-sdk/client-sagemaker";
const sm = new SageMakerClient({ region: "us-east-1" });InputDataConfig is a list of named channels. Each channel says where its data lives in S3 and what content type it is.
# --- Python (boto3) ---
input_data_config = [{
"ChannelName": "train",
"DataSource": {
"S3DataSource": {
"S3DataType": "S3Prefix",
"S3Uri": "s3://my-ml-bucket/training-data/train/",
"S3DataDistributionType": "FullyReplicated",
}
},
"ContentType": "text/csv",
}]// --- TypeScript (AWS SDK v3) ---
const inputDataConfig = [{
ChannelName: "train",
DataSource: {
S3DataSource: {
S3DataType: "S3Prefix",
S3Uri: "s3://my-ml-bucket/training-data/train/",
S3DataDistributionType: "FullyReplicated",
},
},
ContentType: "text/csv",
}];ChannelName becomes an environment variable inside the container (SM_CHANNEL_TRAIN), which is how the training code finds the data.S3Uri can point at a single object or a prefix; S3Prefix pulls down every object under it.validation) the same way if your algorithm needs held-out data.FullyReplicated copies the full dataset to every training instance; distributed training pages cover sharding.Related: Training Jobs: Built-in Algorithms vs Custom Containers.
ResourceConfig picks the compute; OutputDataConfig says where the trained model artifact should be written.
# --- Python (boto3) ---
resource_config = {
"InstanceType": "ml.m5.xlarge",
"InstanceCount": 1,
"VolumeSizeInGB": 30,
}
output_data_config = {"S3OutputPath": "s3://my-ml-bucket/training-output/"}// --- TypeScript (AWS SDK v3) ---
const resourceConfig = {
InstanceType: "ml.m5.xlarge",
InstanceCount: 1,
VolumeSizeInGB: 30,
};
const outputDataConfig = { S3OutputPath: "s3://my-ml-bucket/training-output/" };InstanceCount above 1 splits work across a cluster; single-instance jobs are the simplest starting point.VolumeSizeInGB sizes the EBS volume attached to each training instance for data and checkpoints.S3OutputPath, so multiple jobs never collide.ml.g5.xlarge) matter for deep learning workloads, not for lightweight algorithms like XGBoost on small data.CreateTrainingJob ties the algorithm image, IAM role, data, resources, and stopping condition together into one job.
# --- Python (boto3) ---
sm.create_training_job(
TrainingJobName="xgboost-basics-001",
AlgorithmSpecification={
# AWS-published built-in algorithm image, referenced like any other container.
"TrainingImage": "683313688378.dkr.ecr.us-east-1.amazonaws.com/xgboost:1.7-1",
"TrainingInputMode": "File",
},
RoleArn="arn:aws:iam::111122223333:role/SageMakerExecutionRole",
InputDataConfig=input_data_config,
OutputDataConfig=output_data_config,
ResourceConfig=resource_config,
HyperParameters={"num_round": "100", "objective": "binary:logistic"},
StoppingCondition={"MaxRuntimeInSeconds": 3600},
)// --- TypeScript (AWS SDK v3) ---
import { CreateTrainingJobCommand } from "@aws-sdk/client-sagemaker";
await sm.send(new CreateTrainingJobCommand({
TrainingJobName: "xgboost-basics-001",
AlgorithmSpecification: {
// AWS-published built-in algorithm image, referenced like any other container.
TrainingImage: "683313688378.dkr.ecr.us-east-1.amazonaws.com/xgboost:1.7-1",
TrainingInputMode: "File",
},
RoleArn: "arn:aws:iam::111122223333:role/SageMakerExecutionRole",
InputDataConfig: inputDataConfig,
OutputDataConfig: outputDataConfig,
ResourceConfig: resourceConfig,
HyperParameters: { num_round: "100", objective: "binary:logistic" },
StoppingCondition: { MaxRuntimeInSeconds: 3600 },
}));HyperParameters values are always strings, even for numeric settings - the container parses them.MaxRuntimeInSeconds is a hard cap; SageMaker stops the job (and billing) once it is reached.TrainingJobArn - the job runs asynchronously from here.DescribeTrainingJob reports the current TrainingJobStatus - InProgress, Completed, Failed, or Stopped - plus a failure reason if it did not succeed.
# --- Python (boto3) ---
desc = sm.describe_training_job(TrainingJobName="xgboost-basics-001")
print("status:", desc["TrainingJobStatus"])
if desc["TrainingJobStatus"] == "Failed":
print("reason:", desc["FailureReason"])// --- TypeScript (AWS SDK v3) ---
import { DescribeTrainingJobCommand } from "@aws-sdk/client-sagemaker";
const desc = await sm.send(new DescribeTrainingJobCommand({ TrainingJobName: "xgboost-basics-001" }));
console.log("status:", desc.TrainingJobStatus);
if (desc.TrainingJobStatus === "Failed") {
console.log("reason:", desc.FailureReason);
}InProgress covers everything from provisioning through the container actually running.FailureReason is a plain-text summary - the full logs live in CloudWatch Logs under /aws/sagemaker/TrainingJobs.SecondaryStatus gives a finer-grained phase (Starting, Downloading, Training, Uploading) if you need it.Instead of hand-rolled polling, use the training_job_completed_or_stopped waiter (boto3) or its SDK v3 equivalent.
# --- Python (boto3) ---
waiter = sm.get_waiter("training_job_completed_or_stopped")
waiter.wait(
TrainingJobName="xgboost-basics-001",
WaiterConfig={"Delay": 30, "MaxAttempts": 120}, # up to 60 minutes
)
print("job finished")// --- TypeScript (AWS SDK v3) ---
import { waitUntilTrainingJobCompletedOrStopped } from "@aws-sdk/client-sagemaker";
await waitUntilTrainingJobCompletedOrStopped(
{ client: sm, maxWaitTime: 3600 },
{ TrainingJobName: "xgboost-basics-001" },
);
console.log("job finished");DescribeTrainingJob on your behalf and returns once the status is terminal (Completed, Failed, or Stopped).Failed in every SDK - always check TrainingJobStatus afterward rather than assuming success.Once the job completes, ModelArtifacts.S3ModelArtifacts is the S3 path your deployment stage will use next.
# --- Python (boto3) ---
desc = sm.describe_training_job(TrainingJobName="xgboost-basics-001")
artifact_uri = desc["ModelArtifacts"]["S3ModelArtifacts"]
print("model artifact:", artifact_uri) # e.g. s3://.../xgboost-basics-001/output/model.tar.gz// --- TypeScript (AWS SDK v3) ---
const desc2 = await sm.send(new DescribeTrainingJobCommand({ TrainingJobName: "xgboost-basics-001" }));
const artifactUri = desc2.ModelArtifacts?.S3ModelArtifacts;
console.log("model artifact:", artifactUri);TrainingJobStatus is Completed - it is absent while the job is running.CreateModel expects for its ModelDataUrl/PrimaryContainer.ModelDataUrl field when you deploy.model.tar.gz; its internal contents depend entirely on what the algorithm's container wrote.Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última atualização: 24 de jul. de 2026