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.

Share this article

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 V=R3V=\mathbb{R}^3, and let U⊆VU\subseteq V be a candidate set. Calling UU a subspace means that vectors can stay inside UU while using the addition and scalar multiplication already defined on VV. 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 0u=00u=0 for every u∈Uu\in U. 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 α=β=1\alpha=\beta=1 to recover addition, set β=0\beta=0 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

U={[x1x2x3]∈R3:x3=x1+x2}.U=\left\{ \begin{bmatrix}x_1\\x_2\\x_3\end{bmatrix}\in\mathbb{R}^3 :x_3=x_1+x_2 \right\}.

The equation restricts three displayed coordinates using one relation. Choose x1=ax_1=a and x2=bx_2=b freely; then x3=a+bx_3=a+b. Every vector in UU therefore has the form

[aba+b]=a[101]+b[011].\begin{bmatrix}a\\b\\a+b\end{bmatrix} =a\begin{bmatrix}1\\0\\1\end{bmatrix} +b\begin{bmatrix}0\\1\\1\end{bmatrix}.

Two numbers, aa and bb, determine all three coordinates. Geometrically, the result is a plane inside three-dimensional space. The plane passes through the origin because a=b=0a=b=0 gives (0,0,0)(0,0,0).

Now perform every part of the test. Write two arbitrary members as

u=[aba+b],v=[cdc+d].u=\begin{bmatrix}a\\b\\a+b\end{bmatrix}, \qquad v=\begin{bmatrix}c\\d\\c+d\end{bmatrix}.

Their sum is

u+v=[a+cb+da+b+c+d].u+v =\begin{bmatrix}a+c\\b+d\\a+b+c+d\end{bmatrix}.

The third coordinate equals the first plus the second: (a+c)+(b+d)=a+b+c+d(a+c)+(b+d)=a+b+c+d. Therefore u+v∈Uu+v\in U. Scaling uu by any real λ\lambda gives

λu=[λaλbλ(a+b)]=[λaλbλa+λb],\lambda u =\begin{bmatrix}\lambda a\\\lambda b\\\lambda(a+b)\end{bmatrix} =\begin{bmatrix}\lambda a\\\lambda b\\\lambda a+\lambda b\end{bmatrix},

which also satisfies the defining equation. Together with the zero-vector check, this proves that UU is a subspace of R3\mathbb{R}^3.

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.

Plane subspace inside three-dimensional coordinate spaceA blue plane through the origin shows the set x three equals x one plus x two. Vectors u, v, and u minus v lie on the plane. A dashed vector r ends away from the plane and is joined to its corresponding on-plane point by a dashed gap.U: x₃ = x₁ + x₂u = [1, 0, 1]v = [0, 1, 1]u − v = [1, −1, 0]r = [1, 0, 0]fails 0 = 1 + 00
The plane needs two freely chosen coordinates even though its vectors are written with three. Passing through zero and remaining closed under addition and scaling make it a subspace.
Coordinate checks represented in the plane diagram
VectorCoordinatesMembership testResult
u[1, 0, 1]1 = 1 + 0Inside U
v[0, 1, 1]1 = 0 + 1Inside U
u − v[1, −1, 0]0 = 1 + (−1)Inside U
r[1, 0, 0]0 ≠ 1 + 0Outside 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:

W={x∈R3:x3=x1+x2+1}.W=\{x\in\mathbb{R}^3:x_3=x_1+x_2+1\}.

This is still a flat plane, but (0,0,0)(0,0,0) fails because 0≠0+0+10\neq0+0+1. Therefore WW is not a vector subspace. Scaling also exposes the problem: if w∈Ww\in W, then 2w2w generally satisfies x3=x1+x2+2x_3=x_1+x_2+2, not the equation defining WW.

