thecatsatonthemat1p = softmax(W[a])2loss = -math.log(p[b])3G[a][j] += p[j] - (j == b)4W[a][j] -= lr * G[a][j]p(next token | "the")catmatonsattheLOSS1Q: Capital of France?2A: Paris."No idea.""Paris."REWARDthep(cat)G
Sky mat: pretraining (next-token prediction) · gold mat: fine-tuningTeal block: the model, with its probabilities for the token after "the"Green ▲ / salmon ▼: the gradient says raise / lower that scoreOn the sky pretraining mat: the training text, "the cat sat on the mat", as six token chips; the four-line training loop; the model, showing its probability for each token coming after "the"; and the loss meter. One training step predicts, scores the guess with cross-entropy, works out the gradient and nudges the weights. Repeated 100 times, cat and mat each settle at 0.50 and the loss at about ln 2. On the gold mat in front: fine-tuning on demonstrations, then on answers people ranked, through a reward model. Our model is a bigram model: it looks only at the previous token, with one row of scores per token. A real LLM is a transformer that looks at every earlier token, with billions of weights, but it is trained with the same loss and the same gradient descent.

Text becomes examples

  1. Pretraining starts with a huge pile of text: GPT-3 learned from 300 billion tokens. Ours is one sentence, "the cat sat on the mat": 6 tokens. To keep it simple, each word is one token here.

One training step

Pretraining at scale

Fine-tuning

What happens

;; step 1/10 · corpus
text    "the cat sat on the mat"
tokens  the | cat | sat | on | the | mat
vocab   cat mat on sat the   (5 different tokens)

GPT-3   300 billion tokens of training text
stage 1 of 3: pretraining

Our model is a bigram model: it looks only at the previous token, with one row of scores per token. A real LLM is a transformer that looks at every earlier token, with billions of weights, but it is trained with the same loss and the same gradient descent. We take full-batch steps (all five examples, gradients added) with learning rate 1. Real training uses random mini-batches of millions of tokens, a tuned learning-rate schedule and the Adam optimiser (gradient descent that adapts the step size for each weight from its recent gradients).