TL;DR
Take a list of vectors and compute the dot product of every pair. Write those numbers in a square table, with row i and column j holding the product of vector i and vector j. That table is the Gram matrix. It tells you how all the vectors relate to each one another, and it is the one object that machine learning methods need when they only compare data points.
Builds on: Matrix Multiplication, Matrix Transpose, Dot Product · Leads to: Positive Definite Matrix, Kernel Function
Where it's used
- Kernel methods in machine learning. A support vector machine or kernel ridge regression can be trained using only the Gram matrix of the training points, instead of the points themselves. Replacing the dot product with a kernel function gives the kernel matrix, and the algorithm never needs the features explicitly. Kernel Function explains that replacement.
- Orthogonalisation. Gram–Schmidt, the procedure that turns a set of vectors into perpendicular unit vectors, is built from the inner products that fill a Gram matrix. Those perpendicular vectors are what make least squares fits stable to compute.
- Document similarity. If each document is a vector of word counts, the Gram matrix of a collection of documents lists how much each pair shares. A search system can store it once and read off similarities without recomputing them.
- Checking a matrix. A matrix that is the Gram matrix of some vectors is symmetric and never has negative quadratic values. That is how people test whether a candidate covariance or kernel table is valid.
How it works
Start with three tiny vectors in two dimensions: , and .
- List the vectors as rows. Put them in a table, one row per vector. Call this table . Check: has 3 rows and 2 columns.
- Take the dot product of vector 1 with every vector. , , . These three numbers become row 1 of the table. Check: the first entry is the squared length of , which is 1.
- Repeat for vector 2. , , . Check: the first entry of row 2 equals the second entry of row 1, which is 0.
- Repeat for vector 3. , , . Check: the matrix is symmetric, so the entry in row 3, column 1 equals the entry in row 1, column 3.
- Write the table. The result is Check: the diagonal holds the squared lengths, and the off-diagonal entries are the pairwise dot products.
The key idea is that one step of the procedure is one dot product. You never need the vectors after step 5, only .
The math
Let be an matrix whose rows are the vectors in . The Gram matrix is the matrix of all pairwise dot products:
In words: entry is the dot product of row and row of . The second form says the same thing with Matrix Multiplication and Matrix Transpose: turns the rows into columns, and multiplying gives the dot products of every row with every row.
Step 2 in symbols: the diagonal
. The diagonal entries are squared lengths. Entries are never negative on the diagonal.
Step 4 in symbols: symmetry
Because , we get . So is symmetric.
Positivity, in one line
For any vector , . So is positive semidefinite, meaning for every . It is positive definite exactly when the vectors are linearly independent, which is a property the Positive Definite Matrix lesson builds on.
Rank
The rank of a matrix is the number of its linearly independent rows, which equals the number of independent columns. The rank of is at most , the number of columns of . So with 100 points in 3 dimensions, the Gram matrix has rank at most 3, even though it has 10,000 entries.
Worked examples
- Vectors , , , as in the walkthrough.
- Dot products: , , .
- , , .
- Arrange them: .
- Interpretation: vectors and are perpendicular, so their entry is 0. Vector has squared length 2, which is the diagonal entry.
- Vectors and . Stack them as rows: .
- .
- .
- .
- So . Its determinant is , which is positive, so the two vectors are linearly independent.
- Interpretation: gives , and the matching value gives . The off-diagonal 11 says the two vectors point in fairly similar directions.
Three documents are described by counts of the words "cat", "dog" and "fish": , , .
- Stack them: .
- Diagonal: , , .
- Off-diagonal: , , .
- So .
- Interpretation: the long document has the largest diagonal entry, 8. Every pair shares a weight of 2, but a raw dot product grows with document length, which is why search systems normalise the vectors before comparing them.
Try it: vectors and are rows of a matrix . What is the off-diagonal entry ? Answer: .
Common mistakes and tradeoffs
- Mixing up and . It is tempting to think the Gram matrix is always . In fact is the Gram matrix of the columns of , and it is , while is the Gram matrix of the rows and is . Check the size before you use either one.
- Thinking the Gram matrix is invertible. It is tempting to assume it can always be inverted, as a covariance matrix often can. In fact the rank is at most , so when the Gram matrix is singular, meaning it has no inverse. Kernel methods handle this with regularisation, adding a small multiple of the identity matrix (ones on the diagonal, zeros elsewhere).
- Assuming the entries are fixed. The numbers depend on the inner product you chose. Replace the dot product with a different one and the table changes, which is exactly what a kernel does on purpose.
- Storing it when you do not need to. For a large dataset the Gram matrix has entries. When is small, working with directly is cheaper. The Gram matrix pays off when is large or when a kernel makes effectively infinite.
Use the Gram matrix when an algorithm only compares points, when you want a symmetric table of similarities, or when you need to test positivity. Avoid it when the number of points is huge and the features are few.
History
Jørgen Pedersen Gram, a Danish actuary and mathematician, studied the inner products of a set of vectors in the late 1800s while working on how to make them perpendicular. The procedure now called Gram–Schmidt orthogonalisation uses those inner products, and the matrix that holds them took his name. The matrix stayed a tool of linear algebra for decades. In the 1990s, kernel methods such as support vector machines made it central to machine learning: the algorithm needed only the table of pairwise comparisons, not the features, so it could be applied to data that were awkward to write as vectors.
Recap
- A dot product measures how much two vectors point the same way.
- The Gram matrix of rows is the table of all pairwise dot products, .
- Its diagonal holds squared lengths, and it is always symmetric and positive semidefinite.
- Its rank is at most the number of features, so it can be much smaller than its size.
- Kernel methods replace the dot product with a Kernel Function and train using only the resulting kernel matrix.
- Next, read Positive Definite Matrix to see when the Gram matrix is invertible, and Kernel Function to see how the table is generalised.
Check yourself
QuestionWhy is the Gram matrix of a set of vectors always symmetric?Show answer
Entry is the dot product of vectors and , and the dot product does not depend on order: . So for every pair, and the matrix equals its transpose.
QuestionVectors (3, 0) and (0, 4) are the rows of X. What is the Gram matrix XXᵀ?Show answer
, , and . So .
QuestionWhy can a Gram matrix of 1,000 points in 5 dimensions have rank at most 5?Show answer
The Gram matrix is , where has 5 columns. The rank of a product is at most the smaller inner dimension, and here that inner dimension is 5, so the rank is at most 5 even though the matrix is .
QuestionA data scientist computes the Gram matrix of 200 points in 2 dimensions, then tries to invert it to solve a problem. Why might this fail, and what is a standard fix?Show answer
The rank is at most 2, far below 200, so the matrix is singular and has no inverse. A standard fix is regularisation: add a small positive number times the identity before inverting, which is what kernel ridge regression does.
QuestionTwo documents have word-count vectors with dot product 0. What does their Gram entry say, and what does it not say?Show answer
The entry is 0, so the two documents share no weighted word counts in the dot product sense: the vectors are perpendicular. It does not say that the documents are unrelated in meaning, because the counts ignore word meaning and only record the words that appear.
Sources
- Wikipedia, "Gram matrix": https://en.wikipedia.org/wiki/Gram_matrix
- Wikipedia, "Kernel method": https://en.wikipedia.org/wiki/Kernel_method