← Home

How GPT Actually Works: A Visual Guide to Transformers

How does a GPT actually work? A visual walkthrough of transformers: tokens, word embeddings, dot products, softmax and temperature, with real GPT-3 numbers.

View as Markdown

I used GPT models for months before I could picture one. I knew the words (tokens, embeddings, attention, softmax), but I could not have drawn what happens between typing a prompt and getting a word back.

What fixed that for me was 3Blue1Brown’s But what is a GPT?. This post is the explanation I wish I’d had: one continuous story from raw text to a sampled next token, with every calculation step on its own line and the real GPT-3 numbers attached wherever the full model is too big to write out.

What does a GPT actually do?

GPT = Generative Pre-trained Transformer. Generative: it writes text. Pre-trained: it learned from a huge pile of data first. Transformer: the architecture from the 2017 paper Attention Is All You Need.

The loop underneath is simple. Given some text, the model outputs a probability distribution over the next token. Pick one token from that distribution, append it, and run again:

flowchart LR
    A[Input text] --> B[Model predicts<br/>distribution over<br/>next token]
    B --> C[Sample one token<br/>from the distribution]
    C --> D[Append token<br/>to the text]
    D --> A

That predict → sample → append loop is what you watch when ChatGPT types one word at a time. The rest of this post is the maths inside the “predict” box. I walk it in order: tokens → embeddings → dot products → softmax → temperature, then tie it together with one complete numeric example.

How does text become tokens?

Before any number touches the transformer, the string is split into tokens, which are sometimes whole words and often sub-word pieces. Take the sentence from the video:

To date, the cleverest thinker of all time was …

A tokenizer might split it like this:

**To date , the cle ve rest thinker of all time was …**

“cleverest” becomes cle | ve | rest. The model never sees letters, only integer token IDs. In GPT-3 the vocabulary has exactly:

$$ V = 50{,}257 \text{ possible tokens} $$

The worked example below uses word-sized tokens so the arithmetic stays readable, but the real unit is always the token ID.

How do token IDs become vectors?

Each token ID is mapped to a vector, a long list of numbers, by looking up a column in the embedding matrix $W_E$.

In GPT-3:

$$ \text{embedding dimension } d = 12{,}288 $$
$$ W_E \in \mathbb{R}^{12{,}288 \times 50{,}257} $$

Each column is one token’s vector. “Embedding token $t$” means: grab column $t$ of $W_E$. The values start random and get learned during training. That one matrix alone holds:

$$ 12{,}288 \times 50{,}257 = 617{,}558{,}016 \text{ weights} $$

That is 617 million parameters before a single attention layer, in a model with about 175 billion. I kept a running tally while watching the video, and that is what made the scale concrete for me. I put that count next to two other sizes.

Table of embedding matrix sizes. GPT-3, with d 12,288 and V 50,257, has 617,558,016 weights, 0.35% of 175B. Half the d gives 308,779,008. Double V gives 1,235,116,032.
$W_E$ holds 617,558,016 weights. Halving $d$ halves the count, and doubling $V$ doubles it.

What does training do to those vectors?

After training, directions in embedding space carry meaning. Nobody programmed this; gradient descent found it useful.

  • Gender lives in a direction. $\text{woman} - \text{man}$ is roughly $\text{queen} - \text{king}$, so $\text{king} + (\text{woman} - \text{man})$ lands near queen.
  • Nationality generalizes. $\text{Italy} - \text{Germany} + \text{Hitler}$ lands near Mussolini.
  • Plurality is a direction. $\text{cats} - \text{cat}$ points along “more than one.”

A raw embedding carries no context: the lookup for mole is identical whether you mean the animal, the skin mark, or the chemistry unit. The transformer’s middle layers fix that by letting each vector absorb context from its neighbors. GPT-3 can hold 2,048 tokens at once, and after 96 attention + MLP blocks the last token’s vector has absorbed information from everything before it. I save how that mixing works for the attention deep dive, but the tool it uses everywhere is the dot product.

Why are dot products the similarity score?

To ask “how aligned are two vectors?” you take the dot product:

$$ \vec{a} \cdot \vec{b} = \sum_i a_i b_i $$

Positive → they point the same way (related). Zero → perpendicular (unrelated). Negative → opposite.

Here is a 2-D example. Let:

$$ \vec{a} = \begin{bmatrix} 3 \\ 4 \end{bmatrix}, \quad \vec{b} = \begin{bmatrix} 1 \\ 2 \end{bmatrix} $$

Multiply coordinate by coordinate:

$$ a_1 b_1 = 3 \times 1 = 3 $$
$$ a_2 b_2 = 4 \times 2 = 8 $$

Sum:

$$ \vec{a} \cdot \vec{b} = 3 + 8 = 11 $$

I tried two values of $\vec{b}$. The left one is the example I just worked.

Arrows a equals (3, 4) and b equals (1, 2) pointing the same way. The dot product is 3 times 1 plus 4 times 2, which is 11.

$b = (1, 2)$: the arrows agree, and the dot product is 11.

