Skip to content
AriadneTechnology

The Outer Ring · Chamber 1 of 9

Vectors: Arrows, Lists and Data

Two views of the same object, and why every image, word and user in ML becomes a vector.

35 min 50 XP + 12 questions + 1 challengeMathVideoPapersProofsCodeLab

In this chamber you will

  • Move between the arrow view and the list view of a vector
  • Add vectors and scale them, geometrically and by components
  • Write any vector as a combination of the standard basis
  • Explain how word embeddings turn meaning into geometry

How this course works

In Mathematics for Machine Learning you learned to read the symbols: a bold x\mathbf{x} for a vector, Rn\mathbb{R}^n, ∑\sum, the transpose ⊤\top. This course is about what those objects do. Linear algebra is the mathematics of vectors and matrices, and nearly every model in machine learning is built from it. An image is a vector, a layer of a neural network is a matrix, and training a model means moving one very long vector of parameters around.

You don't need any linear algebra to start. Each idea arrives geometry first (a picture you can drag), then as algebra (rules you can compute with), then with a derivation or a proof (why the rules are true), and always with a link back to machine learning.

Every chamber runs the research loop you met in the first course:

1

Discover

Meet the idea in the wild, before it has a name.

2

Learn

Notation, definitions, derivations and proofs.

3

Read beyond

Go past the course: books, notes and articles.

4

Papers & lectures

Find it in real research, and watch the experts.

5

Your turn

Prove it, code it, test yourself. Then repeat.

Each chamber has an interactive lab with a challenge, proofs to assemble and to write, three coding problems in the style of Project Euler, and at least one decoder for an equation from a real paper. The checkpoint at the end clears the chamber. Nine chambers from now, Talos, the bronze giant who guarded Crete, is waiting.

DiscoverLearnRead beyondPapers & lecturesYour turn

Discover: arithmetic with words

In 2013, three researchers at Microsoft Research looked closely at the numbers a language model had learned. The model had turned every word in its vocabulary into a list of numbers (up to 1,600 of them) only to help it predict the next word in a sentence. But when the researchers did arithmetic with those lists, the arithmetic made sense.

Spotted in the wild

y=xb−xa+xcy = x_b - x_a + x_c
Mikolov, Yih & Zweig (2013), “Linguistic Regularities in Continuous Space Word Representations”, Section 5

To answer the analogy “aa is to bb as cc is to what?”, the paper takes the lists for the three words, computes y=xb−xa+xcy = x_b - x_a + x_c, and looks for the word whose list is closest to yy. With aa = man, bb = king and cc = woman, the abstract reports that king − man + woman gives a vector very close to queen.

How do you subtract a word? You can't. But you can subtract one list of numbers from another, entry by entry. That is the first idea of this chamber: a vector is a list of numbers that you can add, subtract and scale, and it is also an arrow you can draw. (The paper writes its vectors in plain italics, xbx_b. This course writes them in bold, xb\mathbf{x}_b, as the first course did.)

Try the arithmetic on vectors small enough to do by hand.

Quick check +20 XP

Here are toy word vectors in R2\mathbb{R}^2: xman=(1,1)\mathbf{x}_{\text{man}} = (1, 1), xwoman=(1,3)\mathbf{x}_{\text{woman}} = (1, 3) and xking=(5,1)\mathbf{x}_{\text{king}} = (5, 1). Working one entry at a time, what is xking−xman+xwoman\mathbf{x}_{\text{king}} - \mathbf{x}_{\text{man}} + \mathbf{x}_{\text{woman}}?

DiscoverLearnRead beyondPapers & lecturesYour turn

Learn: two views of one object

A vector has two faces, and much of the power of linear algebra comes from switching between them.

The arrow. A physicist's vector is an arrow: it has a length and a direction, and nothing else. Slide it anywhere and it's the same vector. In linear algebra we usually start every arrow at the origin, the point where the axes cross, so the arrow is pinned down by where its tip lands.

The list. A programmer's vector is an ordered list of numbers, like [3, 1]. Order matters: (3,1)(3, 1) and (1,3)(1, 3) are different vectors.

The bridge between the two is coordinates. The list (3,1)(3, 1) says: from the origin, walk 3 units along the first axis and 1 unit along the second. The arrow ends there. We write vectors as columns,

v=[31]∈R2,\mathbf{v} = \begin{bmatrix} 3 \\ 1 \end{bmatrix} \in \mathbb{R}^2,

read “v is the column vector three, one, in R two”. In running text we save space and write v=(3,1)\mathbf{v} = (3, 1). The numbers v1=3v_1 = 3 and v2=1v_2 = 1 are the vector's components, or entries.

