One Row Becomes One-Hot
Map one discrete category code against a four-slot vocabulary into a binary indicator vector with a single active 1.
The problem
Feeding raw category codes such as [30, 10, 40, 20] into a linear layer forces a single weight to treat category as four times category and category as three-quarters of category . When codes name unordered groups — shipping carriers, blood types, or device families — that arithmetic order is fictional.
One-hot encoding replaces one scalar code with one binary slot per pinned category in [10, 20, 30, 40]. Given input and selected row_index , which slot lights up as and which slots stay at ?
Work through the numbers
Inspect row_index of [30, 10, 40, 20]: its category code is . Compare for exact equality against every slot of the pinned vocabulary [10, 20, 30, 40] in left-to-right order.
Slot checks (). Slot checks (). Slot checks (). Slot checks (). Collecting all four slots produces [0, 0, 1, 0], whose entries sum to .
Check the other three rows against [10, 20, 30, 40]. Row (code ) becomes [1, 0, 0, 0]. Row (code ) becomes [0, 0, 0, 1]. Row (code ) becomes [0, 1, 0, 0]. Every known category lands on its own orthogonal basis vector .
The live model
Four slot blocks represent vocabulary entries [10, 20, 30, 40], lighting the active slot in green and keeping inactive slots muted. At default row_index (code ), slot lights up for [0, 0, 1, 0]. Dragging row_index to (code ) moves the active block to slot ([1, 0, 0, 0]); at row_index (code ) slot lights up ([0, 0, 0, 1]); at row_index (code ) slot lights up ([0, 1, 0, 0]).
![Four binary indicator blocks for vocabulary [10, 20, 30, 40] showing [0, 0, 1, 0] with slot 30 active green.](../assets/ml/one-row-becomes-one-hot--demo.png)
let row_index = scene.parameter("row_index", 0.0, 0.0, 3.0, 1.0);
let val = input.select(0, int(row_index));
let vocab = torch.tensor("[10.0, 20.0, 30.0, 40.0]", DType.F64);
let answer = val.eq(vocab).to(DType.F64);
out.set("one_hot_row", answer);
{
"one_hot_row": [
0,
0,
1,
0
]
}Watch the parameter change
Your turn
The starter program produces the cumulative vector [1, 1, 1, 0] (sum ) on code instead of a one-hot vector with a single active slot ([0, 0, 1, 0]). Set answer to the four-element one-hot indicator vector for input at row_index against pinned vocabulary [10, 20, 30, 40]; Check varies both the category vector and row_index.
![Starter blocks lighting three slots [1, 1, 1, 0] via greater-or-equal instead of one-hot [0, 0, 1, 0].](../assets/ml/one-row-becomes-one-hot--exercise.png)
let row_index = scene.parameter("row_index", 0.0, 0.0, 3.0, 1.0);
let val = input.select(0, int(row_index));
let vocab = torch.tensor("[10.0, 20.0, 30.0, 40.0]", DType.F64);
let answer = (val - vocab).ge(0.0).to(DType.F64);
out.set("one_hot_row", answer);
{
"one_hot_row": [
1,
1,
1,
0
]
}Select element int(row_index) from input and return its 4-element one-hot vector against pinned vocabulary [10, 20, 30, 40].
Read the result carefully
Look at the Euclidean geometry of the four one-hot vectors [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], and [0, 0, 0, 1]. Any two distinct categories differ by in two coordinates, so every distinct pair has identical squared Euclidean distance . Unlike raw codes (whose squared distances range from to ), one-hot encoding places every distinct category equidistant from every other category.
Equally important, taking a dot product between a weight vector and [0, 0, 1, 0] plucks out alone, giving each category an independent learned parameter.
Recap
- One-hot encoding compares a single categorical value for exact equality against each slot of a pinned -item vocabulary.
- For known categories in
[10, 20, 30, 40], row (code ) maps to[0, 0, 1, 0], with and three zeros. - Orthogonal indicator vectors make all distinct category pairs equidistant () and grant each category its own weight.