Arrows a equals (3, 4) and b equals (-4, 3) at right angles. The dot product is 3 times -4 plus 4 times 3, which is 0.

$b = (-4, 3)$: the arrows are perpendicular, and the dot product is 0.

In GPT-3’s embedding space each vector has 12,288 coordinates, so the sum has 12,288 terms: the same idea, just longer. Attention uses dot products to score how much token $j$ should listen to token $i$, and I unpack that in how attention works in transformers. For predicting the next token, the model takes the last context-rich vector and runs one more matrix multiply.

How does the model score every possible next token?

After all the blocks, take the final vector $\vec{h}$ for the last token — shape $12{,}288 \times 1$ in GPT-3. Multiply by the unembedding matrix $W_U$:

$$ W_U \in \mathbb{R}^{50{,}257 \times 12{,}288} $$
$$ \vec{z} = W_U \, \vec{h} $$

Each entry of $\vec{z}$ is a raw score, called a logit, for one vocabulary token. You get 50,257 of them. $W_U$ has the same shape as $W_E$ transposed, so it adds another:

$$ 50{,}257 \times 12{,}288 = 617{,}558{,}016 \text{ weights} $$

The two ends together hold just over 1 billion parameters; the other ~174 billion sit in the attention and MLP blocks in the middle.

Why only the last vector at inference? During training the model predicts the next token at every position in one pass, which gives thousands of learning signals. At inference only the next token matters, so you read off the last vector.

What does softmax turn those scores into?

Logits can be any real number and they don’t sum to 1. Softmax converts them into a valid probability distribution. For logits $z_1, z_2, \ldots, z_V$:

$$ \text{softmax}(\vec{z})_i = \frac{e^{z_i}}{\sum_{j=1}^{V} e^{z_j}} $$

Every output lands in $(0, 1)$ and the outputs sum to 1. A three-token case shows every step.

Suppose the vocabulary is [cat, dog, runs] and the logits are:

$$ \vec{z} = \begin{bmatrix} 2 \\ -1 \\ 1 \end{bmatrix} $$

Step 1: exponentiate each logit.

$$ e^{z_{\text{cat}}} = e^{2} = 7.389056 $$
$$ e^{z_{\text{dog}}} = e^{-1} = 0.367879 $$
$$ e^{z_{\text{runs}}} = e^{1} = 2.718282 $$

Step 2: sum for the normalizer.

$$ Z = 7.389056 + 0.367879 + 2.718282 $$
$$ Z = 10.475217 $$

Step 3: divide each exponential by $Z$.

$$ P(\text{cat}) = \frac{7.389056}{10.475217} = 0.7054 $$
$$ P(\text{dog}) = \frac{0.367879}{10.475217} = 0.0351 $$
$$ P(\text{runs}) = \frac{2.718282}{10.475217} = 0.2595 $$

Check the sum:

$$ 0.7054 + 0.0351 + 0.2595 = 1.0000 $$

Greedy decoding picks cat (highest probability). Sampling rolls a weighted die with those three probabilities. In the real model, the same softmax runs over all 50,257 tokens.

What does temperature change in those probabilities?

Temperature $T$ rescales logits before softmax:

$$ \text{softmax}(\vec{z}/T)_i = \frac{e^{z_i/T}}{\sum_j e^{z_j/T}} $$

Low $T$ sharpens the distribution toward the top token, which makes output safer, more repetitive and nearly deterministic. High $T$ flattens it, which makes output more varied and more error-prone. The same logits $\vec{z} = [2, -1, 1]$ now get $T = 0.5$.

Step 1: divide each logit by $T$.

$$ \frac{z_{\text{cat}}}{T} = \frac{2}{0.5} = 4 $$
$$ \frac{z_{\text{dog}}}{T} = \frac{-1}{0.5} = -2 $$
$$ \frac{z_{\text{runs}}}{T} = \frac{1}{0.5} = 2 $$

Step 2: exponentiate.

$$ e^{4} = 54.598150 $$
$$ e^{-2} = 0.135335 $$
$$ e^{2} = 7.389056 $$

Step 3: normalizer.

$$ Z_{T=0.5} = 54.598150 + 0.135335 + 7.389056 = 62.122541 $$

Step 4: probabilities.

$$ P(\text{cat}) = \frac{54.598150}{62.122541} = 0.8790 $$
$$ P(\text{dog}) = \frac{0.135335}{62.122541} = 0.0022 $$
$$ P(\text{runs}) = \frac{7.389056}{62.122541} = 0.1189 $$

Compared with $T = 1$, cat went from 70.5% to 87.9%. The API slider I had been nudging without understanding does exactly this: rescale the logits, then apply softmax. I plotted all three probabilities against the temperature. At 1, cat is at 70.5%. At 0.5, it is at 87.9%.

Probabilities of cat, dog and runs against temperature, for logits 2, -1 and 1. Cat is 0.879 at T equals 0.5 and 0.705 at T equals 1.
Low $T$ pushes *cat* towards 1, and high $T$ pushes all three towards $1/3$.

Can we walk through one complete next-token prediction?

