Sinusoidal Position Encoding › Worked Example: Encoding a Short Sentence
Language AI HandbookAll chapters Sinusoidal Position Encoding › Worked Example: Encoding a Short SentenceOne section · Sections in this chapter6 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's make the abstract concrete with a small, worked example. Consider a sentence with five tokens: "The cat sat on mat." We'll encode the positions 0 through 4 using a tiny encoding with dimensions, small enough to trace through by hand for the first few values.
With , we have dimension pairs, numbered . The frequency denominators for each pair are:
The wavelengths for each pair are:
- Pair 0: positions
- Pair 1: positions
- Pair 2: positions
- Pair 3: positions
For position 0 (the token "The"), every sine term evaluates to and every cosine term evaluates to , giving:
This is a special property of position 0 that makes it immediately recognizable: it is the only position where the odd dimensions (cosines) are all 1.
For position 1 (the token "cat"), we compute:
So .
The pattern is clear: in the first dimension pair (), there is already a large change from position 0 to position 1 (from to ), because the wavelength is only about 6. In the later dimension pairs ( and ), the change is tiny, because a single step is a tiny fraction of their 628- or 3532-position wavelengths.
Now consider position 4 (the token "mat") in the first dimension pair: and . This is very different from both position 0 and position 1, confirming that the high-frequency pair rapidly distinguishes even nearby positions. Meanwhile, the slow-frequency pairs at and have barely moved from their position-0 values, since 4 is much less than the wavelengths of 628 and 3532.
# Worked example: encode a 5-token sequence with d=8
d_example = 8
tokens = ["The", "cat", "sat", "on", "mat"]
PE_example = sinusoidal_position_encoding(len(tokens), d_example)Position encodings for 'The cat sat on mat' (d=8) ============================================================ Token Pos dim0 dim1 dim2 dim3 dim4 dim5 dim6 dim7 ------------------------------------------------------------ The 0 +0.000 +1.000 +0.000 +1.000 +0.000 +1.000 +0.000 +1.000 cat 1 +0.841 +0.540 +0.100 +0.995 +0.010 +1.000 +0.001 +1.000 sat 2 +0.909 -0.416 +0.199 +0.980 +0.020 +1.000 +0.002 +1.000 on 3 +0.141 -0.990 +0.296 +0.955 +0.030 +1.000 +0.003 +1.000 mat 4 -0.757 -0.654 +0.389 +0.921 +0.040 +0.999 +0.004 +1.000

This worked example makes the abstract formula tangible. You can trace exactly how each token's position influences its encoding, and you can see that the information is spread across all dimensions simultaneously rather than concentrated in any single one. In practice, these position encodings are added element-wise to the token embeddings, so the final input to the transformer is the sum of what the token means (its semantic embedding) and where it sits in the sequence (its positional encoding).