R2\mathbb{R}^2 is the plane: every list of two real numbers. R3\mathbb{R}^3 is ordinary three-dimensional space, and Rn\mathbb{R}^n is every list of nn real numbers. You can't draw an arrow in R784\mathbb{R}^{784}, but you don't need to. Every rule in this chamber works one entry at a time, so it works in 784 dimensions exactly as it does in 2. Draw in 2D, then trust the algebra in any number of dimensions.

An arrow doesn't have to start at the origin. The arrow from point PP to point QQ is written PQ→\overrightarrow{PQ}, and its components are “destination minus start”. From P=(1,2)P = (1, 2) to Q=(4,0)Q = (4, 0), it's PQ→=(4−1,0−2)=(3,−2)\overrightarrow{PQ} = (4 - 1, 0 - 2) = (3, -2): three right and two down.

Vector notation in this chamber
  • Rn\mathbb{R}^n“R n”
    Every list of nn real numbers: the space the vectors live in. R2\mathbb{R}^2 is the plane, R3\mathbb{R}^3 ordinary space.
    x∈R784\mathbf{x} \in \mathbb{R}^{784}
  • [v1v2]\begin{bmatrix} v_1 \\ v_2 \end{bmatrix}“the column vector v one, v two”
    A vector written as a column. Its entries are the arrow's coordinates. In running text it's written (v1,v2)(v_1, v_2) to save space.
    v=[31]=(3,1)\mathbf{v} = \begin{bmatrix} 3 \\ 1 \end{bmatrix} = (3, 1)
  • PQ→\overrightarrow{PQ}“vector P Q”
    The arrow from point PP to point QQ: “destination minus start”.
    PQ→=Q−P\overrightarrow{PQ} = Q - P
  • u+v\mathbf{u} + \mathbf{v}“u plus v”
    Add entry by entry. As arrows: put the tail of v\mathbf{v} at the tip of u\mathbf{u} (tip to tail).
    (3,1)+(1,2)=(4,3)(3, 1) + (1, 2) = (4, 3)
  • c vc\,\mathbf{v}“c times v”
    Scalar multiplication: multiply every entry by the number cc. Stretches or shrinks the arrow by ∣c∣|c|, and flips it if c<0c < 0.
    3 (1,−2)=(3,−6)3\,(1, -2) = (3, -6)
  • −v-\mathbf{v}“minus v”
    The negative of v\mathbf{v}: the same length, pointing the opposite way. It equals (−1)v(-1)\mathbf{v}.
    −(2,−5)=(−2,5)-(2, -5) = (-2, 5)
  • u−v\mathbf{u} - \mathbf{v}“u minus v”
    u+(−v)\mathbf{u} + (-\mathbf{v}). Drawn from the origin, it's the arrow from the tip of v\mathbf{v} to the tip of u\mathbf{u}.
    (4,3)−(1,2)=(3,1)(4, 3) - (1, 2) = (3, 1)
  • 0\mathbf{0}“the zero vector”
    Every entry zero. Adding it changes nothing, and it has no direction.
    v+0=v\mathbf{v} + \mathbf{0} = \mathbf{v}
  • ei\mathbf{e}_i“e i”
    The ii-th standard basis vector: 1 in position ii, 0 everywhere else. In machine learning it's called a one-hot vector.
    e2=(0,1,0)∈R3\mathbf{e}_2 = (0, 1, 0) \in \mathbb{R}^3
  • ∑i=1kcivi\sum_{i=1}^{k} c_i \mathbf{v}_i“the sum of c i times v i”
    A linear combination: scale each vector vi\mathbf{v}_i by its coefficient cic_i, then add the results.
    c1v1+c2v2c_1\mathbf{v}_1 + c_2\mathbf{v}_2
  • xking\mathbf{x}_{\text{king}}“x king”
    A word embedding: the learned vector for one word. Here the subscript names the word, not a position.
    xking∈R300\mathbf{x}_{\text{king}} \in \mathbb{R}^{300}

Learn: adding, scaling and subtracting

Adding two vectors means one thing in each view, and the two agree.

  • As lists: add entry by entry, so (3,1)+(1,2)=(4,3)(3, 1) + (1, 2) = (4, 3).
  • As arrows: put the tail of the second arrow at the tip of the first, tip to tail. The sum runs from the start of the first to the tip of the second.

Why do they agree? Walking along (3,1)(3, 1) takes you 3 right and 1 up. Walking along (1,2)(1, 2) from there takes you 1 more right and 2 more up. In total you've gone 3+13 + 1 right and 1+21 + 2 up. Walking the two arrows in the other order traces the other two sides of a parallelogram and ends at the same corner, which is why u+v=v+u\mathbf{u} + \mathbf{v} = \mathbf{v} + \mathbf{u}.