Second, unit-length vectors do not form a subspace. A normalized embedding ee may satisfy ∥e∥=1\lVert e\rVert=1, while 2e2e has length 22 and 0e0e has length 00. 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 x3=x1+x2x_3=x_1+x_2, the stored rows alone need not include their sums, negatives, or every real scaling. The constraint-defined family UU is a subspace; the finite sample used to discover or estimate it is merely a subset of UU.

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 x1+x2−x3x_1+x_2-x_3 for each result. The NumPy matmul documentation defines the shape rule implemented by @; here, a 3×23\times2 coefficient matrix times a 2×32\times3 direction matrix produces three vectors in R3\mathbb{R}^3.

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 xx to a representation h(x)∈Rdh(x)\in\mathbb{R}^d. Choose an anchor representation h0h_0 and examine nearby offsets

Δh=h(x)−h0.\Delta h=h(x)-h_0.

If those offsets are well approximated by combinations of a small collection of directions, a subspace UU gives a useful local linear model. In the original coordinates, the approximation is the shifted set

h0+U={h0+u:u∈U}.h_0+U=\{h_0+u:u\in U\}.

The offset family UU passes through zero. The shifted representation patch h0+Uh_0+U passes through h0h_0 and is generally not itself a subspace of Rd\mathbb{R}^d. 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, {0}\{0\}, passes every condition: zero plus zero is zero, and every scalar times zero is zero. The whole ambient space VV 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 {u}\{u\} also fails over the real numbers: 2u2u is not in the set unless u=0u=0. By contrast, the entire line {λu:λ∈R}\{\lambda u:\lambda\in\mathbb{R}\} 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 0∈U0\in U. Second, take arbitrary u,v∈Uu,v\in U and show that u+v∈Uu+v\in U. Third, take an arbitrary real scalar λ\lambda and arbitrary u∈Uu\in U, then show that λu∈U\lambda u\in U. 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 u=(a,b,a+b)u=(a,b,a+b) and v=(c,d,c+d)v=(c,d,c+d) in UU, with real α\alpha and β\beta. Then

αu+βv=(αa+βc,  αb+βd,  α(a+b)+β(c+d)).\alpha u+\beta v =(\alpha a+\beta c,\;\alpha b+\beta d,\; \alpha(a+b)+\beta(c+d)).

The third coordinate equals (αa+βc)+(αb+βd)(\alpha a+\beta c)+(\alpha b+\beta d), the sum of the first two. Therefore αu+βv\alpha u+\beta v has the required form and belongs to UU. Because UU 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: −u=(−2,1,−1)-u=(-2,1,-1) and 2v=(−2,6,4)2v=(-2,6,4). Adding gives −u+2v=(−4,7,3)-u+2v=(-4,7,3). The membership equation is x3=x1+x2x_3=x_1+x_2; here, 3=−4+73=-4+7. 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 0=0+0+10=0+0+1, which is false, so 0∉W0\notin W. Every vector subspace must contain zero; therefore WW 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 ee have unit length, so ∥e∥=1\lVert e\rVert=1. Scalar closure would require 2e2e and 0e0e to remain in the set. Their lengths are 22 and 00, 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 x1+x2−x3=0x_1+x_2-x_3=0 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 h0h_0. Subtracting it produces offsets Δh=h(x)−h0\Delta h=h(x)-h_0 whose zero vector means “no movement from the anchor.” We can model those offsets with UU, then add the anchor back to obtain the shifted local patch h0+Uh_0+U.

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

{0}\{0\} is a subspace because addition and scaling never leave zero. R3\mathbb{R}^3 is a subspace of itself because its vector operations are closed. The empty set is not a subspace because it lacks zero. Finally, {λu:λ∈R}\{\lambda u:\lambda\in\mathbb{R}\} is a subspace: zero appears at λ=0\lambda=0, scaling changes only the coefficient, and adding λu+μu\lambda u+\mu u gives (λ+μ)u(\lambda+\mu)u in the same set.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF
  3. NumPy documentation: numpy.matmul
  4. NumPy documentation: numpy.allclose
  5. Deep Learning of Representations