Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

Vector operations

P1.B225 min●○○ BeginnerReviewed Nov 30, 2026

Why this matters for machine learning

Adding, scaling and combining vectors is how a model learns and how it mixes information: nearly every update and every layer is built from these moves.

  • Every gradient-descent step is a vector operation: scale the gradient by the learning rate, then subtract it from the parameters. (P1.F3: Gradient descent)
  • Word vectors can be added, subtracted and averaged, which is how famous examples such as king − man + woman are explored. (P6.4: Exploring embedding space, coming soon)
  • A transformer layer does not replace its input: a residual connection adds the layer's output back onto it. (P7.7: The transformer block, coming soon)
  • Attention builds each output as a weighted sum of vectors, which is a linear combination. (P7.3: Queries, keys and values step by step, coming soon)

Before this: Scalars, vectors and spaces

After this lesson you can

  • Add and subtract vectors entry by entry and as tip-to-tail arrows, and say when the operation is not defined
  • Scale a vector and say what a factor above 1, between 0 and 1, below 0 and equal to 0 does
  • Use the rules (order and grouping do not matter, scaling spreads over a sum) to simplify an expression
  • Build and read a linear combination, and compute an average and an interpolation of vectors
  • Do all of this in NumPy, and avoid the Python-list trap

You walk 3 blocks east and 1 block north. Then you walk 1 block east and 2 blocks north. Where are you now, compared with the start? Add the east parts, 3+1=43+1=4, and the north parts, 1+2=31+2=3: you are 4 blocks east and 3 blocks north of where you began. Walking one trip after another is adding vectors. This lesson turns that everyday idea into the small set of moves that machine learning is built from.

Adding vectors

A trip is a vector: a=(3,1)\mathbf{a} = (3, 1) means “3 right, 1 up”. A second trip is b=(1,2)\mathbf{b} = (1, 2). Doing a\mathbf{a} and then b\mathbf{b} gives the total trip a+b\mathbf{a}+\mathbf{b}. Add the matching entries:

a+b=(3,1)+(1,2)=(3+1, 1+2)=(4,3)\begin{aligned} \mathbf{a}+\mathbf{b} &= (3, 1) + (1, 2) \\ &= (3+1,\ 1+2) = (4, 3) \end{aligned}

There are two pictures of the same sum:

  • Tip to tail. Draw a\mathbf{a} from the origin. Then draw b\mathbf{b} starting at the tip of a\mathbf{a} (an arrow can start anywhere; P1.B1). The sum is the arrow from the very start to the very end.
  • Parallelogram. Draw a\mathbf{a} and b\mathbf{b} both from the origin. Complete the parallelogram. The sum is the diagonal from the origin.

Both vectors must have the same number of entries. (1,2)+(3,4,5)(1, 2) + (3, 4, 5) is not defined: one of the vectors has an entry with nothing to pair it with. (You will meet the same rule again in the dot product, P1.B3.) You can only add two vectors from the same space, for example two vectors in R2\mathbb{R}^2.

Drag the tips of a and b, then change c and d

−4−224−4−224aba + b
a = (3, 1), b = (1, 2)
entry 1: 1×3 + 1×1 = 4
entry 2: 1×1 + 1×2 = 3
a + b = (4, 3)

Tip to tail: walk along a, then along b. You end at a + b. Switch on the parallelogram to see that b first, then a, ends in the same place.

Thin arrows are a and b. The thick arrows are c·a followed by d·b, tip to tail. The black arrow is the result, from the origin to where you end up.

Drag the tips of a and b (or Tab to a tip and use the arrow keys). The thin arrows are a and b. The thick arrows are a followed by b, tip to tail, and the black arrow is the sum. Switch on 'Show the other order' to see the parallelogram. Then try the sliders: c scales a and d scales b.

With the sliders at c=1c=1 and d=1d=1 the widget shows the plain sum a+b\mathbf{a}+\mathbf{b}. Notice two things. The black arrow is the same whichever order you walk the two legs (switch on the other order). And its two entries are always the sums of the two entries of a\mathbf{a} and b\mathbf{b}, as the readout shows.

Scaling a vector

