CSCE 471 Natural Language Processing · Spring 2027

Transformers: Intuition

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

What does bank mean? In “I deposited money at the bank” it's a financial institution. In “The dog ran to the bank of the river” it's the side of a river. In “The plane began to bank” it's a verb: to tilt while turning. The word is the same; only the context differs. To understand a word, you have to read the words around it.

The Transformer is the architecture behind nearly every modern language model, and its central idea mirrors exactly this: each word updates its own representation by looking at all the other words in the sentence and deciding how much each one matters. This reading builds that idea intuitively, without the full machinery, and explains why it works better than reading a sentence one word at a time.

After reading this, you should be able to explain the two main weaknesses of recurrent neural networks · explain why word meaning depends on context · describe how a word's vector is updated as an attention-weighted sum of all word vectors · explain why all words can be updated in parallel · explain how this design addresses long-distance information and parallelization.

1Two problems with reading one word at a time

A recurrent neural network (RNN) reads a sentence word by word, keeping a hidden state $h_t$ that it updates at every step: $h_t$ depends on the current word and on $h_{t-1}$. This design has two well-known problems:

In 2017, Vaswani et al. proposed a model that drops recurrence entirely and relies on attention alone, in a paper titled “Attention Is All You Need.” They called the model the Transformer.

2How do humans decide what a word means?

Read “This is the bank of the river.” When you reach bank, your initial understanding holds several possible senses at once. Then you use the context: the word river strongly suggests a riverbank, so your refined understanding settles on that sense.

Notice two things about this process:

3How a Transformer mimics this

Each word starts with an initial word vector $\mathbf x_1,\dots,\mathbf x_n$, one per word. To update the vector of one word, the Transformer assigns an attention weight $\alpha_i$ to every word $i$ in the sentence, including the word itself, and takes the weighted sum:

$$\text{updated vector}=\sum_{i=1}^{n}\alpha_i\,\mathbf x_i,\qquad \alpha_i\ge0,\quad \sum_{i=1}^{n}\alpha_i=1.$$

Because the weights are non-negative and sum to 1, this is a weighted average of the word vectors. A word with a large weight pulls the updated vector toward itself; a word with weight near 0 barely affects it. The updated vector is a contextualized representation: it reflects both the word and its context.

To see this concretely, imagine word vectors with just two dimensions: one for the “finance” sense and one for the “river” sense. On its own, bank sits in the middle, because it could mean either.

Attention weights for updating bank (drag to change; they are renormalized to sum to 1)
Figure 1 (interactive). Updating the vector of bank as a weighted average of all word vectors (toy 2-dimensional vectors made up for this page). The grey point is the initial vector of bank; the red point is the updated vector. Give river or money a large weight and the updated vector moves toward that sense.

Where do the weights come from? In a real Transformer they are not set by hand: they are computed from the word vectors themselves, so that related words automatically receive large weights. Exactly how is a story of its own; for now, the key idea is the weighted average.

4Every word, all at once

The same update is applied to every word in the sentence, each with its own set of attention weights. Arrange the weights in a table, with one row per word being updated and one column per word being attended to:

Figure 2 (interactive). Illustrative attention weights for This is the bank of the river. Each row sums to 1 and is one word's weighted average. Click a word on the left to see which words it focuses on.

Crucially, no row depends on the result of another row: every updated vector uses only the initial vectors. So all the words can be updated at the same time, in parallel, which is exactly what GPUs are good at.

And the process can be repeated. The updated vectors become the input to another round of the same kind of update, producing vectors that are refined again. Stacking many such layers lets the model build up increasingly rich, context-aware representations.

5Fixing the two problems

Figure 3 (interactive). Press Play to see how many sequential steps each model needs to process the sentence. The diagram shows how information travels between two words: through every word in between (RNN) or along one direct connection (Transformer). Click two words to measure the distance between them.
RNNTransformer
Steps for information to travel between words $i$ and $j$$|i-j|$1 (direct connection)
Sequential steps to process $n$ words (per layer)$n$1 (all words in parallel)
Table 1. How the Transformer addresses the two problems of RNNs.
Two questions this leaves open. First, how are the attention weights $\alpha_i$ computed from the word vectors? Second, a weighted average doesn't care about the order of the words: shuffle the sentence and each word sees the same set of vectors. Since word order clearly matters in language, the full Transformer needs a way to put position information back in. Both are part of the complete architecture.

6Check your understanding

Exercise 1: a weighted average

Three words have 2-dimensional vectors $\mathbf x_1=[1,0]$, $\mathbf x_2=[0,1]$, $\mathbf x_3=[1,1]$. With attention weights $\alpha=(0.2,\,0.5,\,0.3)$, compute the updated vector.

$0.2[1,0]+0.5[0,1]+0.3[1,1]=[0.2+0.3,\;0.5+0.3]=[0.5,\,0.8]$.

Exercise 2: steps

In a 100-word sentence, how many sequential steps does an RNN need to carry information from word 3 to word 90? How many does a single Transformer layer need? How many sequential steps does each need to process all 100 words in one layer?

RNN: $90-3=87$ steps. Transformer: 1 step, since word 90 attends to word 3 directly. To process all words, an RNN needs 100 sequential steps; a Transformer layer updates all 100 words at once.

Exercise 3: word order

Using only the weighted-average update (with weights that depend only on which words are present), would “dog bites man” and “man bites dog” give the word bites the same updated vector? Why is this a problem?

Yes: bites sees the same three vectors in both sentences, so the weighted average is identical, even though the meanings are opposite. This is why the full Transformer adds information about each word's position.

Quick quiz

7Further reading