Vectors, Dot Products and Matrix Multiply a show in 8 chapters
Goal: score one film for 3 viewers, using only multiply and add
1 of 8 Your guess
Before we start
Voice search, chat assistants and image search all run on programs called neural networks. Google built a whole chip just to run them.
Which picture is closest to what that chip spends its day doing?
Pick the closest. You find out which fits at the end.
2 of 8 The problem
2013: a chip built for one sum
In 2013, engineers at Google did a sum on the back of an envelope and did not like the answer. Their speech recognition ran on neural networks, and it worked.
So they asked: what if people used voice search for three minutes a day? On the ordinary chips in every server, Google's datacenters would have to double to keep up.
Those ordinary chips can do anything, so they do any one thing only so fast. A doubling was not free.
So Google started a rush project: a chip that could do almost nothing. No web server, no database. It did one sum, over and over.
Multiply two numbers, then add the result to a running total. Call that one multiply-and-add.
At its heart was a grid of 256 by 256 little calculators, all doing that one sum at the same moment. They called it the Tensor Processing Unit, or TPU.
It was designed, built and running in datacenters within 15 months, in service from 2015.
Your goal: score one film for a cinema of 3 viewers, how much each would enjoy it, using nothing but multiply-and-add. That is the sum the chip does, and by the end you will have done it by hand.
Romance is left out here, so each film fits on flat paper: 2 slots, 2 directions.
3 of 8 The idea
A film as a short list of numbers
A computer cannot watch a film. It eats numbers only. So describe the film with numbers.
Pick three things, always in the same order: how much action, how much romance, how much comedy, each out of 5. A heist film might score 4, 1 and 3.
These films and scores are made up for this lesson, so the sums fit in your head.
Every film gets the same three slots in the same order. The order is the whole trick: the second slot always means romance.
A list of numbers like this, where each position has a fixed meaning, is a vector. That is all the word means.
How many slots it has is its number of dimensions. These have 3. Real ones have hundreds.
There is a second way to see a vector. Keep two slots, action and comedy, and the heist is a point on graph paper: 4 along, 3 up. Draw an arrow from the corner to it.
The caper, 5 along and 4 up, points almost the same way. The weepie, 0 along and 1 up, points straight up, off on its own.
Films of the same kind point the same way. A vector is a description when you read it, and an arrow when you draw it.
4 of 8 Try it
Multiply the pairs, add them up
Now you. Say one point of action is worth 2 to you, one point of romance costs you 1 (you get bored), and one point of comedy is worth 1.
Your taste is a vector too, in the same order as the film: 2, −1, 1.
How much will you enjoy the heist? Line the two lists up, slot over slot. Multiply each pair, then add the three answers.
Press Play to multiply one pair at a time.
That number is a dot product: two vectors of the same length, multiplied slot by slot, then added. It is the chip's multiply-and-add, done 3 times and totalled.
Why does a minus take away?
Your taste for romance is −1. A positive number times a negative one is negative, so the more romance a film has, the more it pulls your total down.
Same taste, the other two films. Pick one and watch its pairs.
You would pick the caper: 14 beats 10, and the weepie scores −4.
Big and positive: the two lists agree. A zero in a slot adds nothing. Opposite pulls, a film full of romance and you against it, take away.
It works between two films as well. Pair the heist with the caper, then with the weepie.
32 against 8: the heist agrees with the caper four times as much (32 ÷ 8 = 4). A dot product measures how much two lists agree.
The arrows said the same: the heist and the caper point almost the same way.
5 of 8 The idea
A table of viewers
One viewer is not a cinema. Add two more, each a taste: the romantic (−1, 3, 0) and the comic (0, 0, 2). Stack all three tastes in one table.
Each viewer is one row, running across. Each slot is one column, running down. A table of numbers in rows and columns is a matrix.
Multiplying the matrix by the heist means: take each row, dot it with the film, write the answer down. The film sits on top so each number lines up with its pair.
Press Play to score the film one row at a time.
That is matrix multiply: nothing new, only the dot product done once per row. The film went in described by what is in it and came out described by who likes it.
What changed
- Viewers scored for the heist: 1 → 3
Why is the film written across the top?
So each film number sits right above the taste number it pairs with: action over action, romance over romance. Every row pairs with the same film.
Put three films side by side and every viewer scores every film: 3 viewers × 3 films = 9 dot products, one table out.
You and the heist: 10, from step 2.
Google's chip does 65,536 multiply-and-adds in a single tick: this table, far larger.
768 columns × 3,072 rows = 2,359,296 multiply-and-adds, for one layer, for one word
65,536 cells × 2 (a multiply and an add) × 700,000,000 ticks a second= 91.75 trillion a second. The 2017 paper quotes a peak of 92 trillion.
6 of 8 The idea
One layer of a neural network
Two small additions turn this into one step of a neural network. First: some viewers are harder to please before the film even starts.
Give each one a fixed amount added to their score: you start at −4 (a long day), the romantic at 0, the comic at +1. That fixed extra per row is the bias.
Second, a rule: any score below zero becomes zero. A viewer who would hate the film does not report hating it. They just do not show up.
The rule is an activation. This one, "negative becomes zero", is called ReLU: the negative half is cut off, the positive half passes through.
The whole step, written the short way: y = activation(W x + b). x is the film, W the table, b the moods.
What changed
- The romantic's score: −1 → 0
Why throw a negative away?
A viewer who would hate the film does not report hating it. They just do not show up, and a zero sends nothing on to the next layer.
Draw one viewer on their own and you get the drawing every neural network lesson shows: inputs on lines, a weight on each line, a circle that adds, one output. That is a neuron.
The numbers on the lines are your taste. In a network they are called weights. A neuron holds one taste and asks one question: how much does what comes in agree with it?
A layer is a row of these viewers watching the same film at once. Each row of the table is one neuron.
The table has one row per viewer and one column per slot in the film. The work is one multiply-and-add per seat: our 3 by 3 table is 9.
GPT-2 small, a real language model, has layers that take 768 numbers in and give 3,072 out: 3,072 viewers, each with a taste of 768 numbers.
What changed
- Multiply-and-adds for one layer: 9 → 2,359,296
That is for one word. A model reads a page as hundreds of words, through dozens of layers like that.
Now the chip has a shape that makes sense. Its 256 by 256 grid is a cinema of 256 viewers with 256 tastes each.
The tastes sit still in the cells, the film's numbers flow across, and a row of 256 totals comes out the far side. It ticks 700,000,000 times a second.
The paper's peak is for whole numbers, not decimals.
7 of 8 Your guess
Your picture now
Goal met: one film, 3 viewers, 9 multiply-and-adds. The scores 10, −1, 6, plus moods, then the zero rule, came out 6, 0, 7. Which picture is closest now?
Pick one.
c is the TPU: a 256 by 256 grid of multiply-and-add cells. a is how an ordinary program runs, one rule at a time. b stores answers, and a network stores none: it keeps its tables of weights and works every answer out with them.
8 of 8 Keep this
Keep this
A vector is a list where each slot always means one thing. A dot product multiplies two of them slot by slot and adds up: how much they agree.
A matrix is a stack of them, one per row. Times a vector, it is one dot product per row. Add a bias, apply a rule like ReLU, and that is one layer of a neural network.
Next in the lesson: why a bigger film is not a more similar one, and where this sum turns up again in embeddings and attention.
Read the full lessonComing up Multiply the pairs, add them up, drop anything below zero: that is one neuron. Next: the 1958 machine built from exactly that sum, a room of light sensors whose dials turned themselves.