Yes. This section traces the full pipeline on a toy prompt, naming the GPT-3 dimensions wherever the real model would use them.

Prompt: The cat

Step 1 — tokenize

$$ \text{tokens} = [\text{The}, \text{ cat}] $$

(GPT-3 would assign integer IDs; I keep the words.)

Step 2 — embed each token

Toy embedding dimension $d = 2$ instead of 12,288. Suppose $W_E$ has learned:

$$ \vec{E}_{\text{The}} = \begin{bmatrix} 0.5 \\ 0.2 \end{bmatrix}, \quad \vec{E}_{\text{ cat}} = \begin{bmatrix} 1.0 \\ -0.5 \end{bmatrix} $$

In GPT-3 this lookup is a column grab from a $12{,}288 \times 50{,}257$ matrix: the same operation, with 12,288 numbers per token.

Step 3 — mix context (attention + MLP, ×96)

The middle of the model updates every vector so cat knows it follows The. After all blocks, the last vector might become:

$$ \vec{h} = \begin{bmatrix} 2.0 \\ -1.0 \end{bmatrix} $$

This is where attention does the heavy lifting: dot products between queries and keys, softmax over scores, a weighted sum of values. I skip the 96-layer detail here. The output is one context-rich vector $\vec{h}$, and the values above are made up to keep the arithmetic small.

Step 4 — unembed to logits

Toy vocabulary: [cat, dog, runs]. Unembedding matrix:

$$ W_U = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix} $$

Row 0 → cat, row 1 → dog, row 2 → runs. Each logit is a dot product of one row with $\vec{h}$:

$$ z_{\text{cat}} = W_U[0] \cdot \vec{h} = (1)(2) + (0)(-1) = 2 $$
$$ z_{\text{dog}} = W_U[1] \cdot \vec{h} = (0)(2) + (1)(-1) = -1 $$
$$ z_{\text{runs}} = W_U[2] \cdot \vec{h} = (1)(2) + (1)(-1) = 1 $$
$$ \vec{z} = \begin{bmatrix} 2 \\ -1 \\ 1 \end{bmatrix} $$

In GPT-3, $W_U$ has 50,257 rows and each logit is a 12,288-term dot product.

Step 5 — softmax at $T = 1$

$$ e^{2} = 7.389056 $$
$$ e^{-1} = 0.367879 $$
$$ e^{1} = 2.718282 $$
$$ Z = 7.389056 + 0.367879 + 2.718282 = 10.475217 $$
$$ P(\text{cat}) = 7.389056 / 10.475217 = 0.7054 $$
$$ P(\text{dog}) = 0.367879 / 10.475217 = 0.0351 $$
$$ P(\text{runs}) = 2.718282 / 10.475217 = 0.2595 $$

Step 6 — sample and append

Sample runs (26% chance), append it: The cat runs. Feed the longer string back in. Repeat.

Step 7 — what training does to wrong guesses

If the correct next token were runs but the model gave $P(\text{runs}) = 0.2595$, the cross-entropy loss is:

$$ L = -\log(0.2595) = 1.349 $$

The gradient on logits is $p - y$:

$$ \begin{bmatrix} 0.7054 \\ 0.0351 \\ 0.2595 \end{bmatrix} - \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 0.7054 \\ 0.0351 \\ -0.7405 \end{bmatrix} $$

The update pushes cat and dog down and runs up. Backprop carries that through $W_U$, every transformer block, and $W_E$. GPT does not return stored sentences. It is a parameterized model that estimates $P(\text{next token} \mid \text{previous tokens})$ over and over.

I ran the whole step on the toy numbers, with $\vec{h} = (2, -1)$ and $T = 1$. It reproduces Steps 4, 5 and 7.

Bars of the logits 2, -1 and 1 for cat, dog and runs, then the probabilities 0.705, 0.035 and 0.259. The loss if the true token is runs is 1.349.
The loss is 1.349 when the true token is *runs*.

What is the whole pipeline in one picture?

flowchart LR
    A["Prompt text"] --> B["Tokenize<br/>(50,257 vocab)"]
    B --> C["Embed via W_E<br/>(617M params, d=12,288)"]
    C --> D["Attention + MLP<br/>blocks ×96<br/>(dot products inside)"]
    D --> E["Take last vector"]
    E --> F["Unembed via W_U<br/>(617M params)"]
    F --> G["Logits → Softmax<br/>(÷ temperature)"]
    G --> H["Probability over<br/>50,257 tokens"]
    H --> I["Sample next token"]
    I --> A

One mental model kept the architecture from feeling opaque to me: parameters are weights, and nearly every step is a matrix-vector multiply, with nonlinearities (such as softmax) between them so the stack does not collapse into a single linear map.

Where should I go next?

The middle of that diagram, where dot products decide which tokens talk to which, is attention. I wrote the full walkthrough with queries, keys, values, masking, and the real GPT-3 head counts here: how attention works in transformers →.

Watch: But what is a GPT? — 3Blue1Brown · full text version on 3blue1brown.com · part of the Neural Networks series.

Support the writing

If this post helped, a coffee keeps the deep dives coming.

Buy me a coffee