Lensa ML

How a transformer knows word order, in plain words

A transformer, the model design behind large language models, reads all the words of a sentence at once. Its attention step, where each word gathers context from the others, compares words only by meaning, so it can't tell "dog bites man" from "man bites dog". Positional encoding fixes this by adding a position tag to each word.

2 min read · Lensa ML
A heatmap of position tags, 50 positions down and 20 dimensions across, colored red for +1, blue for -1 and pale gray for 0. The left columns flip color every few rows. The right columns form wide stripes, alternating pale gray and red. Position 10 and dimension 4 are outlined, and below, the wave for dimension 4 peaks at 1.00 at position 10.
  1. Each word enters as an embedding, a list of numbers that stands for it, and its tag is a list of the same length added to it. In the heatmap each row is the tag for one position, 0 to 49 from the top, and each column is one dimension, a slot in the list. Red means +1, blue means −1 and pale gray means 0.
  2. Each column follows one wave: PE(pos, 2i) = sin(pos / 10000^(2i/d)) for the even columns, with cos in place of sin for column 2i + 1. Here pos is the position, 2i the column number, d = 20 the number of dimensions, and ^ means "to the power of". The curve below the heatmap is column 4, sin(pos / 6.31), which peaks at 1.000 at position 10. Columns 0 and 1 repeat every 6.3 positions, so their colors flip about every three rows. Each pair of columns to the right is about 2.5 times slower.
  3. A row's colors are that position's fingerprint. One fast wave is not enough: in columns 0 and 1, positions 2 and 46 read 0.91 and −0.42 against 0.90 and −0.43. Column 2 tells them apart, 0.71 against −0.51, like an hour hand beside a minute hand. The slowest waves span thousands of positions, so over these 50 rows sine stays near 0 (pale gray) and cosine near 1 (red), which draws the wide stripes on the right. In long documents they separate distant positions.

Takeaway

The tag is fixed, with nothing to learn, and the original Transformer used 512 dimensions. BERT and GPT-2 learn a table of tags instead, with 512 and 1,024 rows. Many newer language models use rotary position embedding (RoPE), which applies similar waves inside the attention step.

Go deeper · interactive

Positional Encoding

On the Sinusoidal Encoding step, drag the Position and Dimension sliders and watch the outlined row and the wave below change.

Open the explainer

More bites

References