Learn

Learning preferences

Make learning more comfortable. Changes apply immediately.

Enlarges page content, including diagrams and their labels. You can also use your browser’s zoom.

Choose the colours that are easiest for you to distinguish. Applies to lesson diagrams and lab plots. Labels, shapes, hatching and line patterns also distinguish meanings. Source photographs and raw image brightness stay unchanged; mask-only views are available for image overlays.

ExampleComparisonSample link

Saved in this browser on this device. Preferences are separate from progress backups.

Lesson 4 of 13 in this unit

Understand Neural Networks · SOURCE LESSON 07

Follow a token through a Transformer.

Inspect a tiny numerical decoder from token IDs to the next-token distribution.

By the end: Trace embeddings, two attention heads, normalization, residuals, feed-forward layers, logits, and decoding; explain the KV cache.

Start with the exampleJump to the experimentRead the explanation
Prerequisites and path options

Why learn this? Trace how context becomes a next-token prediction so you can understand generation and the memory cost of caching.

01 · TRY

Start with something concrete.

Start with “the cat sat.” Our toy tokenizer produces three IDs. A small decoder turns those IDs into vectors, shares information between positions, and produces scores for ten possible next tokens. Its fixed teaching weights are not trained, so expect unhelpful text even when every calculation is correct.

Try the interactive model
02 · PREDICT
Before you experiment

The next-token distribution gives “mat” the highest probability. What does that establish?

The next-token distribution gives “mat” the highest probability. What does that establish?
03 · EXPERIMENT

Change one thing. Watch what follows.

Loading this browser experiment…

04 · UNDERSTAND
READ & CONNECT

Understand what changed.

Back to the experiment

A shorter explanation, with the original worked examples available below. Both explain the same experiment and its limits.

Same learning goal in both modes: Trace embeddings, two attention heads, normalization, residuals, feed-forward layers, logits, and decoding; explain the KV cache.

The idea in everyday terms

A decoder turns a sequence of token IDs into scores for the next token. Each token starts with numerical information about its identity and position. Attention mixes permitted context, and other transformations refine the result before the final scores are converted into probabilities.

What to watch in the experiment

Follow one token through the displayed layers. The causal mask blocks later tokens so the model cannot read an answer that would be unavailable during generation. Compare cached and uncached calculations: storing earlier keys and values avoids repeating some work without changing the intended result.

What this does not establish

These are fixed teaching weights, not a trained language model. A cache uses memory and belongs to a particular model and prefix. Neither caching nor fluent-looking output establishes factual correctness.

Open the full worked examples, calculations and limitations

This is the same reference used in Technical mode. Expand it whenever you need the exact numbers, assumptions or further explanation.

Text becomes token IDs

A tokenizer converts text into token IDs from a vocabulary. Tokens in real models may be words, word pieces, bytes, or punctuation; a word can require several tokens. This lab uses a deliberately simpler tokenizer: lowercase words and punctuation, a ten-entry vocabulary, and an unknown-token ID for anything else. It displays exactly what happened to each piece.

Each ID selects an embedding row. Position information makes order available to the network. Our lab adds small sinusoidal position vectors. Other architectures can use methods such as rotary position embeddings, which affect attention computations. The exact mechanism varies; position information is not the same thing as a word’s identity.

Follow one decoder block

The lab uses four coordinates per token, two attention heads, and a six-unit feed-forward hidden layer. Its sine-generated weights are fixed and untrained. This is a working numerical example of the operations, not a miniature language model that has learned useful English.

  1. Normalize. Our layer normalization centers a token’s coordinates and rescales them by their spread, with a small stabilizing constant. It prepares values for the next transformation. Models may use other normalization choices.
  2. Compute attention. Two sets of query/key/value coordinates compare and mix only permitted positions. The head outputs are concatenated, then projected back to four coordinates.
  3. Add a residual. A residual connection adds the attention result to the block input, preserving a direct route for information and gradients.
  4. Transform each position. Normalize again, project from four to six coordinates, apply ReLU, project back to four, and add a second residual. This feed-forward network acts on each position separately, after attention has shared information between positions.

The walkthrough shows the selected token’s real intermediate vectors. Choosing a second block repeats the computation with another set of fixed weights. Inspect its input: it is the previous block’s output, not the original embedding.

Turning off explicit position vectors removes one source of order information. In our one-block example, swapping earlier tokens while keeping the final token fixed leaves its attention mixture unchanged, apart from numerical rounding. With multiple causal blocks, earlier representations can already depend on different prefixes; do not conclude that every position-free causal network ignores all order.

Scores become a distribution, then a choice

After the final block and normalization, a vocabulary projection produces logits: one raw score per possible next token. Softmax converts them to a probability distribution. Those probabilities are model outputs, not independently verified confidence estimates.

P(i)=exp⁡(zi/T)∑jexp⁡(zj/T)P(i)=\frac{\exp(z_i/T)}{\sum_j\exp(z_j/T)}

Greedy decoding chooses the highest-scoring token. Sampling draws according to the distribution. Temperature T changes how concentrated that distribution is; a lower positive temperature sharpens it. In this lab, a visible draw between 0 and 1 makes the sampling choice reproducible and inspectable. Append one token to continue generation, up to the ten-token lab limit.

The distribution for each row predicts what comes after that row’s token. During real pretraining, the target is the actual next token in training text, and cross-entropy rewards assigning it high probability. Our lab does no pretraining. Its generated text can be nonsensical even though the arithmetic is correct.

Caching changes cost, not meaning

The cache comparison processes the same sequence as successively longer prefixes. The uncached path recomputes every prefix; the cached path stores earlier keys and values and calculates only each new token’s projections. The comparison reports the largest logit difference and actual query/key/value projection counts. It does not claim measured speed on your device.

A cache needs memory and is tied to the model, input prefix, and settings that produced it. Changing those settings requires recomputing the relevant state. Caching is not learning: it does not alter weights or improve factual accuracy.

Why next-token prediction can teach useful structure

Predicting varied text benefits from grammar, associations, factual patterns, program structure, and reasoning-like procedures. A trained model can use a mixture of reusable patterns and memorization. Fluent or likely text still does not prove a correct fact, a reliable algorithm, or complete causal understanding. Use independent evidence and tools when exactness matters.

05 · APPLY
Check your understanding

A KV cache reuses previously computed keys and values. Which statement is accurate?

A KV cache reuses previously computed keys and values. Which statement is accurate?
06 · CONTINUE

Take this idea with you.

A decoder is a sequence of numerical transformations. Tokenization, position, attention, feed-forward layers, residuals, normalization, and decoding each have a distinct job.

My Learning
Explore related topics and the topic map

How this topic connects

Explore this topic on the interactive map

Use these connections to review a foundation or choose where to go next. You can open any topic without completing the others.

What it builds on

  • Self-attention

    The decoder places attention inside a complete prediction pipeline, connecting context mixing to next-token scores.

  • Vector arithmetic

    Treat a list of numbers as one representation and trace weighted sums. These operations connect the lesson’s inputs to its outputs.

You are hereFollow a token through a Transformer

Inspect a tiny numerical decoder from token IDs to the next-token distribution.

What it enables