To scale a vector, multiply every entry by the same number. A single number like this is a scalar (P1.B1), and the name fits the job: a scalar scales. For a=(3,1)\mathbf{a}=(3, 1):

2a=(6,2)12a=(1.5, 0.5)−a=(−3,−1)0 a=(0,0)\begin{aligned} 2\mathbf{a} &= (6, 2) & \tfrac{1}{2}\mathbf{a} &= (1.5,\ 0.5) \\ -\mathbf{a} &= (-3, -1) & 0\,\mathbf{a} &= (0, 0) \end{aligned}

What the number cc does to the arrow c ac\,\mathbf{a}:

Scalar ccEffect on the arrow
c>1c > 1stretches it, same direction
c=1c = 1no change
0<c<1{0 < c < 1}shrinks it, same direction
c=0c = 0the zero vector (a dot with no direction; P1.B1)
c<0c < 0flips it to point the opposite way, and also stretches it if c<−1c<-1 or shrinks it if −1<c<0-1<c<0

So −2a-2\mathbf{a} is twice as long as a\mathbf{a} and points backwards: (−6,−2)(-6, -2). All the scalings c ac\,\mathbf{a} of one nonzero vector lie on a single line through the origin, the line along a\mathbf{a} (for the zero vector they are all just the origin). (The length of c ac\,\mathbf{a} is ∣c∣|c| times the length of a\mathbf{a}; P1.B4 studies lengths.)

Try it in the widget: set d=0d=0 and slide cc. The thick blue arrow stretches, shrinks and flips, and the sum is just c ac\,\mathbf{a}.

Subtracting vectors

Subtraction is adding the negative: a−b=a+(−b)\mathbf{a}-\mathbf{b} = \mathbf{a}+(-\mathbf{b}), where −b-\mathbf{b} is b\mathbf{b} flipped. Entry by entry:

a−b=(3,1)−(1,2)=(3−1, 1−2)=(2,−1)\begin{aligned} \mathbf{a}-\mathbf{b} &= (3, 1) - (1, 2) \\ &= (3-1,\ 1-2) = (2, -1) \end{aligned}

There is a second, very useful picture. Draw a\mathbf{a} and b\mathbf{b} from the same start. a−b\mathbf{a}-\mathbf{b} is the arrow from the tip of b\mathbf{b} to the tip of a\mathbf{a}. Check: start at b\mathbf{b} and walk a−b\mathbf{a}-\mathbf{b}, and you arrive at b+(a−b)=a\mathbf{b}+(\mathbf{a}-\mathbf{b}) = \mathbf{a}. With numbers: (1,2)+(2,−1)=(3,1)(1, 2)+(2, -1) = (3, 1). This is why P1.B1 found the move from point AA to point BB as B−AB-A: “destination minus start”.

Order matters here, unlike in addition: b−a=(−2,1)\mathbf{b}-\mathbf{a} = (-2, 1) is the flipped arrow, −(a−b)-(\mathbf{a}-\mathbf{b}). In the widget, press a − b and tick “Show the arrow from b to a”.

The three depths

Intuition

Think of trips. Adding vectors is doing one trip after another; the total is the one trip that has the same effect. Scaling is doing a trip twice as far, half as far, or in reverse. Subtracting b\mathbf{b} is “undo trip b\mathbf{b}”.

That is why the rules feel obvious: the order of two trips doesn’t change where you end up (on an open field with no one-way streets), and doing a trip and then undoing it gets you back to the start.

The analogy has a weak spot: you can add two trips, but not two places. “Paris plus Berlin” means nothing, while “Paris to Berlin” is a perfectly good trip. In Rn\mathbb{R}^n we can treat points and trips as the same thing because there is an origin to measure from (P1.B1), but the idea that matters is the trip.

Math

For vectors a=(a1,…,an)\mathbf{a}=(a_1,\dots,a_n) and b=(b1,…,bn)\mathbf{b}=(b_1,\dots,b_n) in Rn\mathbb{R}^n and a scalar cc:

a+b=(a1+b1, …, an+bn)c a=(c a1, …, c an)a−b=a+(−1) b=(a1−b1, …, an−bn)\begin{aligned} \mathbf{a}+\mathbf{b} &= (a_1+b_1,\ \dots,\ a_n+b_n) \\ c\,\mathbf{a} &= (c\,a_1,\ \dots,\ c\,a_n) \\ \mathbf{a}-\mathbf{b} &= \mathbf{a}+(-1)\,\mathbf{b} \\ &= (a_1-b_1,\ \dots,\ a_n-b_n) \end{aligned}

Addition needs a\mathbf{a} and b\mathbf{b} to have the same nn. Scaling works for any vector. In an expression like c a+bc\,\mathbf{a}+\mathbf{b}, the scaling happens first, as with ordinary numbers.

The result of each operation is again a vector in Rn\mathbb{R}^n. So adding and scaling never take you out of the space, which is why “the space of all vectors with nn entries” is a good home for them.

Code

import numpy as np

a = np.array([3, 1])
b = np.array([1, 2])

a + b      # array([4, 3])
2 * a      # array([6, 2])
a - b      # array([ 2, -1])
-b         # array([-1, -2])

NumPy does each operation entry by entry, exactly like the formulas. There is a runnable cell with the pitfalls further down.

The rules, and why they hold

Vector operations obey a few rules. You don’t need to memorise them as a list. Each one holds for a simple reason: every entry is an ordinary number, and ordinary numbers already obey these rules. Here they are in plain words, with a check on a=(3,1)\mathbf{a}=(3,1), b=(1,2)\mathbf{b}=(1,2).

  • Order doesn’t matter for addition: a+b=b+a\mathbf{a}+\mathbf{b}=\mathbf{b}+\mathbf{a}. (Two routes round the parallelogram reach the same corner.)
  • Grouping doesn’t matter: (a+b)+c=a+(b+c)(\mathbf{a}+\mathbf{b})+\mathbf{c}=\mathbf{a}+(\mathbf{b}+\mathbf{c}), so we can write a+b+c\mathbf{a}+\mathbf{b}+\mathbf{c} with no brackets.
  • The zero vector does nothing: a+0=a\mathbf{a}+\mathbf{0}=\mathbf{a}.
  • Every vector can be undone: a+(−a)=0\mathbf{a}+(-\mathbf{a})=\mathbf{0}. (Walk there and back.)
  • Scaling spreads over a sum: c(a+b)=c a+c bc(\mathbf{a}+\mathbf{b})=c\,\mathbf{a}+c\,\mathbf{b}. Check with c=2c=2: 2(4,3)=(8,6)2(4,3)=(8,6) and (6,2)+(2,4)=(8,6)(6,2)+(2,4)=(8,6). (Doubling a whole trip is doubling each leg.)
  • Scalars can be added first: (c+d) a=c a+d a(c+d)\,\mathbf{a}=c\,\mathbf{a}+d\,\mathbf{a}. Check: 5a=2a+3a5\mathbf{a}=2\mathbf{a}+3\mathbf{a}.
  • Scaling twice multiplies: c(d a)=(cd) ac(d\,\mathbf{a})=(cd)\,\mathbf{a}, and 1 a=a1\,\mathbf{a}=\mathbf{a}.

Three things that are not rules:

  • Subtraction is not order-free: a−b≠b−a\mathbf{a}-\mathbf{b}\neq\mathbf{b}-\mathbf{a} (they are opposite arrows).
  • There is no “vector times vector” yet. Multiplying two vectors can mean several different things. The dot product (P1.B3) gives a number; NumPy’s a * b is a different operation, entry-by-entry multiplication, not part of the add-and-scale toolkit.
  • A scalar plus a vector is not defined in maths: 3+(1,2)3+(1, 2) means nothing. NumPy does accept a + 3 and adds 3 to every entry; that is a convenience called broadcasting (P1.B14), not a vector operation.
Go deeper Anything you can add and scale is a vector

