All courses
Courses › How LLMs really work · foundation

Vectors

Treat a vector as a list of numbers with a length and direction; add, scale and subtract vectors.

Builds on: Reading formulas: indices and sums

▶ Watch this lesson Start the whole course

What the animation says

A list is an arrow

A language model stores each word piece as a list of numbers. Take just two: three and four. Read them as a walk from a fixed start: three steps east, then four steps north. Draw the arrow from the start to where you stop. That arrow is a vector. Its length is how far you end up: three squared plus four squared is twenty-five, so length five. Its direction is which way it points. Now double every number: six and eight. How long is that arrow?

Stretch, flip, shrink to one

Ten. Six squared plus eight squared is a hundred, and its square root is ten. Twice as long, same direction. Multiplying every number by the same value is called scaling. It stretches or shrinks the arrow. Scale by minus one and the arrow flips to point the opposite way: minus three, minus four. Scale by one over the length, one fifth, and you get point six and point eight. Length one, same direction. That length-one arrow is what we call the direction. It only exists when the length isn't zero.

Add and subtract

A second arrow: one and minus two. To add, walk the first arrow, then walk the second from where you stopped. You land at four, two. Entry by entry: three plus one, four minus two. Subtracting means adding the flipped arrow. Three minus one is two. Four minus minus two is six. So the difference is two, six. Now draw both arrows from the same start and join their tips. The difference runs along that line. Tap the end where its arrowhead goes.

Any number of entries

Real vectors have thousands of entries, so we stop drawing and keep the rule: work entry by entry. Add by adding matching entries. Both vectors must have the same number of entries. Scale by multiplying every entry. Length is still the square root of the sum of the squares. Now three entries. a is four, one, minus three. b is one, minus two, two. What is a minus two b?

Scale first, then subtract

Two, five, minus seven. First scale: two b is two, minus four, four. Then subtract entry by entry. Four minus two is two. One minus minus four is five. Subtracting a negative adds. Minus three minus four is minus seven. That's the whole toolkit: arrows with a length and a direction, added, scaled and subtracted entry by entry. A language model does exactly this, with thousands of entries.

What can you do with a list of numbers?

A language model stores each word piece as a list of a few thousand numbers. It then does its work by **combining** such lists: adding them, stretching them, taking differences. Before any of that makes sense you need one tool: how to treat a list of numbers as a single object with a **size** and a **direction**, and how to add, scale and subtract such objects.

A list of numbers is an arrow

Take two numbers, $(3, 4)$. Read them as a walk on a map from a fixed starting point: 3 steps east, 4 north. That arrow is a **vector**. Its **length** is how far you end up from the start. Its **direction** is which way it points. - **Add**: walk one arrow, then the other. - **Scale**: stretch or shrink the arrow; a negative number flips it. - **Subtract** $u - v$: draw both arrows from the same start; $u - v$ is the arrow from $v$'s tip to $u$'s tip.

The rules, entry by entry

A vector with $d$ entries ($d$ is its number of **dimensions**): $v = (v_1, \dots, v_d)$. - Add: $(u+v)_i = u_i + v_i$ (only if both have the same $d$) - Scale by a number $c$: $(cv)_i = c\,v_i$ - Subtract: $u - v = u + (-1)v$, so $(u-v)_i = u_i - v_i$ - Length: $\|v\| = \sqrt{\sum_{i=1}^{d} v_i^2}$ - Direction: $v / \|v\|$, meaning $v$ scaled by $c = 1/\|v\|$. This is the same arrow shrunk or stretched to length 1. It is defined only if $\|v\| \neq 0$.

Worked example with u = (3, 4)

$u=(3,4)$, $v=(1,-2)$. - $u+v = (3+1,\ 4-2) = (4, 2)$ - $u-v = (3-1,\ 4+2) = (2, 6)$ - $2u = (6, 8)$ - $\|u\| = \sqrt{9+16} = \sqrt{25} = 5$ - $\|2u\| = \sqrt{36+64} = 10$: double the length, same direction - Direction of $u$: $(3/5,\ 4/5) = (0.6, 0.8)$, length $\sqrt{0.36+0.64} = 1$

Remember this

A vector is a list of $d$ numbers you can picture as an arrow. It has a length $\sqrt{\sum v_i^2}$ and a direction $v/\|v\|$. Add, subtract and scale entry by entry. Scaling changes the length, and a negative number also flips the direction. With both arrows drawn from the same start, $u - v$ is the arrow from $v$'s tip to $u$'s tip.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.