DeepLLM Quiz
Neural Network Architectures

Positional encoding: how a transformer knows the order of words

10 questions on permutation equivariance, adding position vectors, bounded and unique encodings, sine and cosine encodings, relative offsets and learned positions.

  • 10 questions
  • Stopwatch
  • Instant feedback

Shown publicly with your score. Don’t use your full real name.

Sample questions

Play now ↑

Answers stay hidden. Start the quiz to play them.

  1. Sample 1

    Why are position vectors added to token vectors, not concatenated with them?

    • Addition is the only operation that works with vectors
    • Concatenation would lose the token
    • Concatenation would enlarge all subsequent vectors and raise the cost of every later attention calculation

How it works

  • One question per screen. An answer is final once sent.
  • You see right away whether you were right, with a short explanation.
  • The stopwatch runs: on equal scores, the faster player ranks first.
  • Only your best attempt counts, so replaying is never risky.

About this quiz

"The dog chased the cat" and "The cat chased the dog" contain exactly the same words and mean opposite things. A reader notices the difference at once, because words come in an order and order carries meaning. A transformer, as described so far, does not notice. Its layers treat the tokens as a set: shuffle the rows of the input table and the rows of the output are shuffled in just the same way, with nothing else changing. That is good for parallel computation and for learning long-range relations, but it is a serious problem for any task in which order matters, which includes nearly every task involving language. This quiz is about how the problem is solved.

The solution is to put the order into the data, so that the architecture can stay as it is. Each position gets its own vector, and that vector is added to the embedding of the token in that position. You will see why adding, rather than gluing the two vectors together, is the sensible choice, and why mixing position information into the token vectors does not wreck them: vectors that are unrelated tend to be nearly orthogonal in spaces of high dimension, so the network can separate the two kinds of information, and the shortcuts in each layer carry the position signal forward.

The quiz then asks what makes a good position vector. Counting positions with whole numbers produces values that grow without bound. Squeezing positions into a fixed range depends on the length of the sequence. A good encoding is unique for each position, bounded, able to handle sequences longer than those seen in training, and able to express the distance between two positions in a way that does not depend on where they are. You will see how a family of sines and cosines of increasing wavelength meets these requirements, like the digits of a counter that tick at different speeds, and why it needs both sines and cosines. Finally, you will compare it with learned position vectors.

Common misunderstandings include thinking that attention itself knows the order, believing that added position vectors confuse the model, and assuming that learned positions work for any length. They do not extend beyond the lengths seen in training.

The quiz suits learners who have met self-attention and wonder how it copes with sequences, and practitioners adapting models to new lengths. It needs no mathematics beyond following simple arguments. There are 10 questions, each with an explanation, and it takes about four minutes. The idea of putting order into the data, not into the architecture, is elegant and has proved very durable. It keeps the attention mechanism simple and highly parallel, and it moves the question of order into a place where it can be studied and improved on its own. Many later schemes, from encodings that express relative distance directly to ones that stretch to much longer texts, are variations on the themes in this quiz, so a firm grasp of the basic requirements makes them easy to follow.

Questions and answers

›How long does the quiz take?

About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows every answer, so the reasoning behind the design is spelled out.

›Do I need to know how attention works?

A general idea is enough, namely that each output token is a weighted blend of all the input tokens. The quizzes on attention basics and on transformer layers explain it more fully, but the questions here recall what is needed.

›Why does a transformer not know the order of tokens?

The same query, key and value matrices, and the same per-token network, are applied to every token. As a result, permuting the order of the input tokens simply permutes the output in the same way, so the transformer is equivariant with respect to the order. Sharing parameters allows massively parallel processing, but it leaves the architecture blind to position.

›Why does order matter so much?

In language, sentences made of the same words in different orders can have very different meanings, so a representation that ignores order is useless for most tasks. The same holds for sequences of notes, measurements or amino acids, where the position of each element relative to others is part of the information.

›How is position information added?

A position-encoding vector is constructed for each position and added to the embedding of the token in that position. This needs the two vectors to have the same size. Concatenating them instead would enlarge every later vector and so increase the cost of all subsequent attention calculations.

›Does adding position vectors corrupt the token information?

Not in practice. Two unrelated vectors in a high-dimensional space are almost orthogonal, so the network can treat the identity of the token and its position largely separately. Because of the residual connections, the position information is also carried from one layer to the next instead of being lost.

›What makes a good positional encoding?

It should be unique for each position and bounded in size. It should generalise to longer sequences than any seen in training. And it should have a consistent way of expressing the number of steps between any two positions, whatever their absolute positions, because relative position is often more important than absolute position.

›How does the sine and cosine encoding work?

Each element of the position vector is a sine or a cosine of the position, with wavelengths that grow steadily across the elements. The first elements oscillate quickly and the later ones slowly, like the digits of a binary counter. All values lie between minus one and one, and the encoding of a position a fixed number of steps later is a linear combination of the original one, which lets the network attend to relative positions.

›What are learned positional encodings, and what is their limit?

A vector of weights is assigned to each position and learned along with the rest of the model. This avoids hand-crafted formulas, but positions never seen in training get vectors that were never trained, so the method does not extend to longer inputs. It suits cases where the input length stays roughly constant.

More quizzes