P1.B1 promised this idea. Mathematicians call something a vector if you can add two of them and scale one by a number, and the rules above hold (the formal definition of a vector space lists about eight to ten such rules, depending on how the textbook counts them). Lists of numbers are the main example, but not the only one.

  • Images. Add two same-size images pixel by pixel, and scale one to brighten or dim it. (With real images you also clip to the valid range, but the idea is the same.)
  • Functions. Define (f+g)(x)=f(x)+g(x)(f+g)(x)=f(x)+g(x) and (cf)(x)=c f(x)(c f)(x)=c\,f(x). A function is then a vector, and “the sum of two curves” is a curve.
  • Polynomials. (2+3x)+(1+x)=3+4x(2+3x)+(1+x)=3+4x, and 2(2+3x)=4+6x2(2+3x)=4+6x (also in P1.B1).

Everything in this lesson, including linear combinations below, works for all of these, because it only uses adding and scaling. P1.B7 returns to the formal definition. For machine learning, the main example stays Rn\mathbb{R}^n: a model’s parameters, a data row and an embedding are all lists of numbers you can add and scale.

Linear combinations

Now put the two operations together. A linear combination of a\mathbf{a} and b\mathbf{b} is

c a+d bc\,\mathbf{a} + d\,\mathbf{b}

for any scalars cc and dd (called the coefficients or weights). In words: scale each vector, then add. With more vectors it just continues: c1v1+c2v2+⋯+ckvkc_1\mathbf{v}_1 + c_2\mathbf{v}_2 + \dots + c_k\mathbf{v}_k.

Worked example, with a=(3,1)\mathbf{a}=(3,1) and b=(1,2)\mathbf{b}=(1,2):

2a+3b=(6,2)+(3,6)=(9,8)2a−b=(6,2)−(1,2)=(5,0)\begin{aligned} 2\mathbf{a}+3\mathbf{b} &= (6, 2) + (3, 6) = (9, 8) \\ 2\mathbf{a}-\mathbf{b} &= (6, 2) - (1, 2) = (5, 0) \end{aligned}

You have already met special cases. a+b\mathbf{a}+\mathbf{b} is c=d=1c=d=1. a−b\mathbf{a}-\mathbf{b} is c=1, d=−1c=1,\ d=-1. A plain scaling c ac\,\mathbf{a} is d=0d=0. The zero vector is c=d=0c=d=0. The widget above draws the general case: the blue arrow is c ac\,\mathbf{a}, the pink arrow d bd\,\mathbf{b} starts at its tip, and the black arrow is the result.

Which points can you reach? Drag b\mathbf{b} so that it points along the same line as a\mathbf{a} (for example a=(3,1)\mathbf{a}=(3,1) and b=(−3,−1)\mathbf{b}=(-3,-1)) and move the sliders: the result never leaves that line. When a\mathbf{a} and b\mathbf{b} point in genuinely different directions, you can hit any point of the plane with a suitable cc and dd. (The widget’s sliders stop at −2-2 and 22, so very distant points are out of reach there, but larger cc and dd would get you to them.) Try it as a game:

Reach the target: choose c and d

−8−6−4−22468−8−6−4−22468abtarget
a = (2, 1), b = (−1, 2), target = (3, 4)
entry 1: 0×2 + 0×(−1) = 0
entry 2: 0×1 + 0×2 = 0
target − you = (3, 4)

Not there yet. The dashed line shows what is still missing.

Fixed vectors a = (2, 1) and b = (−1, 2). Choose c and d so that the black dot lands on the target ring. The dashed line shows what is still missing. 'Show a solution' fills in a pair that works.

This question, “which vectors can I make from these?”, is the idea of span, and “do I really need both?” is independence. P1.B7 teaches both properly.

Go deeper Preview: a matrix times a vector is a linear combination

Take the matrix with columns (1,3)(1, 3) and (2,4)(2, 4) and multiply it by x=(2,3)\mathbf{x}=(2, 3). The usual row-times-column recipe (P1.B6) gives (1⋅2+2⋅3, 3⋅2+4⋅3)=(8,18)(1\cdot2+2\cdot3,\ 3\cdot2+4\cdot3)=(8, 18). The same answer comes from a linear combination of the columns, with the entries of x\mathbf{x} as weights:

2(13)+3(24)=(26)+(612)=(818)2\begin{pmatrix}1\\3\end{pmatrix}+3\begin{pmatrix}2\\4\end{pmatrix}=\begin{pmatrix}2\\6\end{pmatrix}+\begin{pmatrix}6\\12\end{pmatrix}=\begin{pmatrix}8\\18\end{pmatrix}

