Speed

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 y^=w⋅code+b\hat{y} = w \cdot \mathrm{code} + b forces a single weight ww to treat category 4040 as four times category 1010 and category 3030 as three-quarters of category 4040. 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 {0,1}\{0, 1\} slot per pinned category in [10, 20, 30, 40]. Given input =[30,10,40,20]= [30, 10, 40, 20] and selected row_index =0= 0, which slot lights up as 11 and which slots stay at 00?

Work through the numbers

Inspect row_index =0= 0 of [30, 10, 40, 20]: its category code is 3030. Compare 3030 for exact equality against every slot of the pinned vocabulary [10, 20, 30, 40] in left-to-right order.

Slot 00 checks 30=1030 = 10 (00). Slot 11 checks 30=2030 = 20 (00). Slot 22 checks 30=3030 = 30 (11). Slot 33 checks 30=4030 = 40 (00). Collecting all four slots produces [0, 0, 1, 0], whose entries sum to 0+0+1+0=10 + 0 + 1 + 0 = 1.

Check the other three rows against [10, 20, 30, 40]. Row 11 (code 1010) becomes [1, 0, 0, 0]. Row 22 (code 4040) becomes [0, 0, 0, 1]. Row 33 (code 2020) becomes [0, 1, 0, 0]. Every known category lands on its own orthogonal basis vector ek∈{0,1}4e_k \in \{0, 1\}^4.

The live model

Four slot blocks represent vocabulary entries [10, 20, 30, 40], lighting the active 11 slot in green and keeping inactive 00 slots muted. At default row_index =0= 0 (code 3030), slot 22 lights up for [0, 0, 1, 0]. Dragging row_index to 11 (code 1010) moves the active block to slot 00 ([1, 0, 0, 0]); at row_index =2= 2 (code 4040) slot 33 lights up ([0, 0, 0, 1]); at row_index =3= 3 (code 2020) slot 11 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.
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 33) on code 3030 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].
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 11 in two coordinates, so every distinct pair has identical squared Euclidean distance 12+12=21^2 + 1^2 = 2. Unlike raw codes 10,20,30,4010, 20, 30, 40 (whose squared distances (ca−cb)2(c_a - c_b)^2 range from 100100 to 900900), one-hot encoding places every distinct category equidistant from every other category.

Equally important, taking a dot product wTe2w^T e_2 between a weight vector [w0,w1,w2,w3][w_0, w_1, w_2, w_3] and [0, 0, 1, 0] plucks out w2w_2 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 KK-item vocabulary.
  • For known categories in [10, 20, 30, 40], row 00 (code 3030) maps to [0, 0, 1, 0], with ∑kek=1\sum_k e_k = 1 and three zeros.
  • Orthogonal indicator vectors make all distinct category pairs equidistant (12+12=21^2 + 1^2 = 2) and grant each category its own weight.