MODELCATALOG

Build & train

Build & train

Training container anatomy

Explains where your code, input channels and model output physically appear.

Inputs

image + code + mounted channels + hyperparameters

Outputs

logs + files under /opt/ml/model

Core explanation

The container is the execution environment; channels are mounted inputs, and my script writes the trained artifact to the SageMaker model directory.

01

Mental model

What this means before worrying about syntax.

The training image provides the runtime/framework. SageMaker exposes job inputs and expects model output in SageMaker-defined locations.

  • The container is the runtime contract for training.
  • SageMaker supplies inputs/configuration; your code produces trained assets.
  • Training and serving containers/contracts can differ.
Lifecycle position
framework image + your script + /opt/ml/input/data/* -> train -> /opt/ml/model
02

Implementation anatomy

The sequence you would actually build or configure.

  1. 1

    Container starts on managed training compute.

  2. 2

    Inputs appear under SageMaker-defined paths/environment.

  3. 3

    Entry point parses hyperparameters and channel paths.

  4. 4

    Logs flow to CloudWatch.

  5. 5

    Assets written to /opt/ml/model are packaged/uploaded.

Objects / terms worth remembering
/opt/ml/input/data/<channel>/opt/ml/modeltraining image
03

Concrete example

A real model-development or production flow.

1

/opt/ml/input/data/train -> train channel

↓
2

/opt/ml/input/data/validation -> validation channel

↓
3

/opt/ml/model/vectorizer.joblib

↓
4

/opt/ml/model/model.joblib

↓
5

SageMaker packages model directory to S3

04

Implementation lab

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

01

Inspect the runtime contract in train.py

python
import json
import os
from pathlib import Path

print("SM_MODEL_DIR =", os.environ.get("SM_MODEL_DIR"))
print("SM_CHANNEL_TRAIN =", os.environ.get("SM_CHANNEL_TRAIN"))
print("SM_CHANNEL_VALIDATION =", os.environ.get("SM_CHANNEL_VALIDATION"))

model_dir = Path(os.environ["SM_MODEL_DIR"])
model_dir.mkdir(parents=True, exist_ok=True)
02

Persist all inference-critical assets

python

Everything required to reconstruct inference behavior should be either in the artifact or in an explicitly versioned serving dependency.

import json
import joblib
from pathlib import Path

model_dir = Path(os.environ["SM_MODEL_DIR"])

joblib.dump(pipeline, model_dir / "pipeline.joblib")

(model_dir / "model_metadata.json").write_text(
    json.dumps({
        "model_family": "tfidf-logreg",
        "model_version": "17",
        "confidence_threshold": 0.75,
        "label_order": list(pipeline.classes_),
    }, indent=2)
)

Design decisions that matter

  • Managed framework image vs custom image.
  • Dependency pinning.
  • Network isolation and package download requirements.

Common failure modes

  • A local notebook dependency may not exist in the image.
  • Saving outside /opt/ml/model can omit the asset from the packaged model.
  • Training image compatibility does not guarantee serving-image compatibility.

Production readiness check

Items to close before treating this step as production-ready.

✓ entry point✓ dependencies✓ input paths✓ output path✓ logs✓ image provenance✓ network access