So “a layer applies a matrix to a vector” can be read as “the layer mixes its columns using the input as weights”. P1.B5 and P1.B6 develop this; for now just notice that linear combinations are inside every neural-network layer.

Averages and interpolation

Two of the most useful linear combinations have their own names.

The average (mean) of vectors. Add them up and divide by how many there are. For kk vectors that is a linear combination with every weight equal to 1/k1/k. Take x=(1,2,3)\mathbf{x}=(1,2,3), y=(4,5,6)\mathbf{y}=(4,5,6) and z=(7,8,9)\mathbf{z}=(7,8,9):

13(x+y+z)=13(12,15,18)=(4,5,6)\begin{aligned} \tfrac{1}{3}(\mathbf{x}+\mathbf{y}+\mathbf{z}) &= \tfrac{1}{3}(12, 15, 18) \\ &= (4, 5, 6) \end{aligned}

Entry jj of the mean is the ordinary mean of entry jj of every vector. If each vector is one data example, the mean vector is the “average example”. For two vectors it is the midpoint: 12a+12b\tfrac12\mathbf{a}+\tfrac12\mathbf{b}. (Press Average in the first widget.) A weighted average uses weights that are not negative and add up to 1; they need not be equal (the plain mean is the special case where they are).

Interpolation. To slide smoothly from a\mathbf{a} to b\mathbf{b}, use weights 1−t1-t and tt:

p(t)=(1−t) a+t b\mathbf{p}(t) = (1-t)\,\mathbf{a} + t\,\mathbf{b}

At t=0t=0 you get a\mathbf{a}, at t=1t=1 you get b\mathbf{b}, and at t=0.5t=0.5 the midpoint. The weights always add up to 1. There is a nice second reading using subtraction: (1−t)a+tb=a+t(b−a)(1-t)\mathbf{a}+t\mathbf{b}=\mathbf{a}+t(\mathbf{b}-\mathbf{a}), which says “start at a\mathbf{a} and walk the fraction tt of the arrow b−a\mathbf{b}-\mathbf{a}”. For a=(3,1)\mathbf{a}=(3,1), b=(1,2)\mathbf{b}=(1,2) and t=0.25t=0.25 this gives (2.5, 1.25)(2.5,\ 1.25).

Slide t: the point p = (1 − t)a + tb

−6−4−2246−6−4−2246abp
weights: 1 − t = 0.75 on a, t = 0.25 on b (they add up to 1)
entry 1: 0.75×(−3) + 0.25×3 = −1.5
entry 2: 0.75×(−1) + 0.25×2 = −0.25
p = (−1.5, −0.25)

Between 0 and 1: the result lies on the segment from a to b. It is closer to the one with the bigger weight.

Slide t. The point p = (1 − t)a + t b moves along the straight line through a and b. Between 0 and 1 it is on the segment from a to b; outside that range it goes past one end. This widget uses its own vectors, a = (−3, −1) and b = (3, 2).

For tt between 0 and 1 the point stays on the segment between a\mathbf{a} and b\mathbf{b}. For tt outside that range it continues along the same line, which is called extrapolation.

Where this shows up in machine learning

WhereThe vector operationLesson
A gradient-descent stepθnew=θ−η ∇L\boldsymbol{\theta}_{\text{new}}=\boldsymbol{\theta}-\eta\,\nabla L: scale the gradient by the learning rate η\eta, then subtractP1.F3
One vector for a sentenceA simple baseline is to average the vectors of its wordsP6.3
Blending two training examplesMixup trains on (1−λ)x1+λx2(1-\lambda)\mathbf{x}_1+\lambda\mathbf{x}_2 and mixes the labels the same wayP4.18
Sliding between two latent codesInterpolation between two vectors in a generative modelP8.3
Residual connectionThe block’s output is added to its input: x+f(x)\mathbf{x}+f(\mathbf{x})P7.7
AttentionEach output is a weighted sum (linear combination) of value vectorsP7.3