Scaling a vector by a number cc multiplies every entry by cc: c v=(cv1,cv2)c\,\mathbf{v} = (c v_1, c v_2). Geometrically it stretches the arrow by a factor ∣c∣|c| and turns it round if cc is negative. So 2v2\mathbf{v} is twice as long, 12v\tfrac{1}{2}\mathbf{v} is half as long, and −v-\mathbf{v} has the same length but points the opposite way. Ordinary numbers like cc are called scalars, because they scale.

Two more pieces complete the kit. The zero vector 0=(0,0)\mathbf{0} = (0, 0) is the arrow that goes nowhere, and adding it changes nothing. Subtraction is adding a negative: u−v=u+(−v)\mathbf{u} - \mathbf{v} = \mathbf{u} + (-\mathbf{v}).

Subtraction has a picture worth memorising. Draw u\mathbf{u} and v\mathbf{v} from the origin. Then u−v\mathbf{u} - \mathbf{v} is the arrow from the tip of v\mathbf{v} to the tip of u\mathbf{u}, because v+(u−v)=u\mathbf{v} + (\mathbf{u} - \mathbf{v}) = \mathbf{u}. It's the “destination minus start” rule again. A difference of two vectors tells you how to get from one to the other, and that's what the word-vector paper measures: xking−xman\mathbf{x}_{\text{king}} - \mathbf{x}_{\text{man}} is the step from “man” to “king”.

A worked example, with u=(2,−1)\mathbf{u} = (2, -1) and v=(1,3)\mathbf{v} = (1, 3):

3u−2v=(6,−3)−(2,6)=(4,−9).3\mathbf{u} - 2\mathbf{v} = (6, -3) - (2, 6) = (4, -9).
Quick check +20 XP

Draw u\mathbf{u} and v\mathbf{v} as arrows from the origin. Which vector is the arrow that starts at the tip of v\mathbf{v} and ends at the tip of u\mathbf{u}?

Learn: linear combinations

Put scaling and adding together and you get the central construction of linear algebra. A linear combination of u\mathbf{u} and v\mathbf{v} is any vector of the form

a u+b v,a\,\mathbf{u} + b\,\mathbf{v},

where the scalars aa and bb are called coefficients. With more vectors the idea is the same, written with the sum notation from Chamber 5 of the first course:

c1v1+c2v2+⋯+ckvk=∑i=1kci vi.c_1\mathbf{v}_1 + c_2\mathbf{v}_2 + \cdots + c_k\mathbf{v}_k = \sum_{i=1}^{k} c_i\,\mathbf{v}_i .

It's called linear because you only scale and add: no squaring, and no multiplying vectors together. Think of the vectors as ingredients and the coefficients as how much of each goes in.

Here's the question the lab asks. Given u\mathbf{u}, v\mathbf{v} and a target point, which coefficients land on it exactly? Steer with the sliders first, then read on for the algebra.

Interactive lab

Vector navigator

The violet arrow is a·u + b·v, drawn tip to tail: first a·u, then b·v from where it ends. Choose a and b to land exactly on each of the three rose targets.
−6−6−4−4−2−2224466(4, 4)(−1, 5)(−3, −5)vu

Slide a and b to steer the violet arrow a·u + b·v. Drag the heads of u and v to change the ingredients.

Combination

1·(3, 1)
+0·(1, 3)
=(3, 1)

Targets

  • ○(4, 4)
  • ○(−1, 5)
  • ○(−3, −5)

Vectors (or drag the heads)

u= (,)
v= (,)
Challenge: NavigatorReach all three targets with linear combinations of two vectors.+40 XP

Things to try:

  1. Reach all three targets. One of them needs a coefficient that isn't a whole number.
  2. Drag v\mathbf{v} until it lies along the same line as u\mathbf{u}, for example v=(−3,−1)\mathbf{v} = (-3, -1). How much of the grid can you reach now?
  3. Make u\mathbf{u} the zero vector. What's left?

Learn: finding the coefficients

Guessing is fine in a lab, but not in a space with 784 dimensions. So let's derive the coefficients once and for all in R2\mathbb{R}^2. We want numbers aa and bb with

