提交机器学习训练任务到 AWS SageMaker——打包代码、上传至 S3、在 GPU/CPU 实例上启动、轮询状态并下载产物。适用于训练机器学习模型。
Submit ML training jobs to AWS SageMaker from the command line. Supports PyTorch, TensorFlow, scikit-learn, and XGBoost with managed spot training for cost savings.
boto3 Python package installed ( pip install boto3). sagemaker recommended.
AWS credentials available — EC2 instance profile (recommended), or aws configure / env vars ( AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
S3 bucket for training artifacts
Two IAM roles configured — see references/setup.md for exact policies:
.git, .env, venv, __pycache__, and other
non-essential files. Use --source-dir to explicitly scope what gets packaged.
Always review --dry-run output before submitting to production.references/setup.md.Follow the SageMaker training script contract: read data from SM_CHANNEL_TRAIN,
save model to SM_MODEL_DIR. See references/training-scripts.md for templates.
bash
python3 scripts/sagemaker_train.py \
--job-name my-experiment-001 \
--script ./train.py \
--role arn:aws:iam::ACCOUNT:role/SageMakerRole \
--bucket my-sagemaker-bucket \
--instance-type ml.g5.xlarge \
--spot \
--framework pytorch \
--input-data s3://my-bucket/data/train/ \
--hyperparameters '{"epochs":"50","lr":"0.001"}' \
--output-dir ./results
The script packages your code, uploads to S3, submits the job, polls until
complete, and downloads model artifacts to --output-dir.
bash
# Estimate before running
python3 scripts/sagemaker_cost.py --instance-type ml.g5.xlarge --duration 3600 --spot
# Check actual cost after job completes
python3 scripts/sagemaker_cost.py --job-name my-experiment-001
bash
python3 scripts/sagemaker_list.py --max 5
python3 scripts/sagemaker_list.py --status Failed
FlagPurposeDefault--spotManaged spot training (up to 70% savings)off--instance-typeCompute instanceml.g5.xlarge--max-runtimeKill job after N seconds3600--frameworkpytorch, tensorflow, sklearn, xgboostpytorch--image-uriCustom Docker image (overrides framework)auto--requirementsrequirements.txt for extra depsnone--dry-runPrint config without submittingoff--no-waitSubmit and exit without pollingoff--resume JOBReconnect to a running/completed job (skip submission)—--source-dirDirectory with all training codescript's parent--input-dataS3 input(s), format: channel:s3://...none--envJSON environment variables{}
For tabular/Kaggle workloads:
ml.m5.2xlarge (CPU, $0.54/hr)ml.g4dn.xlarge (T4, $0.74/hr) — cheapest GPUml.g5.xlarge (A10G, $1.41/hr) — best price/performanceml.p3.2xlarge (V100, $4.28/hr)Always use --spot for non-urgent training — typical savings of 30-70%.
For autonomous agents running training jobs in a loop:
references/training-scripts.md--dry-run first to validate configsagemaker_train.py — it blocks until completion by default--output-dirFor parallel experiments, use --no-wait and poll with sagemaker_list.py.
Verify the entire pipeline works end-to-end (~$0.01, takes ~3 min):
bash
python3 scripts/sagemaker_smoke_test.py \
--role arn:aws:iam::ACCOUNT:role/SageMakerTrainingExecutionRole \
--bucket my-sagemaker-bucket
This runs a local pre-flight, submits a minimal job to SageMaker, verifies
the downloaded model artifact, and checks cost. Use --keep to preserve output files.