Math for AI course · Lesson 28 of 180
Subspaces for AI: Test Restricted Feature Families
Learn the subspace test, prove that a plane is closed under vector operations, and use NumPy to inspect lower-dimensional representation directions.
An encoder may produce hundreds of coordinates for every input even when the nearby representations move along only a few coherent directions. To reason about that restriction, we need more than a cluster of points. We need a set that remains valid when its directions are added and scaled.
That set is a vector subspace. By the end of this lesson, you will be able to run the subspace test, prove that a plane through the origin passes it, reject tempting counterexamples, and interpret a lower-dimensional linear model of learned representation offsets.
Learning outcome: Test subspace conditions and interpret a lower-dimensional region.
Lesson 27: Vector spaces define legal representations established the ambient rules for vector addition and scalar multiplication. A subspace keeps those same operations and narrows the allowed vectors. The official Mathematics for Machine Learning companion develops vector subspaces in Section 2.4.3 of the book PDF. The representation scenario, calculations, code, visual, and assessments below are original.
A restriction must survive the operations
Let the ambient vector space be , and let be a candidate set. Calling a subspace means that vectors can stay inside while using the addition and scalar multiplication already defined on . The ambient space supplies the arithmetic; the subset has to prove that it is closed under that arithmetic.
The zero-vector check is fast and decisive. Any subspace must contain zero because scalar closure requires for every . Addition then says that two allowed directions can be combined. Scalar closure says that an allowed direction remains allowed when it is stretched, shrunk, reversed, or collapsed to zero.
The compact linear-combination test packages those requirements together. Set to recover addition, set to recover scaling, and set both coefficients to zero to recover the zero vector. This is the same linear-combination operation introduced in Lesson 17, now used as a membership test for an entire family of vectors.
Worked example: a plane through the origin
Consider the set
The equation restricts three displayed coordinates using one relation. Choose and freely; then . Every vector in therefore has the form
Two numbers, and , determine all three coordinates. Geometrically, the result is a plane inside three-dimensional space. The plane passes through the origin because gives .
Now perform every part of the test. Write two arbitrary members as
Their sum is
The third coordinate equals the first plus the second: . Therefore . Scaling by any real gives
which also satisfies the defining equation. Together with the zero-vector check, this proves that is a subspace of .
Plane-in-space map
A subspace keeps every linear combination on the plane
The blue plane contains the origin and every vector whose third coordinate equals the sum of the first two. Solid arrows stay on the plane; the dashed arrow fails the equation.
| Vector | Coordinates | Membership test | Result |
|---|---|---|---|
| u | [1, 0, 1] | 1 = 1 + 0 | Inside U |
| v | [0, 1, 1] | 1 = 0 + 1 | Inside U |
| u − v | [1, −1, 0] | 0 = 1 + (−1) | Inside U |
| r | [1, 0, 0] | 0 ≠ 1 + 0 | Outside U |
The picture suggests the result, while the algebra proves it: a drawing can show a few arrows, but closure must hold for every pair of vectors and every real coefficient.
The diagram answers the geometric question: “Where can these features move?” The plane is lower-dimensional than its ambient space because one coordinate is forced by the other two. Still, the proof matters more than the perspective drawing. A few arrows landing on a plane cannot establish closure for infinitely many possible vectors.
Three attractive sets that fail
Subspaces are special among subsets. Shape, symmetry, and visual flatness are not sufficient.
First, shift the worked plane by one unit:
This is still a flat plane, but fails because . Therefore is not a vector subspace. Scaling also exposes the problem: if , then generally satisfies , not the equation defining .
Second, unit-length vectors do not form a subspace. A normalized embedding may satisfy , while has length and has length . The set fails scalar closure and excludes the zero vector. This is why the normalized embeddings discussed in Lesson 27 can be useful model inputs without forming a vector space or subspace.
Third, a finite dataset is usually not a subspace. Even if every recorded representation satisfies , the stored rows alone need not include their sums, negatives, or every real scaling. The constraint-defined family is a subspace; the finite sample used to discover or estimate it is merely a subset of .
NumPy can check examples, not replace the proof
For the worked plane, store the two generating directions as rows. Each row of
coefficients tells NumPy how much of each direction to combine:
import numpy as np
directions = np.array([
[1.0, 0.0, 1.0],
[0.0, 1.0, 1.0],
])
coefficients = np.array([
[2.0, -1.0],
[-1.0, 3.0],
[0.5, 0.25],
])
offsets = coefficients @ directions
constraint = np.array([1.0, 1.0, -1.0])
constraint_residuals = offsets @ constraint
print(offsets)
print(constraint_residuals)
print(np.allclose(
constraint_residuals,
0.0,
rtol=0.0,
atol=1e-12,
))
The output is
[[ 2. -1. 1. ]
[-1. 3. 2. ]
[ 0.5 0.25 0.75]]
[0. 0. 0.]
True
The first @ forms three linear combinations. The second evaluates
for each result. The NumPy matmul
documentation
defines the shape rule implemented by @; here, a coefficient
matrix times a direction matrix produces three vectors in
.
The exact example produces zeros, but real model activations use finite-precision
arithmetic. The NumPy allclose
documentation
defines the tolerance comparison and warns that its default absolute tolerance
can be inappropriate for values much smaller than one. Setting rtol=0.0 and
choosing an explicit atol makes the intended zero test visible. The tolerance
still has to match the activation scale and the error that the application can
accept; 1e-12 is appropriate for this constructed float64 example, not a
universal threshold.
This program checks chosen vectors against a known constraint. It does not prove that an arbitrary cloud of model activations belongs to the plane. That broader claim needs a fitted model, residual analysis, and evidence that the chosen linear approximation is adequate for the region being studied.
Representation manifolds are usually only locally flat
Let an encoder map an input to a representation . Choose an anchor representation and examine nearby offsets
If those offsets are well approximated by combinations of a small collection of directions, a subspace gives a useful local linear model. In the original coordinates, the approximation is the shifted set
The offset family passes through zero. The shifted representation patch passes through and is generally not itself a subspace of . Centering is what separates a legal direction from an absolute location.
This viewpoint is an approximation, not a claim that every learned representation lies on one global plane. In Yoshua Bengio’s primary paper on deep representations, manifold learning is described as finding low-dimensional high-density regions and coordinate systems for them. The paper presents PCA as a linear ancestor of manifold learning and also explains why a global linear assumption can miss curved structure. Locally, a plane can summarize the directions that vary near one anchor; farther away, the useful directions may rotate or the approximation error may grow.
For an AI practitioner, that creates a concrete interpretation rule:
- a subspace describes a family of allowed offsets closed under mixing and scaling;
- a shifted plane describes possible representations near an anchor; and
- a curved representation manifold may need different local planes in different neighborhoods.
The subspace test tells us whether the linear object is internally consistent. It does not tell us whether the object fits the data well, captures a causal feature, or preserves the information a downstream task needs.
Boundary cases sharpen the test
Two subspaces are easy to overlook. The set containing only the zero vector, , passes every condition: zero plus zero is zero, and every scalar times zero is zero. The whole ambient space is also a subspace of itself. These are called trivial subspaces, although both are useful boundary cases when debugging an argument.
The empty set fails because it does not contain zero. A single nonzero vector also fails over the real numbers: is not in the set unless . By contrast, the entire line does pass because it includes every real scaling, their sums, and zero.
Subspaces give precise language for restricted feature families. Once a set passes the test, any linear combination stays legal. The next lesson asks a different question about the directions used to describe that set: whether some of them are redundant.
Check your understanding
Question 1
What three conditions must a nonempty subset U of a real vector space satisfy to be a subspace?
Show the step-by-step solution
First, verify that . Second, take arbitrary and show that . Third, take an arbitrary real scalar and arbitrary , then show that . The vectors and scalar must be arbitrary because checking a few examples cannot establish closure for the whole set.
Question 2
Use the compact test to show that U = {(a, b, a + b): a, b are real} is closed under linear combinations.
Show the step-by-step solution
Choose and in , with real and . Then
The third coordinate equals , the sum of the first two. Therefore has the required form and belongs to . Because is nonempty, the compact test also supplies the zero vector.
Question 3
For u = (2, -1, 1) and v = (-1, 3, 2), compute -u + 2v and test whether the result lies in the lesson's plane.
Show the step-by-step solution
Scale first: and . Adding gives . The membership equation is ; here, . The equation holds, so the result lies in the plane, as closure predicts.
Question 4
Why does the shifted plane W = {x in R^3: x_3 = x_1 + x_2 + 1} fail the subspace test immediately?
Show the step-by-step solution
Test the zero vector before doing any longer calculation. Substitution gives , which is false, so . Every vector subspace must contain zero; therefore cannot be a subspace. The extra constant shifts the plane away from the origin.
Question 5
Do all unit-length embedding vectors form a subspace? Show the failing operation.
Show the step-by-step solution
No. Let have unit length, so . Scalar closure would require and to remain in the set. Their lengths are and , respectively, so neither is unit length. The set also excludes the zero vector. Normalization is useful, but its output family is not a vector subspace.
Question 6
The NumPy code reports constraint residuals [0, 0, 0]. What has this established, and what remains unproven?
Show the step-by-step solution
It establishes that the three constructed offsets satisfy to the chosen numerical tolerance. It also verifies the matrix shapes and arithmetic for those examples. It does not prove that every real model activation lies in this plane, that the plane is a good fit to a larger dataset, or that the represented directions matter to a downstream task. Those are empirical claims requiring more evidence.
Question 7
Why do we subtract an anchor h_0 before modeling nearby representations with a subspace U?
Show the step-by-step solution
Subspaces describe directions and must pass through zero. Nearby representations usually cluster around a nonzero anchor . Subtracting it produces offsets whose zero vector means “no movement from the anchor.” We can model those offsets with , then add the anchor back to obtain the shifted local patch .
Question 8
Classify each set as a subspace of R^3 or not: {0}, R^3, the empty set, and {lambda u: lambda is real} for a fixed nonzero u.
Show the step-by-step solution
is a subspace because addition and scaling never leave zero. is a subspace of itself because its vector operations are closed. The empty set is not a subspace because it lacks zero. Finally, is a subspace: zero appears at , scaling changes only the coefficient, and adding gives in the same set.