a[u1u2]+b[v1v2]=[t1t2].a\begin{bmatrix} u_1 \\ u_2 \end{bmatrix} + b\begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \begin{bmatrix} t_1 \\ t_2 \end{bmatrix}.
  1. 1

    One equation per component

    Two vectors are equal when every component is equal, so the vector equation is two ordinary equations:

    au1+bv1=t1,au2+bv2=t2.a u_1 + b v_1 = t_1, \qquad a u_2 + b v_2 = t_2 .
  2. 2

    Eliminate b

    Multiply the first equation by v2v_2 and the second by v1v_1:

    au1v2+bv1v2=t1v2,au2v1+bv1v2=t2v1.a u_1 v_2 + b v_1 v_2 = t_1 v_2, \qquad a u_2 v_1 + b v_1 v_2 = t_2 v_1 .

    Subtract the second from the first. The bb terms cancel, leaving

    a (u1v2−u2v1)=t1v2−t2v1.a\,(u_1 v_2 - u_2 v_1) = t_1 v_2 - t_2 v_1 .
  3. 3

    Eliminate a

    Now multiply the first equation by u2u_2 and the second by u1u_1, and subtract the first from the second. This time the aa terms cancel:

    b (u1v2−u2v1)=u1t2−u2t1.b\,(u_1 v_2 - u_2 v_1) = u_1 t_2 - u_2 t_1 .
  4. 4

    Divide, if you can

    Both results contain the same number, D=u1v2−u2v1D = u_1 v_2 - u_2 v_1. If D≠0D \neq 0, divide by it:

    a=t1v2−t2v1D,b=u1t2−u2t1D.a = \frac{t_1 v_2 - t_2 v_1}{D}, \qquad b = \frac{u_1 t_2 - u_2 t_1}{D}.

Check it on u=(2,1)\mathbf{u} = (2, 1), v=(1,3)\mathbf{v} = (1, 3) and the target (4,7)(4, 7). Here D=2⋅3−1⋅1=5D = 2 \cdot 3 - 1 \cdot 1 = 5, so a=(4⋅3−7⋅1)/5=1a = (4 \cdot 3 - 7 \cdot 1)/5 = 1 and b=(2⋅7−1⋅4)/5=2b = (2 \cdot 7 - 1 \cdot 4)/5 = 2. Indeed, u+2v=(2+2,1+6)=(4,7)\mathbf{u} + 2\mathbf{v} = (2 + 2, 1 + 6) = (4, 7).

And when is D=0D = 0? You can check that it happens exactly when one vector is a multiple of the other, so that both lie on one line through the origin. Then every combination stays on that line, and targets off it can't be reached: the lab showed you this. Hold on to the number DD. It returns in Chamber 5 as the determinant, where it measures the area of the parallelogram that u\mathbf{u} and v\mathbf{v} span.

Learn: the standard basis

Two vectors in R2\mathbb{R}^2 deserve names of their own:

e1=[10],e2=[01].\mathbf{e}_1 = \begin{bmatrix} 1 \\ 0 \end{bmatrix}, \qquad \mathbf{e}_2 = \begin{bmatrix} 0 \\ 1 \end{bmatrix}.

They are one step along each axis. Every vector is a combination of them, and the coefficients are simply its components:

[3−2]=3[10]−2[01]=3e1−2e2.\begin{bmatrix} 3 \\ -2 \end{bmatrix} = 3\begin{bmatrix} 1 \\ 0 \end{bmatrix} - 2\begin{bmatrix} 0 \\ 1 \end{bmatrix} = 3\mathbf{e}_1 - 2\mathbf{e}_2 .

Rn\mathbb{R}^n has nn of them: ei\mathbf{e}_i has a 1 in position ii and 0 everywhere else. Together they're called the standard basis (Chamber 3 explains what a basis is in general), and they reveal what the list view of a vector really is:

x=∑i=1nxi ei.\mathbf{x} = \sum_{i=1}^{n} x_i\,\mathbf{e}_i .

The components of x\mathbf{x} are its coefficients in the standard basis. That deserves a proof, because it's really two promises.

Claim. Every vector x∈Rn\mathbf{x} \in \mathbb{R}^n can be written as a linear combination of e1,…,en\mathbf{e}_1, \ldots, \mathbf{e}_n, and in exactly one way.

We need one fact about the basis vectors. The jj-th entry of ei\mathbf{e}_i is the Kronecker delta δij\delta_{ij} from Chamber 4 of the first course: 1 if i=ji = j, and 0 otherwise.

Proof. Existence. Use the components of x\mathbf{x} as the coefficients. The jj-th entry of ∑ixi ei\sum_i x_i\,\mathbf{e}_i is ∑ixi δij\sum_i x_i\,\delta_{ij}. Every term of that sum is zero except the one with i=ji = j, so the jj-th entry is xjx_j. That holds for every jj, so ∑ixi ei=x\sum_i x_i\,\mathbf{e}_i = \mathbf{x}.

