MODELCATALOG

Build & train

Build & train

ModelTrainer

This is the central high-level V3 training abstraction.

Inputs

training image + source code + compute + channels + hyperparameters

Outputs

training job + logs + model artifact in S3

Core explanation

In V3 I think ModelTrainer first: configure the training environment, supply input data, then launch the managed training job.

01

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.
Lifecycle position
training image + compute + hyperparameters + input data -> ModelTrainer.train() -> SageMaker Training Job
02

Implementation anatomy

The sequence you would actually build or configure.

  1. 1

    Choose image/framework version.

  2. 2

    Configure role and compute.

  3. 3

    Set hyperparameters/environment.

  4. 4

    Supply named inputs.

  5. 5

    Run managed training.

  6. 6

    Write model assets to /opt/ml/model and emit metrics.

Objects / terms worth remembering
sagemaker.train.ModelTrainerComputeOutputDataConfig
03

Concrete example

A real model-development or production flow.

1

scikit-learn image

↓
2

train + validation S3 inputs

↓
3

TF-IDF fit on train only

↓
4

LogisticRegression fit

↓
5

validation F1 logged

↓
6

vectorizer/model written to /opt/ml/model

04

Implementation lab

Concrete code, data manipulation and configuration for this lifecycle step.

01

1. Package your training source

python
from sagemaker.train.configs import SourceCode

source_code = SourceCode(
    source_dir="src",
    entry_script="train.py",
    ignore_patterns=[
        ".git", ".env", "__pycache__", "data", ".venv"
    ],
)
02

2. Configure and launch ModelTrainer

python

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,
)
03

3. Parse SageMaker paths and hyperparameters inside train.py

python
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.

✓ image version✓ IAM role✓ instance type/count✓ input locations✓ hyperparameters✓ metrics✓ artifact path