What This Track Showed
Combine train-only min-max scaling, regularized least squares, and held-out evaluation in one leak-free tensor pipeline.
The problem
Across this book we saw why paper calculations stop: tables grow to thousands of unseen cells, gradients triple per-layer work across epochs, activations scale with batch size, attention matrices grow quadratically with context, and float32 rounding breaks associativity. Yet the exact rules taught on tiny tables never become obsolete; tensor code executes those identical rules at scale, provided data boundaries stay intact.
To close the book, audit a six-row pipeline [[10, 2], [20, 4], [40, 8], [50, 10], [30, 7], [70, 13]] where rows 0..3 form the training split and rows 4..5 ([30, 7] and [70, 13]) are held-out test rows. Pin min-max scaling on training , fit centered ridge regression with penalty on training rows, and return [u_test, pred_test] on the held-out split.
Work through the numbers
Pin the feature range on the four training inputs [10, 20, 40, 50]: and . Scaling training inputs gives with mean , while training targets [2, 4, 8, 10] have mean . Applying that pinned ruler to held-out produces ; maps above because it exceeds the training maximum of .
Fit centered ridge regression on the four training rows. Centered features and centered targets have cross-product sum and sum of squares .
Adding ridge penalty gives denominator , so slope and intercept . Scoring yields and , giving [[0.5, 6.0], [1.5, 11.0]].
The live model
Blue dots mark the four training pairs in scaled coordinates, amber dots mark true held-out targets, and green markers show the fitted line and test predictions. At default penalty , test outputs are [[0.5, 6.0], [1.5, 11.0]]. At penalty (unregularized least squares), slope rises to and intercept drops to , predicting [[0.5, 6.0], [1.5, 14.0]]. At penalty , slope shrinks to and predicts [[0.5, 6.0], [1.5, 10.0]]; at , slope is and predicts [[0.5, 6.0], [1.5, 8.5]].
![Train points in [0, 1], held-out targets at u = 0.5 and 1.5, and leak-free ridge predictions [6.0, 11.0] at penalty 0.375.](../assets/ml/what-this-track-showed--demo.png)
let penalty = scene.parameter("penalty", 0.375, 0.0, 1.375, 0.125);
let n = input.size(0);
let tr_x = input.slice(0, 0, 4).select(1, 0);
let tr_y = input.slice(0, 0, 4).select(1, 1);
let te_x = input.slice(0, 4, n).select(1, 0);
let lo = tr_x.min();
let span = tr_x.max() - lo;
let u_tr = (tr_x - lo).div(span);
let u_te = (te_x - lo).div(span);
let uc = u_tr - u_tr.mean();
let yc = tr_y - tr_y.mean();
let w = (uc * yc).sum().div(uc.square().sum() + penalty);
let b = tr_y.mean() - w * u_tr.mean();
let pred = b + w * u_te;
let cols = torch.list().add(u_te.unsqueeze(1)).add(pred.unsqueeze(1));
let answer = torch.cat(cols, 1);
out.set("test_scaled_and_pred", answer);
{
"test_scaled_and_pred": [
[
0.5,
6
],
[
1.5,
11
]
]
}Watch the parameter change
Your turn
The starter pipeline contaminates training preprocessing with held-out rows, distorting both scaled test coordinates and the regularized slope. Set answer to [u_test, pred_test] for the held-out rows (4..5) when min-max scaling and centered ridge regression (penalty ) are fit strictly on the four training rows (0..3); Check tests tables whose held-out values fall inside, above, and below the training interval.
![Starter pipeline leaking test row 70 into min-max scaling, compressing u_test to [0.33, 1.0] and distorting predictions.](../assets/ml/what-this-track-showed--exercise.png)
let n = input.size(0);
let all_x = input.select(1, 0);
let tr_x = input.slice(0, 0, 4).select(1, 0);
let tr_y = input.slice(0, 0, 4).select(1, 1);
let te_x = input.slice(0, 4, n).select(1, 0);
let lo = all_x.min();
let span = all_x.max() - lo;
let u_tr = (tr_x - lo).div(span);
let u_te = (te_x - lo).div(span);
let uc = u_tr - u_tr.mean();
let yc = tr_y - tr_y.mean();
let w = (uc * yc).sum().div(uc.square().sum() + 0.375);
let b = tr_y.mean() - w * u_tr.mean();
let pred = b + w * u_te;
let cols = torch.list().add(u_te.unsqueeze(1)).add(pred.unsqueeze(1));
let answer = torch.cat(cols, 1);
out.set("test_scaled_and_pred", answer);
{
"test_scaled_and_pred": [
[
0.3333333333333333,
6
],
[
1,
9.404255319148936
]
]
}Return [u_test, pred_test] for the held-out rows (index 4 onward) using min-max bounds and centered ridge regression (penalty 0.375) fit strictly on the four training rows (indices 0..3).
Read the result carefully
Notice why leaking into min-max scaling changes the trained slope under ridge regularization, not only the coordinate labels. Stretching from to scales each training value to two-thirds of its pinned value, shrinking from to while penalty stays fixed, which over-penalizes the slope and distorts test predictions to instead of .
Notice also why test row [30, 7] predicts at every penalty setting: its scaled coordinate sits right on the training mean , where every centered ridge line passes through . Exact toy examples make invariants like these visible so you can trust the same tensor code when dimensions grow by orders of magnitude.
Recap
- Preprocessing statistics such as and belong to the training split and are applied unchanged to held-out rows, even when new features fall outside .
- Leaking test rows into feature scaling alters regularized weights by changing training feature variance relative to the fixed penalty.
- Small exact tables verify invariants and split boundaries, while tensor operations carry the identical algebra to production scale.