Uniqueness. Now suppose some numbers c1,…,cnc_1, \ldots, c_n also work: x=∑ici ei\mathbf{x} = \sum_i c_i\,\mathbf{e}_i. By the same calculation, the jj-th entry of the right-hand side is cjc_j. Two vectors are equal only when all their entries are equal, so cj=xjc_j = x_j for every jj. Any combination that works uses exactly the coefficients we already had. ■\blacksquare

Notice the shape of the argument. “There is one” and “there is only one” are separate claims, and each needs its own proof. Uniqueness proofs nearly always go like the second half: suppose there's another answer, then show it's forced to equal the first.

Quick check +20 XP

Which combination of standard basis vectors equals (4,0,−7)∈R3(4, 0, -7) \in \mathbb{R}^3?

Learn: the rules of the game

Everything you've done with vectors rests on a short list of rules. For all vectors u\mathbf{u}, v\mathbf{v}, w\mathbf{w} and scalars aa, bb:

RuleIn symbols
Order doesn't matteru+v=v+u\mathbf{u} + \mathbf{v} = \mathbf{v} + \mathbf{u}
Grouping doesn't matter(u+v)+w=u+(v+w)(\mathbf{u} + \mathbf{v}) + \mathbf{w} = \mathbf{u} + (\mathbf{v} + \mathbf{w})
Zero changes nothingv+0=v\mathbf{v} + \mathbf{0} = \mathbf{v}
Every vector has a negativev+(−v)=0\mathbf{v} + (-\mathbf{v}) = \mathbf{0}
Scaling by one changes nothing1 v=v1\,\mathbf{v} = \mathbf{v}
Scaling twice multiplies the scalarsa(b v)=(ab) va(b\,\mathbf{v}) = (ab)\,\mathbf{v}
A scalar spreads over a sum of vectorsa(u+v)=au+ava(\mathbf{u} + \mathbf{v}) = a\mathbf{u} + a\mathbf{v}
A vector spreads over a sum of scalars(a+b)v=av+bv(a + b)\mathbf{v} = a\mathbf{v} + b\mathbf{v}

For lists of numbers, each rule follows from the matching rule for ordinary numbers, applied one entry at a time. For example, the ii-th entry of a(u+v)a(\mathbf{u} + \mathbf{v}) is a(ui+vi)=aui+avia(u_i + v_i) = a u_i + a v_i, which is the ii-th entry of au+ava\mathbf{u} + a\mathbf{v}.

Now the mathematician's move. Any collection of objects that can be added and scaled while obeying these eight rules is called a vector space, and every theorem proved from the rules alone holds in all of them at once. Polynomials qualify: you can add two polynomials and scale one, and the rules hold. So do sound recordings and images, added sample by sample or pixel by pixel. That's why one body of theory serves so many kinds of data. You'll use the rules, lightly, in this chamber's proofs.

Learn: data as vectors

Machine learning's first move, almost always, is to turn data into vectors, so that everything above applies.

  • Images. A greyscale MNIST digit is 28 by 28 pixels, each a brightness value. Read the pixels row by row into one list and the image becomes a vector in R784\mathbb{R}^{784}. Brightening an image scales it, blending two images is a linear combination, and the “average digit” is the combination with every coefficient equal to 1n\tfrac{1}{n}.
  • Ratings. If a streaming service has 10,000 films, a user can be a vector in R10000\mathbb{R}^{10000}: their rating of each film. Chamber 2 shows how a recommender system learns a much shorter vector for every user and every film, and predicts ratings from them.
  • Features. A house can be (floor area,bedrooms,age)∈R3(\text{floor area}, \text{bedrooms}, \text{age}) \in \mathbb{R}^3. A table of data is a stack of such feature vectors, one per row.
  • Words. A word has no natural numbers attached, so we learn some. An embedding gives each word a vector, and training adjusts the vectors until words used in similar ways end up with similar vectors. The pre-trained vectors released with Google's word2vec tool give each of 3 million words and phrases a vector of 300 numbers.
Quick check +20 XP

Each image in the CIFAR-10 dataset is 32 pixels wide and 32 pixels tall, with three numbers per pixel (red, green and blue). Flattened into one long list, an image is a vector in Rn\mathbb{R}^n. What is nn?

Now the analogy from the start of the chamber makes sense. Suppose the step from “man” to “woman” is roughly the same wherever you take it, so that

xqueen−xking≈xwoman−xman.\mathbf{x}_{\text{queen}} - \mathbf{x}_{\text{king}} \approx \mathbf{x}_{\text{woman}} - \mathbf{x}_{\text{man}} .

