Math for AI course · Lesson 32 of 180
Dimension Counts Independent Directions
Separate the number of embedding coordinates from the number of samples, and learn why a matrix shape alone cannot tell you how many independent directions its vectors span.
An embedding batch can have four rows and hundreds of coordinates per row. The first number tells you how many inputs were encoded; the second tells you the coordinate width of each output. Neither number, by itself, tells you how many independent directions the observed vectors actually use.
In this lesson, we will read those counts separately, connect dimension to the number of vectors in a basis, and see why two arrays with the same shape can span spaces of different dimensions. The distinction matters when reading an embedding API, estimating what a batch can tell you, or describing a representation without confusing stored examples with the space that holds them.
The previous lesson showed how a basis gives every vector a coordinate description. Lesson 31: Bases give compact coordinate systems established the key idea we need here: a basis spans a space without redundant directions. The official Mathematics for Machine Learning companion site develops dimension in Section 2.6 of the book PDF. The small datasets, code, visual, and assessments in this lesson are original.
A basis gives dimension its count
Recall that a basis is a linearly independent set of vectors that spans a vector space. If every vector in a space can be described using a basis , then the dimension of is the number of vectors in that basis:
The notation means “the number of elements in the set .” It does not count the number of entries used to write any one basis vector. Every basis of a finite-dimensional space has the same number of vectors, even though the actual basis vectors can differ. That count is what makes dimension a property of the space rather than of one particular coordinate system.
The vectors above each contain three coordinates, but that is not the definition of dimension. The decisive fact is that the three vectors form a basis: together they generate every vector in , and none is a linear combination of the other two. A different basis can point in other directions and still contain exactly three vectors.
Four samples can still live in three dimensions
Suppose an encoder returns four vectors in :
Store each vector as one row of a matrix:
Its shape is : four rows and three columns. Here, rows are samples and columns hold the three coordinates of each sample. The first three rows are the standard basis, so the four samples span all of . The fourth row adds another sample, but not another independent direction, because . The vector space has dimension three; the batch has four examples.
Now compare this batch:
It has the same shape, , yet every row is a multiple of . One vector, , spans all four samples, so their span is a line with dimension one. The ambient coordinate space is still ; the particular samples occupy only a one-dimensional subspace of it.
Embedding dimension and batch shape
The same four-by-three shape can span three directions or one
Both batches contain four samples with three coordinates each. The coordinate tables show the exact values; the diagrams summarize how many independent directions their rows use.
Three independent directions
Shape: 4 samples × 3 coordinates
| Sample | Coordinate 1 | Coordinate 2 | Coordinate 3 |
|---|---|---|---|
| x₁ | 1 | 0 | 0 |
| x₂ | 0 | 1 | 0 |
| x₃ | 0 | 0 | 1 |
| x₄ | 1 | 1 | 1 |
Dimension of the sample span3 directions
One independent direction
Shape: 4 samples × 3 coordinates
| Sample | Coordinate 1 | Coordinate 2 | Coordinate 3 |
|---|---|---|---|
| x₁ | 1 | 0 | 0 |
| x₂ | 2 | 0 | 0 |
| x₃ | −1 | 0 | 0 |
| x₄ | 3 | 0 | 0 |
Dimension of the sample span1 direction
Rows count inputs, columns count stored coordinates, and basis vectors count independent directions. The table is the exact data; the small direction sketches are schematic.
A shape reports how an array is stored. It does not reveal whether its rows are independent; the values and their relationships determine the dimension of their span.
The diagram keeps three questions separate: how many inputs were encoded, how many coordinates each output contains, and how many directions those particular outputs span. The arrays share the same shape, but only the first batch reaches every direction in .
NumPy reports shape, not a basis
NumPy’s ndarray.shape is a tuple containing the length of each array axis.
For a two-dimensional matrix stored as rows of coordinates, that tuple
is (N, d). The shape tells us how the values are arranged. To identify the
dimension of a span, we must also reason about whether some rows can be built
from others.
Here is the synthetic example encoded directly in NumPy:
import numpy as np
full = np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
])
line = np.array([
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[3.0, 0.0, 0.0],
])
print(full.shape, line.shape)
print(np.allclose(full[3], full[0] + full[1] + full[2]))
print(np.allclose(
line,
np.array([1.0, 2.0, -1.0, 3.0])[:, None]
* np.array([1.0, 0.0, 0.0]),
))
The output is:
(4, 3) (4, 3)
True
True
The first line confirms that both matrices have four rows and three
coordinates per row. The first True verifies that the last full sample is
the sum of the three basis rows. The second verifies that each line sample
is the stated scalar multiple of . Those relationships justify the span
dimensions: three for full, one for line.
The NumPy shape documentation
defines shape as the tuple of array dimensions. That is a storage description,
not a promise that all coordinates vary independently. A quick check of
len(full.shape) would return two because full has two array axes. That
number is also distinct from the dimension three of its embedding space.
The batch axis is not the embedding axis
The distinction appears in real AI code as well. The Sentence Transformers
usage documentation
encodes three sentences with all-MiniLM-L6-v2 and reports an embedding array
with shape [3, 384]. The first length corresponds to the three supplied
sentences; the second is the output coordinate width. The model API exposes
get_embedding_dimension()
for the output width, and its encode method returns embeddings for the
provided inputs.
For a million stored documents with the same encoder, the corpus may contain a million rows, while each row remains 384 numbers wide. Increasing the corpus adds more vectors; it does not turn the embedding space into a million dimensions. Conversely, four outputs in cannot span all 384 dimensions, because four vectors supply at most four independent directions. They might span fewer if some outputs repeat a direction or are linear combinations of others.
One more boundary matters: the output width describes the chosen model and
configuration, not a universal property of the texts. Sentence Transformers
allows an output truncation dimension in encode; after such a configuration
change, the returned coordinate width changes too. The encode
API
documents the truncate_dim option. The vectors still need to be compared in
a compatible coordinate space. And a larger dimension alone
does not guarantee that an embedding preserves more useful meaning; that
requires evaluating the representation for its intended task.
Dimension gives us a count of independent basis directions. A shape gives us the lengths of array axes, and a batch gives us a collection of samples. Once those quantities stop sharing one label, the next question becomes easier to ask: how many independent directions do the actual rows contain? Lesson 33 introduces matrix rank to count that effective dimension systematically.
Check your understanding
Question 1
A basis B for a vector space contains five vectors. What is the dimension of the space, and why does the number of coordinates printed inside one basis vector not define it?
Show the step-by-step solution
The space has dimension five because dimension is defined as the number of vectors in a basis: . A coordinate list describes one vector relative to a coordinate system. Its entries are not separate basis vectors of , so counting them answers a different question.
Question 2
An encoder returns an array with shape (7, 12), where each row is one embedding. How many samples are present, how many coordinates does each embedding have, and what is the dimension of the ambient coordinate space?
Show the step-by-step solution
The first shape entry gives seven rows, so there are seven samples. The second entry gives twelve coordinates per row. If these are ordinary dense vectors in the encoder’s output coordinates, the ambient space is , which has dimension 12. The seven samples do not change that coordinate space’s dimension.
Question 3
For x₁ = (1, 0, 0), x₂ = (0, 1, 0), x₃ = (0, 0, 1), and x₄ = (1, 1, 1), how many samples are there and what is the dimension of their span? Show the dependency you use.
Show the step-by-step solution
There are four samples. The first three are the standard basis vectors of , so they are independent and span three-dimensional space. The fourth satisfies and therefore adds no new direction. The span has dimension three, not four.
Question 4
Four rows have the form (c, 0, 0), with c equal to 1, 2, −1, and 3. What is the dimension of their span, and what basis can you give?
Show the step-by-step solution
Each row equals . Therefore all four vectors lie on the line . The nonzero vector spans that line, and the one-element set is a basis. The span has dimension one.
Question 5
Two data matrices both have shape (4, 3). Can their row spans have different dimensions? Give a concrete explanation using the two lesson examples.
Show the step-by-step solution
Yes. In full, the first three rows are the three standard basis vectors,
so they span and the row span has dimension three. In line,
all four rows are multiples of , so they span a line and have span
dimension one. Shape records row and column counts; it does not encode the
linear relationships among row values.
Question 6
If N vectors lie in ℝᵈ, explain why the dimension r of their span cannot exceed either N or d.
Show the step-by-step solution
The vectors themselves form a generating set for their span. Removing redundant members can leave at most independent basis vectors, so . The span is also a subspace of , whose dimension is , so . Combining the two bounds gives .
Question 7
Sentence Transformers documents an output shape [3, 384] for three encoded sentences. Which number is the sample count, which is the embedding width, and can those three outputs span all of ℝ³⁸⁴? Explain.
Show the step-by-step solution
There are three sentence samples, and each output vector has 384 coordinates. The ambient embedding space is . Three vectors can span at most three independent directions, so they cannot span all 384 dimensions. They may span fewer than three if some outputs are dependent.
Question 8
A NumPy array X has shape (5, 9). Why does len(X.shape) equal 2 without implying that the embeddings have dimension two?
Show the step-by-step solution
X.shape has one entry per array axis: five rows and nine columns. Thus the
array is two-axis data, and len(X.shape) counts those two axes. If each row
is an embedding with nine coordinates, the ambient embedding space is
and has dimension nine. Array axes and vector-space basis
directions are different counts.