A REAL TRAINED TRANSFORMER · RUNNING IN THIS PAGE · 0 LIBRARIES

THE ATTENTION
MICROSCOPE

Attention is the idea that made modern AI possible: every word gets to look back at every earlier word and decide which ones matter. It's usually invisible, buried under billions of parameters. This page puts a real trained transformer — small enough to see through — under glass. Type a sentence; every line you see is a genuine attention weight from a genuine model.

LOOK THROUGH IT → EXPLAIN IT FIRST
01

What attention actually is

Older networks read a sentence like water through a pipe — each word squeezed through in order, distant words fading. The transformer's 2017 move: let every word query every other word directly.

For each token, the model computes three small vectors, and the names are wonderfully literal:

  • a "here is what I'm looking for";
  • a "here is what I can offer";
  • a "and here is the information I'll hand over if you pick me."

Each token's query is compared against every earlier token's key — a dot product, i.e. a match score. The scores go through a , which turns them into positive weights summing exactly to 1: an attention budget. The token then takes a weighted blend of everyone's values. High weight = "this word matters for understanding me". That blend flows forward, and prediction is built on it.

attention(Q,K,V) = softmax( Q·Kᵀ / √d ) · V   // the most famous equation in modern AI

Two more pieces and you can read everything below. First, this model predicts the next word, so a token may only look backwards — the blanks out the future (that's why the map in the microscope is a triangle). Second, the model runs several attentions in parallel — — each free to learn a different notion of "what matters": one head might track grammar while another tracks names. This page's model has 8 heads across 2 layers, and you can inspect each one.

02

The microscope

Build a sentence from the model's 67-word vocabulary (it's a small creature — see the honesty box). Click any token to see where its attention goes. The star demo is preloaded: to guess the adjective after "…because it was", the model must look back at both nouns.

INPUT — A SENTENCE FROM THE MODEL'S WORLD
ATTENTION — CLICK A TOKEN, FOLLOW ITS EYES
attention weight (thicker = more)selected token
PICK A HEAD — EACH LEARNED ITS OWN JOB
LAYER 1 (FIRST)
LAYER 2 (SECOND)
NEXT-WORD PREDICTION — LIVE

THE FULL ATTENTION MAP

Every row is a token looking left; darker pixel = more attention. The empty top-right triangle is the causal mask: the future is invisible. Selected token's row is outlined.

Three experiments, one revelation each

  • ★ Cat on mat. With the cat sat on the mat because it was loaded, look at the predictions: they split almost evenly between cat-ish words (sleepy, soft) and mat-ish words (warm, flat). Now look at the attention lines from was: they reach back to cat and mat. Then cycle the individual layer-1 heads and find the division of labour — one lookup head locks onto the animal at ~99%, two others lock onto the object. The model is resolving "it" before your eyes, with different heads carrying different candidates — nobody programmed that; it emerged from gradient descent because guessing the adjective is impossible without it. Swap to DOG ON ROOF and watch both the attention and the predictions move house.
  • Repeated name. Load anna took the ball and — the model is ~99% sure the next word is anna. Click and, select the head labelled NAME/INDUCTION: it stares straight back at the first anna. This copy-what-came-before circuit is an — the same circuit later found operating inside real large language models.
  • Was or were? Load the cats near the box — prediction: were. Load the DISTRACTOR preset — prediction flips to was, ignoring the plural bait right next door. Click the last token and find the head reaching past the distractor to the true subject. That's grammar as a learned attention pattern.
03

Heads have jobs (nobody assigned them)

The head labels in the microscope weren't designed in — they were discovered, by measuring each head's behaviour after training. That discipline has a name, and it's one of the liveliest fields in AI.

Training never tells a head what to do. It only says: predict the next word, and here's your share of the gradient. Yet out of that pressure, specialists condense. In this page's model, measurement found antecedent-lookup heads (they connect "it…was" back to the nouns), a name/induction head (it finds earlier repetitions and copies them forward), and a subject-lookup component that solves was/were by attending past distractor nouns to the true subject.

The astonishing part is that the same story repeats at a billion times the scale. When researchers opened up real LLMs, they found induction heads doing precisely what the one on this page does — spot a repeated pattern, attend back to what followed it last time, copy — and there is strong evidence these circuits are a large part of how big models learn from examples in their prompt (so-called in-context learning) 2022. Reverse-engineering trained networks into circuits like these is called . On a 60,000-parameter model you can do it with a for-loop. On a trillion-parameter model it is a research frontier — and one of the main hopes for making AI systems auditable rather than oracular.

THE DEEP POINT

Attention weights are the rare part of a neural network that arrives pre-drawn as a diagram: a number between every pair of words, begging to be rendered as lines. That's what makes this microscope possible — and it's why attention maps became the field's favourite window into what these systems are doing, even though they are only part of the story (the values, and everything downstream, matter too).

04

Honesty box: what this model is, exactly

Everything you just saw is real, and it's important to be precise about what "real" means here:

  • The model is a genuine transformer — ~60,000 parameters, 2 layers, 4 attention heads per layer, 48-dimensional — trained with backpropagation and the Adam optimizer written from scratch in NumPy (the gradients were verified against finite differences). Its weights are embedded in this page (~220 KB) and the you're running is ~150 lines of plain JavaScript. View source: it's all there.
  • Its world is 67 words and four kinds of sentence, generated synthetically — deliberately, so that interpretable heads had to form and you could catch them at work. It has never seen the internet. It cannot chat. It is a bacterium to a frontier model's whale — but bacteria and whales run the same biochemistry, and that's the point.
  • What's different at frontier scale: real models read tokens, not whole words; they are ~100 layers deep and ~100× wider, with vocabularies of 100–200K; they add refinements (rotary position encodings, grouped or sparse attention, mixture-of-experts layers); and after next-word pretraining they are shaped by fine-tuning and RLHF. None of that changes what an attention head is.
  • The road not taken: we could have shipped a famous pre-trained model instead — GPT-2 runs in browsers via the ONNX runtime, and the excellent Transformer Explainer does exactly that. The cost is a large download and a black-box runtime. This page chose the other trade: a model small enough that nothing is hidden — not the weights, not the math, not the training code.

Companion pages: How Machines Read (how text becomes tokens before any of this happens) and How Machines Learn (the neurons, gradients and backpropagation underneath it all).