Roles Come First
Assign train and held-out split roles before inspecting feature ranges so held-out extremes never enter training bounds.
The problem
One of the most common mistakes in machine learning pipelines happens before any model is trained: a practitioner normalizes or scales the entire CSV first and splits into training and held-out rows second. Reversing that order lets held-out feature extremes dictate the ruler used during training.
Preventing preprocessing leakage begins with a strict chronological rule: split roles come first, and every summary statistic is partitioned by role before a single feature value is rescaled.
Work through the numbers
Six sensor readings carry [role, x] in table = [[1, 10], [1, 20], [0, 50], [1, 30], [0, 5], [1, 25]], where role = 1 marks training rows and role = 0 marks held-out rows. Notice where the two extreme values sit: r2 = 50 and r4 = 5 both belong to the held-out set.
Separating x by role before computing bounds yields two distinct intervals:
- Training rows (
r0 = 10,r1 = 20,r3 = 30,r5 = 25):tr_min = 10andtr_max = 30. - Held-out rows (
r2 = 50,r4 = 5):te_min = 5andte_max = 50.
Stacking [tr_min, tr_max, te_min, te_max] gives [10, 30, 5, 50]. To compute a masked minimum in tensor code without zero-pulling positive numbers, replace excluded slots with hi = x.max() + 1 (51) before .min(), and replace excluded slots with lo = x.min() - 1 (4) before .max().
The live model
The left panel colors training rows blue and held-out rows amber; the right panel reports [tr_min, tr_max] above [te_min, te_max]. At the default r2_role = 0, r2 (x = 50) stays held-out, so train spans [10, 30] and held-out spans [5, 50]. Slide r2_role to 1 to reassign r2 to training: tr_max jumps to 50 ([10, 50]) while r4 remains the lone held-out row at [5, 5].
![Role-separated feature intervals: train rows span [10, 30] while held-out rows span [5, 50].](../assets/ml/roles-come-first--demo.png)
let r2_role = scene.parameter("r2_role", 0.0, 0.0, 1.0, 1.0);
let table = input.clone();
table.select(0, 2).set(0, r2_role);
let role = table.select(1, 0);
let x = table.select(1, 1);
let hi = x * 0.0 + x.max() + 1.0;
let lo = x * 0.0 + x.min() - 1.0;
let is_tr = role.eq(1.0);
let is_te = role.eq(0.0);
let tr_min = torch.where(is_tr, x, hi).min().reshape(1);
let tr_max = torch.where(is_tr, x, lo).max().reshape(1);
let te_min = torch.where(is_te, x, hi).min().reshape(1);
let te_max = torch.where(is_te, x, lo).max().reshape(1);
let ls = torch.list().add(tr_min).add(tr_max).add(te_min).add(te_max);
let answer = torch.cat(ls, 0);
out.set("bounds", answer);
{
"bounds": [
10,
30,
5,
50
]
}Your turn
Look at the two interval cards on the starter canvas: both read [5, 50], because the training bounds currently inspect the entire column and pull in held-out rows r2 = 50 and r4 = 5. Restrict tr_min and tr_max to the training split so answer reports [tr_min, tr_max, te_min, te_max]. Check includes a probe where every training value is negative (-22 to -18), so zero-filling excluded rows cannot pass either.
![Calling x.min() and x.max() without role == 1 leaks held-out extremes [5, 50] into the training interval.](../assets/ml/roles-come-first--exercise.png)
let table = input;
let role = table.select(1, 0);
let x = table.select(1, 1);
let hi = x * 0.0 + x.max() + 1.0;
let lo = x * 0.0 + x.min() - 1.0;
let is_te = role.eq(0.0);
let tr_min = x.min().reshape(1);
let tr_max = x.max().reshape(1);
let te_min = torch.where(is_te, x, hi).min().reshape(1);
let te_max = torch.where(is_te, x, lo).max().reshape(1);
let ls = torch.list().add(tr_min).add(tr_max).add(te_min).add(te_max);
let answer = torch.cat(ls, 0);
out.set("bounds", answer);
{
"bounds": [
5,
50,
5,
50
]
}Compute [tr_min, tr_max, te_min, te_max] in answer so the training bounds inspect only training rows and the held-out bounds inspect only held-out rows.
Isolating the training split narrows the top card to [10, 30] and sets answer to [10, 30, 5, 50].
Read the result carefully
Why use out-of-range sentinels hi = x.max() + 1 and lo = x.min() - 1 instead of multiplying x * role? Multiplying held-out rows by 0 injects synthetic zeros into the column: that drags tr_min down to 0 whenever all training readings are positive ([10, 20, 30, 25]) and pulls tr_max up to 0 whenever all training readings are negative ([-18, -22, -20]).
Recap
- Split role labels (
1for train,0for held-out) must be fixed before computing any feature statistic. - Held-out rows routinely fall below
tr_min(5 < 10) or abovetr_max(50 > 30); keeping roles separate makes that gap explicit. - Sentinel replacement (
hi = x.max() + 1for.min(),lo = x.min() - 1for.max()) computes exact masked bounds without zero-distortion.