Formula Bro

Math and physics formulas, explained like you are new.


Positional Encoding

In one sentence: Positional encoding gives each word a pattern of waves that says where it sits in the sentence, so a model that reads all the words at once still knows their order.

PE(pos,2i)=sin⁡(pos100002i/d)PE(pos,2i+1)=cos⁡(pos100002i/d)\text{PE}_{(pos,\,2i)} = \sin\left(\frac{pos}{10000^{2i/d}}\right) \qquad \text{PE}_{(pos,\,2i+1)} = \cos\left(\frac{pos}{10000^{2i/d}}\right)

Read it out loud: “PE at position pos, index 2i, equals the sine of pos divided by 10000 raised to the power 2i over d. PE at index 2i plus 1 equals the cosine of the same angle. PE is the positional encoding. The letter pos is the position of the word, counting from 0. The letter i counts pairs of numbers, starting from 0, and the letter d is the size of each list of numbers. The sine and cosine are wave functions, and 10000 is a fixed number from the original paper.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Vectors and embeddings

A vector is an ordered list of numbers, treated as one object. A word embedding is a vector that stands for a word’s meaning, with dd numbers in it. The position encoding is also a list of dd numbers, so the two lists have the same length and can be added together number by number.

Sine and cosine

Sine, written sin\sin, and cosine, written cos\cos, are wave functions. Give them an angle and they return a number between −1-1 and 11. As the angle grows, they rise and fall smoothly, and together they trace a circle. For example, sin⁡0=0\sin 0 = 0 and cos⁡0=1\cos 0 = 1.

Radians and pi

An angle in this formula is measured in radians, not degrees. A full turn is 2π≈6.2832\pi \approx 6.283 radians, so one radian is about 57.3 degrees. The Greek letter π\pi, called pi, is about 3.14159. The angle 2 radians is about 115 degrees, and 0.02 radians is about 1.1 degrees.

Powers, like 100002i/d10000^{2i/d}

A power says how many times to multiply a number by itself, and a fraction in the power can mean a root. Any number to the power 0 is 1, so 100000=110000^{0} = 1. The power 100001/210000^{1/2} is the square root of 10000, which is 100, because 100×100=10000100 \times 100 = 10000. In the formula the power 2id\frac{2i}{d} is a fraction, so the divisor grows as the pair index grows.

Frequency of a wave

A wave repeats over and over. Its frequency is how often it repeats, and a fast wave completes many repeats in a short span. In this formula each pair of numbers is one wave. Pairs with a small index repeat quickly, and pairs with a large index repeat slowly, so together the pairs describe position at many different scales.

Transformer and attention

A transformer is a neural network design for sequences such as sentences. Attention lets each word look at the other words and decide how much each one matters. A transformer reads all the words at the same time, rather than one after another, so it needs position information to know the order.

Pairs of places and the index ii

The list of dd numbers is split into pairs. Places 0 and 1 form the first pair, places 2 and 3 form the second pair, and so on. The index ii counts the pairs, starting at 0. The pair with index ii uses the places 2i2i and 2i+12i+1. So with d=4d = 4 there are two pairs, with i=0i = 0 and i=1i = 1.

What each symbol means

SymbolMeaning

PE(pos,2i)\text{PE}_{(pos,\,2i)}

The encoding value at position pospos in an even-numbered place, 2i2i. It is a plain number with no unit. It always lies between −1-1 and 11, because it comes from a sine, and it is added to the word embedding.

PE(pos,2i+1)\text{PE}_{(pos,\,2i+1)}

The encoding value at the same position in the next, odd-numbered place, 2i+12i+1. It comes from a cosine, so it also lies between −1-1 and 11. Each even and odd pair shares the same frequency.

pospos

The position of the word in the sentence, counting from 0 for the first word. So the second word has pos=1pos = 1. It is a count with no unit.

ii

The pair index, counting from 0 up to d2−1\frac{d}{2} - 1. A small ii gives a fast wave, and a large ii gives a slow wave. It has no unit.

dd

The size of the encoding: the number of places in each list, which equals the size of the word embedding. It must be even, so that the numbers come in pairs. The original paper used d=512d = 512.

1000010000

A fixed constant from the original paper. It sets how slow the slowest wave is: the wavelengths run from 2π2\pi up to about 10000×2π10000 \times 2\pi. It has no unit.

sin\sin

The sine function, a wave that turns an angle into a number from −1-1 to 11. Its input is an angle measured in radians.

cos\cos

The cosine function, a wave like the sine but shifted by a quarter turn. It also gives numbers from −1-1 to 11, and its input is the same angle in radians.

The formula piece by piece

pospos

The position of the word, counting from 0. It is the only thing that changes from one word to the next, so it decides which wave values each word receives.

2id\frac{2i}{d}

A fraction that describes which pair we are using, and it lies between 0 and 1. For d=4d = 4, pair i=0i = 0 gives 04=0\frac{0}{4} = 0, and pair i=1i = 1 gives 24=0.5\frac{2}{4} = 0.5. It sets how slow the pair’s wave is.

100002i/d10000^{2i/d}

The divisor for this pair, a power of 10000. With exponent 0 the divisor is 1, and with exponent 0.5 it is 100. A bigger divisor means the position changes the angle more slowly, so the wave is slower.

pos100002i/d\frac{pos}{10000^{2i/d}}

The angle, in radians, for this word and this pair. The position is divided by the divisor, so a bigger position gives a bigger angle, and a bigger divisor gives a smaller angle.

sin⁡(pos100002i/d)\sin\left(\frac{pos}{10000^{2i/d}}\right)