Add xking\mathbf{x}_{\text{king}} to both sides and you get xqueen≈xking−xman+xwoman\mathbf{x}_{\text{queen}} \approx \mathbf{x}_{\text{king}} - \mathbf{x}_{\text{man}} + \mathbf{x}_{\text{woman}}, which is exactly y=xb−xa+xcy = x_b - x_a + x_c. The four words sit at the corners of a rough parallelogram. The paper's first example is grammatical rather than royal: xapple−xapples≈xcar−xcarsx_{\text{apple}} - x_{\text{apples}} \approx x_{\text{car}} - x_{\text{cars}}, one offset for “singular to plural”.

DiscoverLearnRead beyondPapers & lecturesYour turn

Read beyond the course

Each of these takes the ideas of this chamber a little further, and each is free.

Book · free online · ~30 min

Mathematics for Machine Learning

Marc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Chapter 2: opening pages (17–19) and Section 2.4.2, Vector Spaces

The book you met in the first course. Chapter 2 opens by asking what a vector is, and answers with arrows, polynomials, audio signals and lists of numbers. Section 2.4.2 states the vector-space rules formally. Skip Section 2.4.1 on groups for now.

Interactive · free online · ~30 min

Immersive Linear Algebra

J. Ström, K. Åström & T. Akenine-Möller · Chapter 2: Vectors, Sections 2.1–2.5

A whole textbook with interactive figures: drag the vectors in the pictures and watch sums, scalings and coordinates change. It covers this chamber from a slightly different angle, with bases and coordinates in Section 2.5.

Book · free online · ~40 min

Introduction to Applied Linear Algebra: Vectors, Matrices, and Least Squares

Stephen Boyd & Lieven Vandenberghe · Chapter 1, Sections 1.1–1.3

A book built around applications. Its first chapter is full of data as vectors: word counts, images, customer purchases, time series. Read it to see how many things this chamber's operations already describe.

Article · free online · ~25 min

The Illustrated Word2vec

Jay Alammar

A gentle, heavily illustrated walk from “describe a person with a few numbers” to real word embeddings, including king − man + woman on real vectors. The second half explains how the vectors are trained, which is the equation decoded below.

DiscoverLearnRead beyondPapers & lecturesYour turn

Papers and lectures

The paper from the start of this chamber is short (six pages) and more readable than most. Read the abstract, then the paragraph of Section 1 that begins “In this work”, with the apples and cars. Then read Section 5, “The Vector Offset Method”, which is half a column long, and look at Figure 2, which draws the offsets as arrows. Skip Section 3, which describes the recurrent neural network that produced the vectors, and glance at Table 2 only to see that the network's longer vectors answered more analogies correctly. Section 5 contains one formula you can't read yet, a cosine similarity. You'll meet it in Chamber 2.

Linguistic Regularities in Continuous Space Word RepresentationsTomas Mikolov, Wen-tau Yih, Geoffrey Zweig · NAACL-HLT, 2013

The paper that showed relationships between words become constant offsets between their vectors, and made king − man + woman ≈ queen famous.

Later that year, Mikolov and colleagues at Google published a faster way to train word vectors, the word2vec method. Its Equation 2 shows where the vectors come from. Read Section 1 and the first page of Section 2, up to and including Equation 2. The rest of Section 2 (hierarchical softmax and negative sampling) is engineering you can come back to.

Distributed Representations of Words and Phrases and their CompositionalityTomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, Jeffrey Dean · NeurIPS, 2013

The word2vec paper. Its largest model was trained on about 33 billion words of text, and word2vec vectors became a standard starting point for language tasks for years, until models such as BERT learned vectors that change with context.

Decode the paper · Eq. (2), Section 2

Distributed Representations of Words and Phrases and their Compositionality

Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, Jeffrey Dean · NeurIPS, 2013

+30 XP
p(wO∣wI)=exp⁡(vwO′⊤vwI)∑w=1Wexp⁡(vw′⊤vwI)p(w_O|w_I) = \frac{\exp\left({v'_{w_O}}^{\top} v_{w_I}\right)}{\sum_{w=1}^{W} \exp\left({v'_w}^{\top} v_{w_I}\right)}

The skip-gram model learns word vectors by predicting, for each word in a text, the words that appear near it. Equation 2 gives the probability that word wOw_O appears near word wIw_I. Each word gets two vectors: an “input” vector vwv_w and an “output” vector vw′v'_w. Match each symbol to its meaning.

