Practice in Lab: Project: train, validate and export a nonlinear classifier →
Project: train, validate and export a nonlinear classifier
This project trains a nonlinear classifier with real PyTorch autograd and Adam. Preprocessing belongs to the training partition; validation selects a historical checkpoint; test labels only measure the frozen result. Copying a state dictionary is essential because live weights continue changing. The exported artifact contains both preprocessing and model parameters, allowing independent inference after JSON serialization. The tiny XOR fixture demonstrates nonlinear learning and data boundaries. It is a CPU training exercise rather than evidence of generalization on a representative production dataset.
import torch
x = torch.tensor([[0., 1.], [1., 0.]])
print(x.std(0, correction=0))Review the design
Audit fitted statistics, checkpoint ownership, deterministic initialization and held-out identities. A passing synthetic test is not an estimate of real deployment performance.
Your task
Rows contain unique disjoint id, finite two-feature x and integer binary y. Fit population means/stds on train only (constant scale=1), then train Linear(2,8), Tanh, Linear(8,2) with Adam lr=.04 for steps full-batch updates. Select a deep-copied model state by lowest validation cross entropy, first exact tie. Restore it and evaluate test. Return artifact with state/means/scales, predictions, initial_loss, training_loss, validation_loss, best_step, test_loss and validation history. Preserve caller CPU RNG using fork_rng. Reject invalid partitions and steps. Trace uses four updates; checks train longer.
The explanation opens here once every check passes.