A concrete gradient step: parameters θ=(2,3)\boldsymbol{\theta}=(2, 3), gradient ∇L=(4,−2)\nabla L=(4, -2) and learning rate η=0.1\eta=0.1. Scale first: η∇L=(0.4, −0.2)\eta\nabla L=(0.4,\ -0.2). Then subtract: θnew=(2−0.4, 3+0.2)=(1.6, 3.2)\boldsymbol{\theta}_{\text{new}}=(2-0.4,\ 3+0.2)=(1.6,\ 3.2). The whole step is one scaling and one subtraction. (The gradient itself is P1.F3’s topic.)

A concrete average, with the made-up toy word vectors from P1.B3: the average of cat=(0.9,0.8,0.1)\text{cat}=(0.9, 0.8, 0.1) and dog=(0.8,0.9,0.2)\text{dog}=(0.8, 0.9, 0.2) is (0.85, 0.85, 0.15)(0.85,\ 0.85,\ 0.15).

A concrete residual connection: if the input is x=(1,2,3)\mathbf{x}=(1, 2, 3) and the block computes f(x)=(0.1,−0.2,0.3)f(\mathbf{x})=(0.1, -0.2, 0.3), the output is x+f(x)=(1.1, 1.8, 3.3)\mathbf{x}+f(\mathbf{x})=(1.1,\ 1.8,\ 3.3). Addition needs matching dimensions, so f(x)f(\mathbf{x}) must have as many entries as x\mathbf{x}; this is one reason the vectors in a transformer keep the same width from layer to layer.

Go deeper Word arithmetic: king − man + woman (famous, and often overstated)

Word embeddings (P6.3) give each word a vector. The famous example is that vking−vman+vwoman\mathbf{v}_{\text{king}}-\mathbf{v}_{\text{man}}+\mathbf{v}_{\text{woman}}, a linear combination with weights 1,−1,11,-1,1, is close to vqueen\mathbf{v}_{\text{queen}}. Mikolov, Yih and Zweig (2013) reported this for word vectors they trained, and it was repeated widely.

Keep three cautions in mind:

  • It was shown for particular trained embeddings. It is not a law of vectors, and how well it works depends on the embedding and on the words.
  • The usual test excludes the three words in the question (king, man, woman) from the possible answers. Nissim, van Noord and van der Goot (2020) point out that without that rule the nearest word can be one of the inputs themselves, which makes the result look less impressive.
  • “Close to” means nearest among many words, not exactly equal.

So treat it as a fun, partly true illustration that directions in embedding space can carry meaning, not as proof that embeddings “understand” analogies. P6.4 explores embedding space with a proper explorer.

Vector operations in code

Try it: vector operations in NumPy

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The error text says the shapes “could not be broadcast together”. “Broadcasting” is the rule NumPy uses to decide whether two shapes can be combined (P1.B14); for two 1-D arrays it simply means they must have the same length, or one of them must have length 1. For the two arrays here, shapes (2,) and (3,), it fails. Use NumPy arrays, not Python lists, for vector maths: with lists, + concatenates and * 2 repeats, which is almost never what you want.

Check your understanding

  1. What is (3,1)+(1,2)(3, 1) + (1, 2)?

  2. Which of these sums is defined?

  3. What is −2a-2\mathbf{a} for a=(3,1)\mathbf{a}=(3, 1)?

  4. For two vectors drawn from the same start, which arrow is a−b\mathbf{a}-\mathbf{b}?

  5. For a=(3,1)\mathbf{a}=(3,1) and b=(1,2)\mathbf{b}=(1,2), what is 2a−b2\mathbf{a}-\mathbf{b}?

