Training code is product code
The shipped model is the reloaded artifact, not the object still living in the trainer.
A churn notebook cannot be reproduced by the teammate expected to ship it.
This chapter moves from make the core explicit to cross the artifact boundary. We will make the idea concrete in a small code example before placing it back inside a production system with a declared latency, cost, and operator-capacity budget.
Serialize, reload, and rerun the same request; gate publication on exact or declared-tolerance parity.
Verify the behavior illustrated in train_and_verify.py, then measure decision utility, slice reliability, and system latency.
A mutable data/latest path and unpinned dependencies create a different model from the same command. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
Training code creates an artifact that another system must trust. That makes its interfaces, tests, failure behavior, and serialization path product concerns.
A useful training module separates pure computation from file and network effects. Small functions with explicit inputs can be tested on tiny fixtures, while orchestration handles loading, logging, and artifact publication.
The most important parity test crosses the serialization boundary. A model that predicts correctly in memory but differently after loading is not the product that evaluation approved.
- dependency locks
- run manifests
- schema contracts
- golden inference
Follow the reasoning before touching the code.
- 01
Make the core explicit
Pass arrays and parameters into a deterministic fit function instead of reading globals or notebook state.
- 02
Test a tiny invariant
Use a hand-checkable dataset to verify shape, finite output, and an expected prediction before expensive training.
- 03
Cross the artifact boundary
Serialize, reload, and rerun the same request; gate publication on exact or declared-tolerance parity.
import pickle
def fit(xs, ys):
assert len(xs) == len(ys) and xs
return {"slope": sum(x * y for x, y in zip(xs, ys)) / sum(x * x for x in xs)}
def predict(model, xs):
return [model["slope"] * x for x in xs]
model = fit([1.0, 2.0, 3.0], [2.0, 4.0, 6.0])
expected = predict(model, [4.0])
restored = pickle.loads(pickle.dumps(model))
assert predict(restored, [4.0]) == expected == [8.0]
print("serialized inference parity passed")Test a fitted model again after serialization.
What to notice
- The tiny fixture makes the expected slope and prediction auditable without a reference library.
- Evaluation and verification exercise the same predict function, avoiding two subtly different inference paths.
YOUR TURN Pause and predict before revealing the answer.
Add x=0 as the only training example. What production-quality behavior should replace the raw division error?
Need a hint?
The denominator is an input invariant, and the failure should explain the violated contract.
Check your reasoning
Reject the dataset before fitting with a specific validation error that says the feature has zero squared magnitude.
Lessons 1, 2 in this course.
Connect the chapter to the course’s quantitative spine.
The shipped model is the reloaded artifact, not the object still living in the trainer. The content-addressed run identity below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
Content-addressed run identity
| Symbol | Meaning / shape / unit |
|---|---|
code | source revision digest |
data | immutable dataset digest |
image | OCI image digest |
seed | declared random seed |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: code, data, image, seed.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c01(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
Reproducibility fault injector
Change how many declared inputs remain mutable and observe replay parity.
Assumption: Each mutable input independently creates a 14-point replay risk.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| input 1 | mutable | replay risk |
| input 2 | mutable | replay risk |
| input 3 | content-addressed | verified |
| input 4 | content-addressed | verified |
| input 5 | content-addressed | verified |
Trace the complete operating path.
Carry the chapter’s central idea into the system: The shipped model is the reloaded artifact, not the object still living in the trainer. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Make the core explicit” into a written invariant and a failing test.
- 02
Promote train_and_verify.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Cross the artifact boundary” as the release checkpoint: Serialize, reload, and rerun the same request; gate publication on exact or declared-tolerance parity.
- 04
Serve or materialize behind explicit a declared latency, cost, and operator-capacity budget.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
A mutable data/latest path and unpinned dependencies create a different model from the same command. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Reproducible baseline package
Design a Docker-only workflow that validates data, trains, evaluates, serializes, performs golden inference, and emits complete lineage.
- Run manifest and data contract
- Raw-input golden parity test
- Failure and retry runbook
- Model card with residual nondeterminism
Read primary material with a purpose.
Return to the opening failure.
Reject promotion when any artifact lacks lineage, golden raw-request parity, or a failure-safe recovery path.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a seeded, tested, packaged, versioned training workflow with a model card.