The sine of that angle. It is the value for the even place, 2i2i. Because sine stays between −1-1 and 11, the value never grows, however large the position becomes.

PE(pos,2i)=sin⁡(pos100002i/d)PE(pos,2i+1)=cos⁡(pos100002i/d)\text{PE}_{(pos,\,2i)} = \sin\left(\frac{pos}{10000^{2i/d}}\right) \qquad \text{PE}_{(pos,\,2i+1)} = \cos\left(\frac{pos}{10000^{2i/d}}\right)

Each word’s position code has dd numbers. In each pair, the even place holds the sine of the angle, and the odd place holds the cosine of the same angle.

What the formula is doing, and why

A transformer reads all the words at the same time. Attention on its own does not care about order, so dog bites man and man bites dog would look like the same set of vectors. Positional encoding fixes this by adding a position signal to each word’s vector, so the same word in a different place gets a different vector.

The input is a position pospos and the size dd. The output is a list of dd numbers for that position. The numbers come in pairs. The places 2i2i and 2i+12i+1 share one frequency, set by the divisor 100002i/d10000^{2i/d}. The first number of each pair is a sine, and the second is a cosine, of the same angle. So each pair moves around a circle as the position grows.

Why many frequencies? The frequencies form a series that runs from fast to slow. Pairs with a small ii change quickly from one position to the next, which tells neighboring words apart. Pairs with a large ii change slowly, which keeps track of position across long distances. Read all the pairs together, and each position has its own pattern of numbers.

Why sine and cosine, and why add? A sine alone repeats, so two different angles can give the same value. Pairing it with a cosine places each pair on a circle, so the angle can be read without confusion. The original paper adds the encoding to the embedding instead of appending it, so the vector keeps its size dd, which the rest of the model expects. The authors also hoped the pattern would help the model handle sentences longer than those it saw in training. That was a hope, not a proven result.

When it applies. Position codes like this one are needed by any transformer that reads all positions at once. Other models give order in different ways: some learn their position vectors during training, and others build position into the attention step itself. The formula gives a value for any position, but very long inputs can still cause trouble in practice.

An everyday picture

Think of a clock with several hands that turn at different speeds. The fastest hand tells you small differences in time, and the slowest hand tells you roughly where you are in a long day. Read all the hands together and the time is unique. Positional encoding works the same way: the fast waves tell neighboring positions apart, and the slow waves place words far down the sentence.

Worked example: Encoding position 2 in a four-number vector

Use d=4d = 4, so each word gets four numbers in two pairs. Find the encoding for position pos=2pos = 2, which is the third word, because positions start counting at 0.

  1. Find each pair’s divisor. For pair i=0i = 0, the power is 2×04=0\frac{2 \times 0}{4} = 0, and 100000=110000^{0} = 1. For pair i=1i = 1, the power is 2×14=0.5\frac{2 \times 1}{4} = 0.5, and 100000.510000^{0.5} is the square root of 10000, which is 100100, because 100×100=10000100 \times 100 = 10000.
  2. Work out the first pair. The angle is 21=2\frac{2}{1} = 2 radians, about 115 degrees. Its sine is PE(2,0)=sin⁡(2)≈0.9093\text{PE}_{(2,0)} = \sin(2) \approx 0.9093 for the even place. Its cosine is PE(2,1)=cos⁡(2)≈−0.4161\text{PE}_{(2,1)} = \cos(2) \approx -0.4161 for the odd place.
  3. Work out the second pair. The angle is 2100=0.02\frac{2}{100} = 0.02 radians, only about 1.1 degrees. Its sine is PE(2,2)=sin⁡(0.02)≈0.0200\text{PE}_{(2,2)} = \sin(0.02) \approx 0.0200, and its cosine is PE(2,3)=cos⁡(0.02)≈0.9998\text{PE}_{(2,3)} = \cos(0.02) \approx 0.9998. The slow pair has barely moved away from its starting value, which is what a long-range signal looks like.
  4. Stack the four values. The encoding for position 2 is (0.9093,−0.4161,0.0200,0.9998)(0.9093,\ -0.4161,\ 0.0200,\ 0.9998). This list is added, number by number, to the embedding of the third word in the sentence, so the word’s vector carries its position.
  5. Compare with the next position. At position pos=3pos = 3, the first pair’s angle is 3 radians, which gives sin⁡3≈0.1411\sin 3 \approx 0.1411 and cos⁡3≈−0.9900\cos 3 \approx -0.9900. The fast pair has changed a lot. The slow pair only moves to about 0.03000.0300 and 0.99960.9996. Comparing the two positions shows how neighboring words are told apart.

Answer: The position vector for position 2 is (0.9093, -0.4161, 0.0200, 0.9998). The first pair changes quickly from one position to the next, so it separates neighboring words. The second pair changes slowly, so it carries a longer-range signal.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why use sine and cosine instead of plain numbers?

Sine and cosine always stay between −1-1 and 11, so the values never grow with the position. Mixing many frequencies also gives each position its own pattern.

Why does each pair have both a sine and a cosine?

A sine alone repeats, so two different angles can give the same value. Pairing it with a cosine places each pair on a circle, so the angle, and therefore the position, can be read without confusion.

Is the encoding learned during training?

Not in the original paper. The formula is fixed. Other models learn their position vectors as part of training.

Does the encoding change the meaning of a word?

It shifts the word’s vector by a position-dependent amount. The model learns to use both the meaning part and the position part when it pays attention to other words.

Sources

  1. Vaswani et al., Attention Is All You Need (arXiv:1706.03762)
  2. Transformer (deep learning), Wikipedia

| All AI & ML formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: ai-ml/positional-encoding.md.