Speed

Column Order Is Part of the Data

Align one-hot column order strictly to the trained weight vector so category predictions never scramble at inference time.

The problem

A trained linear model stores its parameters by position, not by category name. Suppose a model was fit on the pinned vocabulary order [10, 20, 30] and learned category weights w=w = [2, -1, 5] plus intercept b=10b = 10. Weight 22 belongs to column 00 (code 1010), weight −1-1 belongs to column 11 (code 2020), and weight 55 belongs to column 22 (code 3030).

Now four inference rows arrive with category codes [10, 20, 30, 10]. If the serving pipeline encodes those rows in the pinned order [10, 20, 30], every row hits its trained weight in y^=Xw+b\hat{y} = X w + b. What happens if someone re-sorts or reverses the encoding columns to [30, 20, 10] without permuting ww?

Work through the numbers

First encode [10, 20, 30, 10] against the pinned order [10, 20, 30]. Row 00 (code 1010) becomes [1, 0, 0]. Row 11 (code 2020) becomes [0, 1, 0]. Row 22 (code 3030) becomes [0, 0, 1]. Row 33 (code 1010) becomes [1, 0, 0].

Evaluate y^i=Xiw+10\hat{y}_i = X_i w + 10 with w=w = [2, -1, 5]. Row 00 predicts 10+2=1210 + 2 = 12. Row 11 predicts 10+(−1)=910 + (-1) = 9. Row 22 predicts 10+5=1510 + 5 = 15. Row 33 predicts 10+2=1210 + 2 = 12. The exact prediction vector is [12, 9, 15, 12].

Now reverse the vocabulary at inference time to [30, 20, 10] while keeping w=w = [2, -1, 5]. Row 00 (code 1010) lands in column 22 with weight 55, predicting 10+5=1510 + 5 = 15 instead of 1212. Row 11 (code 2020) stays in middle column 11 and predicts 99. Row 22 (code 3030) lands in column 00 with weight 22, predicting 10+2=1210 + 2 = 12 instead of 1515. Predictions scramble to [15, 9, 12, 15] with zero runtime errors.

The live model

Four horizontal bars compare each row's predicted score against the trained reference [12, 9, 15, 12]. At default swap_order =0= 0, vocabulary order [10, 20, 30] aligns with w=w = [2, -1, 5] and all four bars match [12, 9, 15, 12] in blue. Drag swap_order to 11 to flip encoding order to [30, 20, 10]: rows 00, 22, and 33 jump to 1515, 1212, and 1515 in amber while middle row 11 stays at 99.

Four horizontal prediction bars showing [12, 9, 15, 12] in blue when [10, 20, 30, 10] is encoded in pinned order [10, 20, 30].
let swap_order = scene.parameter("swap_order", 0.0, 0.0, 1.0, 1.0);
let vocab = torch.tensor("[10.0, 20.0, 30.0]", DType.F64);
if (swap_order > 0.5) {
  vocab = torch.tensor("[30.0, 20.0, 10.0]", DType.F64);
}
let enc = input.unsqueeze(1).eq(vocab.unsqueeze(0)).to(DType.F64);
let w = torch.tensor("[2.0, -1.0, 5.0]", DType.F64).reshape(3, 1);
let answer = enc.matmul(w).reshape(input.size(0)) + 10.0;
out.set("predictions", answer);
{
  "predictions": [
    12,
    9,
    15,
    12
  ]
}
Watch the parameter change

Your turn

The starter pipeline routes codes 1010 and 3030 to the wrong weights, predicting [15, 9, 12, 15] instead of [12, 9, 15, 12]. Set answer to the linear model predictions y^=Xw+10\hat{y} = X w + 10 for any 1D batch input of codes in {10,20,30}\{10, 20, 30\} when w=w = [2, -1, 5] was trained on pinned column order [10, 20, 30]; Check tests unseen sequences of varying lengths.

Starter prediction bars showing scrambled [15, 9, 12, 15] because categories were encoded in reversed order [30, 20, 10].
let vocab = torch.tensor("[30.0, 20.0, 10.0]", DType.F64);
let enc = input.unsqueeze(1).eq(vocab.unsqueeze(0)).to(DType.F64);
let w = torch.tensor("[2.0, -1.0, 5.0]", DType.F64).reshape(3, 1);
let answer = enc.matmul(w).reshape(input.size(0)) + 10.0;
out.set("predictions", answer);
{
  "predictions": [
    15,
    9,
    12,
    15
  ]
}

Encode 1D vector input against pinned vocabulary [10, 20, 30] and return predictions X * [2, -1, 5] + 10.

Read the result carefully

Notice why row 11 (code 2020) still predicted 99 even when swap_order =1= 1 reversed [10, 20, 30] into [30, 20, 10]. Reversing a three-element list leaves its center element fixed! On a spot check of row 11 alone, a broken deployment pipeline would look completely healthy while mispricing every code-1010 and code-3030 row by ∣5−2∣=3|5 - 2| = 3 points.

This silent failure explains why production feature stores serialize the exact ordered category array alongside the weight tensor. Column order is not cosmetic metadata; it is the coordinate system that binds each column of XX to its corresponding entry of ww.

Recap

  • Weight vectors index columns strictly by integer position, so [10, 20, 30] and [30, 20, 10] define distinct feature spaces.
  • Encoding [10, 20, 30, 10] under [10, 20, 30] with w=w = [2, -1, 5] and b=10b = 10 yields y^=\hat{y} = [12, 9, 15, 12].
  • Reversing the vocabulary to [30, 20, 10] silently swaps weights 22 and 55, turning predictions into [15, 9, 12, 15] without raising a shape error.