Vector Spaces and Subspaces
A vector space is any collection of objects you can add and scale without leaving the collection. Arrows in , polynomials, images and even functions qualify. ML cares because the set of things a model can produce (predictions of a linear model, outputs of a layer, the solutions of ) is very often a subspace, and subspaces have a dimension you can compute.
The definition in one breath
A real vector space is a set with an addition and a scalar multiplication that obey the familiar rules: addition is associative and commutative, there is a zero vector and negatives , and scaling distributes:
Examples: ; all matrices ; polynomials of degree at most 3; all grayscale images (adding two images pixel by pixel gives an image). The full axiomatic treatment is in Vector Spaces.
Subspaces
A subset is a subspace when it contains and is closed under addition and scaling: . Geometrically, subspaces of are the origin, lines and planes through the origin, and itself.
Two subspaces appear in every ML problem with a data matrix :
| Subspace | Definition | Meaning |
|---|---|---|
| column space (image) | all | every output a linear model can produce |
| null space (kernel) | all with | parameter changes that leave every output unchanged |
import numpy as np
A = np.array([[1.0, 2.0, 3.0],
[2.0, 4.0, 6.0],
[1.0, 0.0, 1.0]])
# two vectors in the null space
n1 = np.array([-1.0, -1.0, 1.0])
print("A n1 =", A @ n1)
# closure: any combination stays in the null space
x = 2.5 * n1 - 4.0 * n1
print("A (combination) =", A @ x)
# the image: every output A x has y2 = 2 y1
rng = np.random.default_rng(0)
for _ in range(3):
y = A @ rng.normal(size=3)
print("output", np.round(y, 3), " y2 - 2 y1 =", round(float(y[1] - 2 * y[0]), 10))A n1 = [0. 0. 0.]
A (combination) = [0. 0. 0.]
output [1.783 3.566 0.766] y2 - 2 y1 = 0.0
output [0.118 0.237 0.466] y2 - 2 y1 = 0.0
output [1.087 2.174 0.6 ] y2 - 2 y1 = 0.0
The image here is a plane in (all outputs satisfy ), so no input can produce, say, .
Not every set is a subspace
The solution set of with is not a subspace: it does not contain , and the sum of two solutions solves . It is an affine subspace, a shifted copy of the null space. Likewise, the set of probability vectors (non-negative, summing to 1) is not a subspace, because scaling by leaves it.
import numpy as np
A = np.array([[1.0, 1.0]])
b = np.array([2.0])
s1, s2 = np.array([2.0, 0.0]), np.array([0.5, 1.5]) # both solve x1 + x2 = 2
print("A s1 =", A @ s1, " A s2 =", A @ s2, " A (s1 + s2) =", A @ (s1 + s2))
print("contains zero?", np.allclose(A @ np.zeros(2), b))A s1 = [2.] A s2 = [2.] A (s1 + s2) = [4.]
contains zero? False
In software
- Word embeddings live in and analogies are vector arithmetic (), which only makes sense because embeddings form a vector space.
- A linear layer with weight matrix can only output vectors in ; a bottleneck layer of width restricts the next layer's inputs to a subspace of dimension at most .
- Low-rank adapters (LoRA) restrict weight updates to a small subspace on purpose.
Exercises
Exercise 1: Subspace or not?
Which are subspaces of ? (a) the line ; (b) the line ; (c) the first quadrant .
Answer
Only (a).
Solution
(a) contains and is closed: if and then . (b) misses . (c) contains but not .
Exercise 2: Polynomials
Is the set of polynomials of degree at most 2 with a subspace?
Answer
Yes.
Solution
The zero polynomial has . If , then , and the degree stays at most 2. (With the condition it would fail.)
Exercise 3: Find a null vector (code)
Find a nonzero vector in the null space of and confirm it.
Answer
(or any multiple).
Solution
From the equations and ; take .
import numpy as np
A = np.array([[1.0, 1, 0], [0, 1, 1]])
print(A @ np.array([1.0, -1, 1]))[0. 0.]
Test yourself
Which of these is always a subspace of ?
- The solution set of with
- The null space of
- The unit sphere
- The set of vectors with positive entries
Every subspace must contain…
- the vector of all ones
- the zero vector
- a unit vector
- at least two vectors
The column space of a weight matrix is…
- the set of inputs that give zero output
- the set of all outputs the layer can produce
- the diagonal of
- the rows of