wIw_I
wOw_O
vwIv_{w_I}
vwO′v'_{w_O}
vw′⊤vwI{v'_w}^{\top} v_{w_I}
WW
∑w=1W\sum_{w=1}^{W}

Options

Watch

Vectors | Chapter 1, Essence of linear algebra3Blue1Brown · 10 min

Grant Sanderson's Essence of linear algebra is the classic visual introduction to the subject, and this first episode covers this chamber in ten minutes. Watch for his three perspectives on what a vector is (the physics student's, the computer science student's and the mathematician's), and for how he pictures addition and scaling as movements.

DiscoverLearnRead beyondPapers & lecturesYour turn

Your turn

If you haven't cleared the lab's challenge yet, it's waiting in the Learn section. Here, match arrows to lists and maths to code, assemble a proof and write one, then solve three problems in code.

Match · Arrow ↔ Vector

Arrows and lists

+25 XP

Match each description of an arrow to its list of components.

Three steps right, one step up
The arrow from (1,1)(1, 1) to (4,−1)(4, -1)
(3,1)(3, 1) stretched to twice its length
(3,1)(3, 1) turned to face the opposite way
(3,1)(3, 1) followed, tip to tail, by (−1,2)(-1, 2)
e2\mathbf{e}_2 in R2\mathbb{R}^2
Staying where you are

Options

Match · Maths ↔ NumPy

From symbols to NumPy

+25 XP

NumPy arrays add and scale entry by entry, exactly like vectors. Match each expression to the NumPy that computes it. Assume u, v are 1D arrays, V holds one vector per row, and remember that Python counts from 0.

u+v\mathbf{u} + \mathbf{v}
3v3\mathbf{v}
u−v\mathbf{u} - \mathbf{v}
2u−v2\mathbf{u} - \mathbf{v}
e2∈R4\mathbf{e}_2 \in \mathbb{R}^4
0∈R5\mathbf{0} \in \mathbb{R}^5
∑i=13civi\sum_{i=1}^{3} c_i \mathbf{v}_i

Options

Proofs

The first proof shows that combining combinations never gets you anything new, an idea Chamber 3 builds on. The second is a classic of geometry that vectors make short.

Proof puzzle

Combinations of combinations

+25 XP

Claim

If w\mathbf{w} and z\mathbf{z} are both linear combinations of u\mathbf{u} and v\mathbf{v}, then so is w+z\mathbf{w} + \mathbf{z}.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

The diagonals of a parallelogram

+40 XP

Claim

The two diagonals of any parallelogram cut each other in half.

Put one corner at the origin, so the corners are 0\mathbf{0}, u\mathbf{u}, u+v\mathbf{u} + \mathbf{v} and v\mathbf{v}. You may use that the midpoint of the segment from point p\mathbf{p} to point q\mathbf{q} is p+12(q−p)\mathbf{p} + \tfrac{1}{2}(\mathbf{q} - \mathbf{p}): start at p\mathbf{p} and go half of the way to q\mathbf{q}.

Preview

Your typeset proof appears here.

Code it up

Each problem has exactly one answer. Write a few lines of Python, on your own machine or in the runner under each problem, and submit the number. The second applies the vector offset method from the paper; the third rewards the uniqueness proof from this chamber.

Problem 1·Warm-up

Back to the entrance

+20 XP

Theseus enters the labyrinth at the origin of R2\mathbb{R}^2 and makes 1000 moves. Move kk, for k=1,2,…,1000k = 1, 2, \ldots, 1000, is the vector

dk=((k mod 3)−1, (k mod 7)−3).\mathbf{d}_k = \big((k \bmod 3) - 1,\ (k \bmod 7) - 3\big).

Moves add tip to tail, so after nn moves he stands at

pn=d1+d2+⋯+dn=∑k=1ndk.\mathbf{p}_n = \mathbf{d}_1 + \mathbf{d}_2 + \cdots + \mathbf{d}_n = \sum_{k=1}^{n} \mathbf{d}_k.

For how many nn in {1,2,…,1000}\{1, 2, \ldots, 1000\} is he back at the entrance, that is, pn=0\mathbf{p}_n = \mathbf{0}?

An exact integer (or a fraction like 7/12)

Problem 2·Standard

Analogies by parallelogram

+35 XP

In the vector offset method of Mikolov, Yih and Zweig (2013), the analogy “aa is to bb as cc is to ?” is answered by computing y=xb−xa+xc\mathbf{y} = \mathbf{x}_b - \mathbf{x}_a + \mathbf{x}_c and looking for the word whose vector is closest to y\mathbf{y}. In a toy vocabulary with whole-number vectors we can look for exact hits instead.

