Vectors: arrows and arrays
A vector is a list of numbers, and for two numbers it is also an arrow. Adding and scaling look the same either way, and they are all a learning step does.
1. The idea
A vector is a list of numbers, π± = (xβ, xβ, β¦, x_d). That is all a computer sees: a 1-D array. With two or three numbers there is also a picture, an arrow from the origin to the point with those coordinates. The picture is what makes the operations make sense; the list is what lets them work in 768 dimensions, where nobody can draw.
Adding two vectors adds matching entries. As arrows, put the tail of π at the tip of π: where you end up is π + π. Multiplying by a single number c (a scalar) multiplies every entry: the arrow keeps its line, gets c times as long, and turns round if c is negative.
In machine learning nearly everything is a vector: one example's features, a word's embedding, a layer's output, the gradient. Adding and scaling them is what a training step does: ΞΈ β Ξ·βL takes the parameter vector and adds a scaled copy of the gradient vector.
2. Try it: add and scale arrows
Drag the tips of π and π (or focus one and use the arrow keys). The dashed copy of π starts at the tip of π, and π + π ends where it does. The slider scales π by c; try a negative c.
3. The math
4. The code (python)
import numpy as np
a = np.array([2.0, 1.0])
b = np.array([0.5, 2.0])
print(a + b) # [2.5 3. ]
print(1.5 * a) # [3. 1.5]
print(-1 * a) # [-2. -1.]
theta = np.array([0.5, -1.0, 2.0]) # parameters
grad = np.array([1.0, -2.0, 0.5]) # gradient of the loss
eta = 0.1
print(theta - eta * grad) # [ 0.4 -0.8 1.95]5. Youβll see this in
- Gradient descent, read with the seven questions a vector minus a scaled vector
- Core Course chapter 6: arrays and batches
- Reference notes: linear algebra vector spaces, more formally
6. Check yourself
Finished this chapter?
The Epoch Zero newsletter
What changed in ML architectures this week: new models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.