Speed

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 xx, fit centered ridge regression with penalty 0.3750.375 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]: lo=10\mathrm{lo} = 10 and span=50−10=40\mathrm{span} = 50 - 10 = 40. Scaling training inputs gives utrain=[0,0.25,0.75,1]u_{\mathrm{train}} = [0, 0.25, 0.75, 1] with mean μu=0.5\mu_u = 0.5, while training targets [2, 4, 8, 10] have mean μy=6\mu_y = 6. Applying that pinned [10,50][10, 50] ruler to held-out xtest=[30,70]x_{\mathrm{test}} = [30, 70] produces utest=[0.5,1.5]u_{\mathrm{test}} = [0.5, 1.5]; x=70x = 70 maps above 1.01.0 because it exceeds the training maximum of 5050.

Fit centered ridge regression on the four training rows. Centered features uc=[−0.5,−0.25,0.25,0.5]u_c = [-0.5, -0.25, 0.25, 0.5] and centered targets yc=[−4,−2,2,4]y_c = [-4, -2, 2, 4] have cross-product sum 2+0.5+0.5+2=5.02 + 0.5 + 0.5 + 2 = 5.0 and sum of squares 0.25+0.0625+0.0625+0.25=0.6250.25 + 0.0625 + 0.0625 + 0.25 = 0.625.

Adding ridge penalty 0.3750.375 gives denominator 0.625+0.375=1.00.625 + 0.375 = 1.0, so slope w=5.0/1.0=5.0w = 5.0 / 1.0 = 5.0 and intercept b=6.0−5.0(0.5)=3.5b = 6.0 - 5.0(0.5) = 3.5. Scoring utest=[0.5,1.5]u_{\mathrm{test}} = [0.5, 1.5] yields 3.5+5.0(0.5)=6.03.5 + 5.0(0.5) = 6.0 and 3.5+5.0(1.5)=11.03.5 + 5.0(1.5) = 11.0, 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 =0.375= 0.375, test outputs are [[0.5, 6.0], [1.5, 11.0]]. At penalty =0.0= 0.0 (unregularized least squares), slope rises to 8.08.0 and intercept drops to 2.02.0, predicting [[0.5, 6.0], [1.5, 14.0]]. At penalty =0.625= 0.625, slope shrinks to 4.04.0 and predicts [[0.5, 6.0], [1.5, 10.0]]; at 1.3751.375, slope is 2.52.5 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.
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 0.3750.375) are fit strictly on the four training rows (0..3); Check tests tables whose held-out xx 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.
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 x=70x = 70 into min-max scaling changes the trained slope under ridge regularization, not only the coordinate labels. Stretching span\mathrm{span} from 4040 to 6060 scales each training uu value to two-thirds of its pinned value, shrinking ∑uc2\sum u_c^2 from 0.6250.625 to 0.27780.2778 while penalty 0.3750.375 stays fixed, which over-penalizes the slope and distorts test predictions to 9.409.40 instead of 11.011.0.

Notice also why test row [30, 7] predicts 6.06.0 at every penalty setting: its scaled coordinate u=0.5u = 0.5 sits right on the training mean μu=0.5\mu_u = 0.5, where every centered ridge line b+wub + w u passes through (μu,μy)=(0.5,6.0)(\mu_u, \mu_y) = (0.5, 6.0). 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 min\mathrm{min} and max\mathrm{max} belong to the training split and are applied unchanged to held-out rows, even when new features fall outside [0,1][0, 1].
  • 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.