The vocabulary has 50 words, numbered 11 to 5050. Word ii has the embedding

xi=(i mod 5, i mod 7, i mod 9)∈R3,\mathbf{x}_i = (i \bmod 5,\ i \bmod 7,\ i \bmod 9) \in \mathbb{R}^3,

and all 50 embeddings are different. Count the ordered triples (a,b,c)(a, b, c) of three different words for which xb−xa+xc\mathbf{x}_b - \mathbf{x}_a + \mathbf{x}_c is exactly the embedding of some word dd, where dd is different from aa, bb and cc.

An exact integer (or a fraction like 7/12)

Problem 3·Challenge

Counting combinations

+50 XP

Take these seven vectors in R2\mathbb{R}^2:

s1=(1,0), s2=(0,1), s3=(1,1), s4=(1,2), s5=(2,1), s6=(2,3), s7=(3,2).\mathbf{s}_1 = (1, 0),\ \mathbf{s}_2 = (0, 1),\ \mathbf{s}_3 = (1, 1),\ \mathbf{s}_4 = (1, 2),\ \mathbf{s}_5 = (2, 1),\ \mathbf{s}_6 = (2, 3),\ \mathbf{s}_7 = (3, 2).

In how many ways can (40,40)(40, 40) be written as a linear combination

c1s1+c2s2+⋯+c7s7=(40,40)c_1\mathbf{s}_1 + c_2\mathbf{s}_2 + \cdots + c_7\mathbf{s}_7 = (40, 40)

whose coefficients c1,…,c7c_1, \ldots, c_7 are non-negative integers? Two ways count as different if at least one coefficient differs.

For a small example, (2,2)(2, 2) can be written in exactly 5 ways: 2s1+2s22\mathbf{s}_1 + 2\mathbf{s}_2, s1+s2+s3\mathbf{s}_1 + \mathbf{s}_2 + \mathbf{s}_3, 2s32\mathbf{s}_3, s1+s4\mathbf{s}_1 + \mathbf{s}_4 and s2+s5\mathbf{s}_2 + \mathbf{s}_5. Check that your code agrees before trusting it on (40,40)(40, 40).

An exact integer (or a fraction like 7/12)

Key takeaways

  • A vector is an arrow and a list. Coordinates are the bridge: (3,1)(3, 1) means 3 along the first axis and 1 along the second, and the same rules work in Rn\mathbb{R}^n for any nn.
  • Add tip to tail or entry by entry; scale by multiplying every entry. A negative scalar flips the arrow, and u−v\mathbf{u} - \mathbf{v} is the arrow from the tip of v\mathbf{v} to the tip of u\mathbf{u}.
  • A linear combination au+bva\mathbf{u} + b\mathbf{v} only scales and adds. In R2\mathbb{R}^2 you can solve for aa and bb by elimination, unless D=u1v2−u2v1=0D = u_1 v_2 - u_2 v_1 = 0 and the vectors share a line.
  • x=∑ixi ei\mathbf{x} = \sum_i x_i\,\mathbf{e}_i, in exactly one way. Components are coefficients in the standard basis, and one-hot vectors are standard basis vectors.
  • Data becomes vectors: images, ratings, features and learned word embeddings, where relationships can show up as roughly constant offsets.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/8
Question 1 of 8 +20 XP

What are the components of the arrow PQ→\overrightarrow{PQ} from P=(1,2)P = (1, 2) to Q=(4,0)Q = (4, 0)?

Question 2 of 8 +20 XP

Let u=(2,−1)\mathbf{u} = (2, -1) and v=(1,4)\mathbf{v} = (1, 4). What is the second component of 3u−2v3\mathbf{u} - 2\mathbf{v}?

Question 3 of 8 +20 XP

Compared with v\mathbf{v}, the vector −2v-2\mathbf{v} is…

Question 4 of 8 +20 XP

Find the numbers aa and bb with a (1,2)+b (3,1)=(7,9)a\,(1, 2) + b\,(3, 1) = (7, 9). What is aa?

Question 5 of 8 +20 XP

Which of these vectors is not a linear combination of (1,1)(1, 1) and (2,2)(2, 2)?

Question 6 of 8 +20 XP

Mikolov, Yih and Zweig (2013) found that relationships appear as “constant vector offsets”. What does that mean?

Question 7 of 8 +20 XP

Which expression is undefined?

Question 8 of 8 +20 XP

Why can't (3,5)(3, 5) be written as a e1+b e2a\,\mathbf{e}_1 + b\,\mathbf{e}_2 in two different ways?

End of the chamber

Clear this chamber

+50 XPVectorLinear CombinationStandard BasisEmbedding