Neurotic

A tiny neural network you can watch learn to tell cats from dogs.

Input space

target 1 target 0 perceptron says 1 wrong

The perceptron

Truth table

highlighted row = next example

Mistakes per pass through the 4 examples

The fix for XOR: add a hidden layer

Input space (x₁, x₂)

Hidden space (h₁, h₂)

The loss landscape

low → high loss ✕ lowest point current model

The model is P(dog) = σ(w·snout + b). It has only two numbers, so every possible model is one point on this map. The shading shows its loss on 40 animals. Training means walking downhill to the lowest point.

The current model as a curve

Cat (y = 0) Dog (y = 1)

Loss after each step

A tiny network: 2 inputs → 2 hidden → 1 output

forward pass backward pass

Every weight's gradient

Nudge check: move one weight up and down by 0.0001, rerun the forward pass, and see how much the loss changes. That is the gradient measured by brute force. It agrees with backprop, but brute force needs a separate forward pass for every weight, while backprop gets them all in one backward sweep.

Loss on this animal after each update

How many weights?

log scale: each gridline is 1,000× more

Weights and biases are the numbers training adjusts, also called parameters. Highlighted bars belong to the current step. Most labs no longer publish the size of their newest models, but the largest are thought to have hundreds of billions to trillions of weights.

What never changed

The neuron. Every circle in every diagram computes z = w₁x₁ + w₂x₂ + … + b and passes z through an activation function. An LLM has billions of these. Lab 1 has one.

The goal. Make the loss small: a single number that measures how wrong the predictions are on the training data. For an LLM the loss is how surprised it was by each next word.

The learning method. Backpropagation finds every weight's gradient (lab 4), and gradient descent nudges each weight downhill (lab 3). This is how GPT-3 was trained too, just across thousands of GPUs at once.

The text so far

outlined = what the model can see

Start again from

Shaded words were written by the model. The rest came from you.

What comes next?

chance it gets picked model's raw probability

Under the hood: how the next word is chosen

Loss: how surprised it is by the next word

Loss = −ln(probability given to the real next word), averaged over the training text. It can't reach 0. After "the cat likes", the training text has fish, milk and mice equally often, so even a perfect model can only give the right word one chance in three.

Word map: the learned embeddings

cat words dog words other

Each word is stored as a list of 12 numbers, its embedding. This map flattens those 12 dimensions down to the 2 that vary most (PCA), so some words that look close are really far apart. A word ends up near other words that are followed by the same things: kitten near cat, puppy near dog, the dog's sleeping places near each other. The colours are for you only. The model was never told which words go with cats.

This toy vs. a real LLM

Some of the training text

What the model actually receives: one piece of text

user template cat your prompt fish model's reply    what it sees

What comes next?

click a bar to choose it yourself

Which earlier tokens mattered?

How it's measured: hide one token from the model (replace it with an average, meaningless embedding) and see how far the chance of the highlighted token falls. A big drop means the model relied on that token. Click any word in the reply to inspect it.

Same prompt, five replies

Each reply token is drawn at random from the probabilities, so one prompt can produce different replies. At temperature 0 it always takes the top token, and all five replies are the same.

How real chatbots do it

A chat app never hands the model "a question". It builds one long text and asks the model to continue it. A typical layout is below. Each model family uses its own special marker tokens.

⟨system⟩ You are a helpful assistant. Keep answers short.
⟨user⟩ What does a cat eat?
⟨assistant⟩ Cats are carnivores. They mostly eat meat and fish. ⟨end⟩
  • The system prompt is just more text at the top. Like your message, it steers the reply only by changing the next-token probabilities.
  • Every reply token can see the whole conversation, not just the last 10 tokens like this toy. Attention (lab 6) lets each new token look back at every earlier one, up to the context window limit.
  • The model has no memory between messages. Every turn, the app sends the whole conversation again. Memory features work the same way: they paste saved notes into the text.
  • It replies instead of rambling on because, after pre-training on huge amounts of ordinary text, it was fine-tuned on many example conversations in this format and on human ratings of replies. The toy here learned from example chats.
  • It stops when it writes the end token. The app cuts the text there and shows you only the part after the assistant marker.

One of this toy's training texts:

Your text, split into tokens

small number = token id · ␣ = space · dashed = a single byte

What the model actually receives

The model never sees letters. Each id is looked up in an embedding table (labs 7 and 8), and from then on the token is just a list of numbers.

The merges, in the order they were learned

Each line: two existing tokens that sat next to each other most often in the training text, joined into one new token. The count is how often that pair appeared at that moment.

Why "strawberry" is hard to spell

How byte-pair encoding (BPE) works

1 · Start with bytes. Every piece of text is stored as bytes, numbers from 0 to 255. Letters, digits and punctuation take one byte each. Characters like é or 🐱 take 2 to 4. So the tokenizer starts with 256 tokens, one per byte, and can encode any text at all.

2 · Merge the most common pair. Count every pair of neighbouring tokens in the training text, join the most frequent pair into a new token, and repeat. Common words end up as one token. Rare words stay in pieces.

3 · Real tokenizers work the same way but learn far more merges from far more text. GPT-2's had a vocabulary of 50,257 tokens, and newer ones have 100,000 to 200,000 or more. For English, a rough rule of thumb is about 4 characters per token.

Why it matters. Context windows, speed and prices are all counted in tokens. Text in languages that were rare in the tokenizer's training data splits into more tokens, so it costs more and fills the context window faster. Spelling, counting letters and arithmetic are harder, because the model sees chunks, not characters or digits.

Where does the question look?

arc thickness = attention weight

All three questions at once

Each row is one question, and each cell is how much of its attention goes to that word. Same sentence, different question, so the attention moves.

Accuracy while training

Share of correct answers on the latest 200 training examples. Every example is a fresh random sentence, so the model can't just memorize a list.

Under the hood: query, key, value

What it learned, and what a real Transformer adds

The task. Read a sentence, then answer one question: which animal, which colour, or which food? The model is one attention head plus an output layer, with weights, all starting random.

Nobody told it which words are colours. It only ever saw sentences and correct answers. To get the answers right, it had to learn query and key vectors that make "color?" match colour words, "food?" match food words, and so on. That is the sharp attention you see after training.

Distance doesn't matter. The answer can be the first word or the tenth. Attention compares the question with every word directly. The fixed 10-token window in lab 8 can't do that.

A real Transformer does this for every word, not just the question. Each layer has many heads, each looking for a different kind of relationship, and dozens of layers are stacked. Word positions are added to the embeddings too, because attention on its own ignores word order. This toy has none of that, so "the dog chased the cat" and "the cat chased the dog" look identical to it.

Feature space

Cat Dog Shading = what the network predicts
Click adds:

Inspector: draw or pick a picture

Cat
Dog

Drag on the pad to draw. Right-click drag (or Eraser) to erase. Everything updates live.

The network

positive weight negative weight

Line thickness shows how big a weight is. Node colour shows that neuron's activation for the current example. Hover a neuron for its numbers.

Under the hood: one example, step by step

Loss (lower is better)

TrainTest

Accuracy

TrainTest

What each hidden neuron looks for

pixel pushes the neuron up pixel pushes it down

Each tile shows one neuron's 400 incoming weights laid out as a 20×20 image. a is that neuron's activation for the picture in the inspector. Tiles start as random static. Weight decay makes the pixels that never matter fade, so the learned shapes stand out. Set it to 0 to compare. A tile that stays static with a=0.00 for every picture is a "dead" ReLU neuron. It never switches on, so it never learns. Try tanh to see the difference.

Test pictures (never used for training)