Transformers · Deep Learning
Attention Is All You Need — Google 2017 paper that replaced RNNs for language
Positional encoding is required because attention has no inherent sense of word order
1
No recurrence — the whole sequence processed in parallel
Unlike RNNs, which process tokens one at a time in sequence, Transformers process the entire sequence in parallel, using self-attention rather than a step-by-step recurrent mechanism.
Example: a Transformer processes all words of a sentence simultaneously, rather than one word at a time like an RNN would.
2
Self-attention — every token attends to every other
Every token in the sequence can directly attend to every other token, computing relevance scores that determine how much each token's information contributes to understanding the others.
Example: the word "it" can directly attend to an earlier noun in the sentence to figure out what "it" refers to, without needing to pass through every word in between sequentially.
3
Positional encoding — injecting word order information
Because self-attention has no built-in sense of sequence order (it treats the input more like an unordered set of tokens attending to each other), positional encoding using sine/cosine functions is added to inject information about each token's position in the sequence.
Example: without positional encoding, "dog bites man" and "man bites dog" would be processed identically by self-attention, since it has no inherent notion of word order.
1
A Transformer needs to correctly distinguish between the sentences "dog bites man" and "man bites dog," which contain the exact same words in different order.
2
Ask: does self-attention alone have any built-in way to distinguish these two orderings? No — self-attention has no inherent sense of word order.
3
Positional encoding is added specifically to inject this missing order information, using sine/cosine functions that encode each token's position in the sequence.
4
With positional encoding included, the Transformer can now correctly distinguish these two sentences, despite self-attention itself having no inherent notion of sequence order.

Exams test whether you understand WHY positional encoding is necessary — specifically because self-attention itself has no inherent sense of order, unlike RNNs which process tokens sequentially and inherently encode order through that sequential processing.

The most common trap is assuming Transformers inherently understand word order just because they process language well overall. Self-attention by itself is fundamentally order-agnostic; positional encoding is a separate, necessary addition specifically to inject that missing sequence-order information.

1. Do Transformers process sequences all at once, or one token at a time like RNNs?
All at once, in parallel, using self-attention rather than sequential recurrence.
Tap to reveal / hide
2. What does self-attention allow every token to do?
Directly attend to every other token in the sequence, computing relevance scores between them.
Tap to reveal / hide
3. Why is positional encoding necessary in a Transformer?
Because self-attention itself has no inherent sense of word order, so position information must be explicitly injected.
Tap to reveal / hide
4. What functions are typically used to generate positional encodings?
Sine and cosine functions.
Tap to reveal / hide
5. What famous paper introduced the Transformer architecture, and in what year?
"Attention Is All You Need," published by Google in 2017.
Tap to reveal / hide