Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 06 · AI · GPT, built from scratch · with code: Python · about 23 min to read

Watch: Attention from scratch: the mechanism that ate the field
▶ VideoPlay it as a journeyTutor comes with a plan

Attention by Hand

Attention and the Transformer
Attention and the Transformer · Open board

The Attention and the Transformer board draws each word looking back at the words before it. This lesson builds that look by hand, then trains it: the fourth of six lessons that end with a small GPT built from scratch. A GPT is the kind of model behind ChatGPT: it writes text one piece at a time, each time guessing what comes next.

Before you start. A weight is one adjustable number inside a model, and training nudges every weight to make the loss smaller (Autograd by Hand). The loss is one number for how wrong the model’s guesses were, and lower is better. In The MLP Language Model a model read the last eight characters of a returns-desk reply (a short customer-service reply about a returned order) and guessed the next one. That model was an MLP, a multilayer perceptron: layers of neurons stacked one after another. From The Maths Floor, Part 2: Probability, Softmax and Temperature: softmax turns a list of scores into shares that add up to 1. From Vectors, Dot Products and Matrix Multiply: multiply two lists of numbers pair by pair and add the products, and you get one number that is big when the lists match. From Attention & the Transformer: attention lets each word take a share of its meaning from each word before it.

Lesson 62 of 83 in AI

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • August 2013, Toronto: Graves gives a pen a finger on the text
  • Where eight characters run out: a long-range dependency
  • Query and key: what a position looks for, what each one shows
  • The causal mask: a card over everything not yet written
  • Value: what each position hands over, added up in the shares
  • Heads: four fingers, each asking its own question
  • The result: two coin tosses become near-certain
  • From three positions to a whole reply
  • Where does a character get its three lists?
  • How does each new number learn which way to move?
  • What did the run show?
  • All of it in code

Terms it explains: Attention and the Transformer, long-range dependency, query, key, causal mask, value, head, attention matrix, cross-attention, feed-forward (FFN/MLP), layer norm, multi-head, O(n²), positional encoding, Query/Key/Value, residual connection, scaled dot-product attention, self-attention, stack of N blocks, transformer block.

Related papers

Review AI