Exercises

  1. Compute (2,−1,4)+(1,3,−2)(2, -1, 4) + (1, 3, -2).
    Show answer(2+1, −1+3, 4−2)=(3,2,2)(2+1,\ -1+3,\ 4-2) = (3, 2, 2).
  2. Compute 3(1,−2)−2(0,5)3(1, -2) - 2(0, 5).
    Show answer(3,−6)−(0,10)=(3,−16)(3, -6) - (0, 10) = (3, -16).
  3. For a=(4,2)\mathbf{a}=(4, 2) write 12a\tfrac12\mathbf{a}, −a-\mathbf{a} and 0 a0\,\mathbf{a}, and say what each does to the arrow.
    Show answer(2,1)(2, 1) halves it, (−4,−2)(-4, -2) flips it, (0,0)(0, 0) collapses it to the zero vector.
  4. Find the vector from the point P=(−1,4)P=(-1, 4) to the point Q=(2,0)Q=(2, 0), and the vector from QQ to PP.
    Show answerQ−P=(3,−4)Q-P=(3, -4) and P−Q=(−3,4)P-Q=(-3, 4): opposite arrows. “Destination minus start.”
  5. In the “reach the target” widget, which cc and dd hit the first target, (3,4)(3, 4)? Check by hand.
    Show answerc=2c=2, d=1d=1: 2(2,1)+1(−1,2)=(4,2)+(−1,2)=(3,4)2(2, 1)+1(-1, 2)=(4, 2)+(-1, 2)=(3, 4).
  6. Find the mean of (2,0)(2, 0), (4,6)(4, 6) and (6,3)(6, 3).
    Show answer13(12,9)=(4,3)\tfrac13(12, 9)=(4, 3). Each entry is the mean of that entry: (2+4+6)/3=4(2+4+6)/3=4 and (0+6+3)/3=3(0+6+3)/3=3.
  7. Let a=(0,10)\mathbf{a}=(0, 10) and b=(8,2)\mathbf{b}=(8, 2). Compute (1−t)a+tb(1-t)\mathbf{a}+t\mathbf{b} for t=0.75t=0.75 and for t=−0.5t=-0.5. Which is between a\mathbf{a} and b\mathbf{b}?
    Show answerFor t=0.75t=0.75: 0.25(0,10)+0.75(8,2)=(6,4)0.25(0, 10)+0.75(8, 2)=(6, 4), which is between them. For t=−0.5t=-0.5: 1.5(0,10)−0.5(8,2)=(−4,14)1.5(0, 10)-0.5(8, 2)=(-4, 14), which is past a\mathbf{a} on the far side from b\mathbf{b}.
  8. Do one gradient-descent step for θ=(1,−2)\boldsymbol{\theta}=(1, -2), ∇L=(0.5,−1)\nabla L=(0.5, -1) and η=0.2\eta=0.2.
    Show answerη∇L=(0.1,−0.2)\eta\nabla L=(0.1, -0.2), so θnew=(1−0.1, −2+0.2)=(0.9,−1.8)\boldsymbol{\theta}_{\text{new}}=(1-0.1,\ -2+0.2)=(0.9, -1.8).
  9. A residual connection adds f(x)=(0.5,0.5,0.5,0.5)f(\mathbf{x})=(0.5, 0.5, 0.5, 0.5) to x=(1,2,3)\mathbf{x}=(1, 2, 3). Can it?
    Show answerNo: x\mathbf{x} has 3 entries and f(x)f(\mathbf{x}) has 4, so the sum is not defined. A residual connection needs the block’s output to have the same size as its input.
  10. In the code cell, look at [1, 2] + [3, 4] with plain lists. What do you get, and how do you make + add entry by entry?
    Show answerYou get [1, 2, 3, 4], the lists joined. Convert them with np.array(...) first and + adds entry by entry: [4 6].

Key takeaways

  • Adding vectors works entry by entry: (3, 1) + (1, 2) = (4, 3). Both vectors must have the same number of entries.
  • As arrows, adding is tip to tail: put the start of b at the tip of a. Going a then b, or b then a, ends at the same place (the parallelogram).
  • Scaling multiplies every entry by the same number. A factor above 1 stretches, between 0 and 1 shrinks, below 0 flips the direction, and 0 gives the zero vector.
  • a − b means a + (−b). As an arrow, it runs from the tip of b to the tip of a.
  • The usual rules (a + b = b + a, grouping does not matter, c(a + b) = ca + cb, and so on) hold because each entry is an ordinary number.
  • A linear combination is c·a + d·b (or more terms): scale some vectors, then add. A mean is a linear combination whose weights add to 1, and (1 − t)a + t b slides from a (t = 0) to b (t = 1).
  • In NumPy, a + b, 2 * a and a - b work entry by entry. With plain Python lists, + joins the lists instead.
Found a mistake? Tell us