Build & train
ModelTrainer
This is the central high-level V3 training abstraction.
training image + source code + compute + channels + hyperparameters
training job + logs + model artifact in S3
In V3 I think ModelTrainer first: configure the training environment, supply input data, then launch the managed training job.
Mental model
What this means before worrying about syntax.
ModelTrainer holds the training image, compute, role, hyperparameters and related job configuration, then launches managed training.
- ModelTrainer is the V3 high-level training launcher/configuration object.
- The training job is ephemeral compute; the model artifact is the durable output.
- Framework-specific fit/optimization logic still belongs to your training code.
training image + compute + hyperparameters + input data -> ModelTrainer.train() -> SageMaker Training Job
Implementation anatomy
The sequence you would actually build or configure.
- 1
Choose image/framework version.
- 2
Configure role and compute.
- 3
Set hyperparameters/environment.
- 4
Supply named inputs.
- 5
Run managed training.
- 6
Write model assets to /opt/ml/model and emit metrics.
Concrete example
A real model-development or production flow.
scikit-learn image
train + validation S3 inputs
TF-IDF fit on train only
LogisticRegression fit
validation F1 logged
vectorizer/model written to /opt/ml/model
Implementation lab
Concrete code, data manipulation and configuration for this lifecycle step.
1. Package your training source
from sagemaker.train.configs import SourceCode
source_code = SourceCode(
source_dir="src",
entry_script="train.py",
ignore_patterns=[
".git", ".env", "__pycache__", "data", ".venv"
],
)
2. Configure and launch ModelTrainer
The current V3 API accepts a list of InputData or Channel objects. The training logic is still in train.py.
from sagemaker.train import ModelTrainer
from sagemaker.train.configs import Compute, InputData
from sagemaker.core import image_uris
SKLEARN_TRAINING_IMAGE_URI = image_uris.retrieve(
framework="sklearn",
region=region,
version="1.2-1",
py_version="py3",
instance_type="ml.m5.xlarge",
image_scope="training",
)
trainer = ModelTrainer(
training_image=SKLEARN_TRAINING_IMAGE_URI,
source_code=source_code,
role=SAGEMAKER_ROLE_ARN,
compute=Compute(
instance_type="ml.m5.xlarge",
instance_count=1,
),
hyperparameters={
"C": 1.0,
"min_df": 2,
"confidence_threshold": 0.75,
},
base_job_name="complaint-tfidf-lr",
)
train_input = InputData(
channel_name="train",
data_source=TRAIN_S3_URI,
)
val_input = InputData(
channel_name="validation",
data_source=VALIDATION_S3_URI,
)
trainer.train(
input_data_config=[train_input, val_input],
wait=True,
logs=True,
)
3. Parse SageMaker paths and hyperparameters inside train.py
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument("--C", type=float, default=1.0)
parser.add_argument("--min_df", type=int, default=2)
parser.add_argument("--confidence_threshold", type=float, default=0.75)
args = parser.parse_args()
train_dir = os.environ["SM_CHANNEL_TRAIN"]
val_dir = os.environ["SM_CHANNEL_VALIDATION"]
model_dir = os.environ["SM_MODEL_DIR"]
print(args)
print(train_dir, val_dir, model_dir)
Design decisions that matter
- CPU vs GPU.
- Instance type/count and distribution.
- Image/version pinning.
- Spot/checkpointing where appropriate.
Common failure modes
- A successful training job does not prove model quality.
- Hyperparameter names must match what your script parses.
- Managed training does not replace your framework training logic.
Production readiness check
Items to close before treating this step as production-ready.