Speed

Apply Scaling to a New Row

Apply frozen training minimums and spans to an unseen observation whose values step outside the training interval.

The problem

In deployment, a trained model receives one new row at a time, and nothing prevents a new sensor reading or customer profile from exceeding the largest value seen during training or falling below the smallest. Suppose three training rows are [[10, 200], [30, 400], [50, 600]], and a new row arrives at [60, 100] (last row of input).

How should min-max scaling transform [60, 100]? Two tempting shortcuts fail here: recomputing lo\mathrm{lo} and hi\mathrm{hi} over all four rows shifts the coordinate system under the trained model's feet, while clamping outputs into [0,1][0, 1] erases how far outside the training box the new row sits.

Work through the numbers

Pin column minimums and spans on the three training rows [[10, 200], [30, 400], [50, 600]] alone. Column 00 has lo0=10\mathrm{lo}_0 = 10, hi0=50\mathrm{hi}_0 = 50, and span 4040. Column 11 has lo1=200\mathrm{lo}_1 = 200, hi1=600\mathrm{hi}_1 = 600, and span 400400.

Apply xnew−lohi−lo\frac{x_{\mathrm{new}} - \mathrm{lo}}{\mathrm{hi} - \mathrm{lo}} directly to the new row [60, 100]. In column 00, 60−1040=5040=1.25\frac{60 - 10}{40} = \frac{50}{40} = 1.25, placing 6060 one-quarter of a training span above 1.01.0. In column 11, 100−200400=−100400=−0.25\frac{100 - 200}{400} = \frac{-100}{400} = -0.25, placing 100100 one-quarter of a training span below 0.00.0.

The exact scaled vector is [1.25, -0.25]. Neither refitting all four rows (which would return [1.0, 0.0]) nor clamping to [0,1][0, 1] (which would also return [1.0, 0.0]) preserves that 25 percent extrapolation on both axes.

The live model

A vertical zero line and [0,1][0, 1] reference frame show signed scaled bars for the new arrival as new_col0 sweeps from 00 to 7070 (holding new_col1 at 100100, scaled −0.25-0.25). At default new_col0 =60= 60, bars show [1.25, -0.25]. At new_col0 =0= 0, column 00 drops below zero to [-0.25, -0.25]; at 1010 it sits on the lower edge [0, -0.25]; at 3030 it reaches [0.5, -0.25]; at 5050 it hits [1.0, -0.25]; at 7070 it extends to [1.5, -0.25].

Signed coordinate bars relative to [0, 1] training bounds showing col 0 at +1.25 and col 1 at -0.25 for new row [60, 100].
let new_col0 = scene.parameter("new_col0", 60.0, 0.0, 70.0, 10.0);
let n = input.size(0);
let train = input.slice(0, 0, n - 1);
let r0 = torch.full(1, new_col0).to(DType.F64);
let r1 = input.select(0, n - 1).slice(0, 1, 2);
let row = torch.cat(torch.list().add(r0).add(r1), 0);
let lo = train.amin(0);
let hi = train.amax(0);
let answer = (row - lo).div(hi - lo);
out.set("scaled_new_row", answer);
{
  "scaled_new_row": [
    1.25,
    -0.25
  ]
}
Watch the parameter change

Your turn

The starter program lets the incoming row redefine the feature bounds, returning [1.0, 0.0] instead of [1.25, -0.25]. Set answer to the un-clamped scaled feature vector for the final row of input using min-max bounds pinned strictly on the preceding training rows; Check tests unseen rows that sit below lo\mathrm{lo}, inside [lo,hi][\mathrm{lo}, \mathrm{hi}], and above hi\mathrm{hi}.

Starter bars refitting min and max on the new row to return [1.0, 0.0] instead of [1.25, -0.25].
let n = input.size(0);
let row = input.select(0, n - 1);
let lo = input.amin(0);
let hi = input.amax(0);
let answer = (row - lo).div(hi - lo);
out.set("scaled_new_row", answer);
{
  "scaled_new_row": [
    1,
    0
  ]
}

Pin column minimums and maximums on the training rows input[0..N-1] and return the un-clamped scaled vector for the new row input[N-1].

Read the result carefully

Why is a scaled value of 1.251.25 or −0.25-0.25 desirable rather than an error? A linear model trained on scaled coordinates learned a slope ww per unit of training span; feeding 1.251.25 lets that linear rule extrapolate smoothly for x=60x = 60, whereas clamping 6060, 7070, and 500500 all to 1.01.0 makes the model blind to every value above 5050.

At the same time, extreme values like 4.54.5 or −3.0-3.0 serve as an immediate out-of-distribution alert in production monitoring: any coordinate outside [0,1][0, 1] tells you at a glance that an incoming feature has left the training range.

Recap

  • New rows are scaled with the frozen training vectors xnew−lotrainhitrain−lotrain\frac{x_{\mathrm{new}} - \mathrm{lo}_{\mathrm{train}}}{\mathrm{hi}_{\mathrm{train}} - \mathrm{lo}_{\mathrm{train}}}, never by updating lo\mathrm{lo} or hi\mathrm{hi}.
  • Values above hitrain\mathrm{hi}_{\mathrm{train}} map cleanly above 1.01.0 and values below lotrain\mathrm{lo}_{\mathrm{train}} map cleanly below 0.00.0 ([60, 100] becomes [1.25, -0.25]).
  • Clamping new scaled features to [0,1][0, 1] discards extrapolation signal and conflates distinct out-of-range inputs.