Search

Search papers and lessons

Gram Matrix

Level 2 · 10 min read

The Gram matrix of a list of vectors holds every pairwise dot product in a square table, so it records how all the vectors relate to each other.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • ReadReady to readNot yet

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: x1=(1,0)\mathbf{x}_1 = (1, 0), x2=(0,1)\mathbf{x}_2 = (0, 1) and x3=(1,1)\mathbf{x}_3 = (1, 1).

  1. List the vectors as rows. Put them in a 3×23 \times 2 table, one row per vector. Call this table XX. Check: XX has 3 rows and 2 columns.
  2. Take the dot product of vector 1 with every vector. x1⋅x1=1\mathbf{x}_1 \cdot \mathbf{x}_1 = 1, x1⋅x2=0\mathbf{x}_1 \cdot \mathbf{x}_2 = 0, x1⋅x3=1\mathbf{x}_1 \cdot \mathbf{x}_3 = 1. These three numbers become row 1 of the table. Check: the first entry is the squared length of x1\mathbf{x}_1, which is 1.
  3. Repeat for vector 2. x2⋅x1=0\mathbf{x}_2 \cdot \mathbf{x}_1 = 0, x2⋅x2=1\mathbf{x}_2 \cdot \mathbf{x}_2 = 1, x2⋅x3=1\mathbf{x}_2 \cdot \mathbf{x}_3 = 1. Check: the first entry of row 2 equals the second entry of row 1, which is 0.
  4. Repeat for vector 3. x3⋅x1=1\mathbf{x}_3 \cdot \mathbf{x}_1 = 1, x3⋅x2=1\mathbf{x}_3 \cdot \mathbf{x}_2 = 1, x3⋅x3=2\mathbf{x}_3 \cdot \mathbf{x}_3 = 2. Check: the matrix is symmetric, so the entry in row 3, column 1 equals the entry in row 1, column 3.
  5. Write the table. The result is G=(101011112).G = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 2 \end{pmatrix}. 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 GG.

The math

Let XX be an n×dn \times d matrix whose rows are the vectors x1,…,xn\mathbf{x}_1, \ldots, \mathbf{x}_n in Rd\mathbb{R}^d. The Gram matrix is the n×nn \times n matrix of all pairwise dot products:

Gij=xi⊤xj,G=XX⊤.G_{ij} = \mathbf{x}_i^{\top} \mathbf{x}_j, \qquad G = X X^{\top}.

In words: entry GijG_{ij} is the dot product of row ii and row jj of XX. The second form says the same thing with Matrix Multiplication and Matrix Transpose: X⊤X^{\top} turns the rows into columns, and multiplying gives the dot products of every row with every row.

Step 2 in symbols: the diagonal

Gii=xi⊤xi=∥xi∥2G_{ii} = \mathbf{x}_i^{\top} \mathbf{x}_i = \lVert \mathbf{x}_i \rVert^2. The diagonal entries are squared lengths. Entries are never negative on the diagonal.

Step 4 in symbols: symmetry

Because xi⊤xj=xj⊤xi\mathbf{x}_i^{\top} \mathbf{x}_j = \mathbf{x}_j^{\top} \mathbf{x}_i, we get G⊤=(XX⊤)⊤=XX⊤=GG^{\top} = (XX^{\top})^{\top} = XX^{\top} = G. So GG is symmetric.

Positivity, in one line

For any vector c∈Rn\mathbf{c} \in \mathbb{R}^n, c⊤Gc=∥X⊤c∥2≥0\mathbf{c}^{\top} G \mathbf{c} = \lVert X^{\top} \mathbf{c} \rVert^2 \ge 0. So GG is positive semidefinite, meaning c⊤Gc≥0\mathbf{c}^{\top} G \mathbf{c} \ge 0 for every c\mathbf{c}. 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 G=XX⊤G = XX^{\top} is at most dd, the number of columns of XX. So with 100 points in 3 dimensions, the 100×100100 \times 100 Gram matrix has rank at most 3, even though it has 10,000 entries.

Worked examples

Example
Example 1: the tiny case
  1. Vectors a=(1,0)\mathbf{a} = (1, 0), b=(0,1)\mathbf{b} = (0, 1), c=(1,1)\mathbf{c} = (1, 1), as in the walkthrough.
  2. Dot products: a⋅a=1\mathbf{a} \cdot \mathbf{a} = 1, a⋅b=0\mathbf{a} \cdot \mathbf{b} = 0, a⋅c=1\mathbf{a} \cdot \mathbf{c} = 1.
  3. b⋅b=1\mathbf{b} \cdot \mathbf{b} = 1, b⋅c=1\mathbf{b} \cdot \mathbf{c} = 1, c⋅c=2\mathbf{c} \cdot \mathbf{c} = 2.
  4. Arrange them: G=(101011112)G = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 2 \end{pmatrix}.
  5. Interpretation: vectors a\mathbf{a} and b\mathbf{b} are perpendicular, so their entry is 0. Vector c\mathbf{c} has squared length 2, which is the diagonal entry.
Example
Example 2: the standard case
  1. Vectors u=(3,4)\mathbf{u} = (3, 4) and v=(1,2)\mathbf{v} = (1, 2). Stack them as rows: X=(3412)X = \begin{pmatrix} 3 & 4 \\ 1 & 2 \end{pmatrix}.
  2. G11=3⋅3+4⋅4=9+16=25G_{11} = 3 \cdot 3 + 4 \cdot 4 = 9 + 16 = 25.
  3. G12=3⋅1+4⋅2=3+8=11G_{12} = 3 \cdot 1 + 4 \cdot 2 = 3 + 8 = 11.
  4. G22=1⋅1+2⋅2=1+4=5G_{22} = 1 \cdot 1 + 2 \cdot 2 = 1 + 4 = 5.
  5. So G=(2511115)G = \begin{pmatrix} 25 & 11 \\ 11 & 5 \end{pmatrix}. Its determinant is 25⋅5−11⋅11=125−121=425 \cdot 5 - 11 \cdot 11 = 125 - 121 = 4, which is positive, so the two vectors are linearly independent.
  6. Interpretation: ∥u∥=5\lVert \mathbf{u} \rVert = 5 gives G11=25G_{11} = 25, and the matching value ∥v∥2=5\lVert \mathbf{v} \rVert^2 = 5 gives G22G_{22}. The off-diagonal 11 says the two vectors point in fairly similar directions.
Example
Example 3: the realistic case, word counts

Three documents are described by counts of the words "cat", "dog" and "fish": d1=(2,0,1)d_1 = (2, 0, 1), d2=(1,1,0)d_2 = (1, 1, 0), d3=(0,2,2)d_3 = (0, 2, 2).

  1. Stack them: X=(201110022)X = \begin{pmatrix} 2 & 0 & 1 \\ 1 & 1 & 0 \\ 0 & 2 & 2 \end{pmatrix}.
  2. Diagonal: d1⋅d1=4+0+1=5d_1 \cdot d_1 = 4 + 0 + 1 = 5, d2⋅d2=1+1+0=2d_2 \cdot d_2 = 1 + 1 + 0 = 2, d3⋅d3=0+4+4=8d_3 \cdot d_3 = 0 + 4 + 4 = 8.
  3. Off-diagonal: d1⋅d2=2+0+0=2d_1 \cdot d_2 = 2 + 0 + 0 = 2, d1⋅d3=0+0+2=2d_1 \cdot d_3 = 0 + 0 + 2 = 2, d2⋅d3=0+2+0=2d_2 \cdot d_3 = 0 + 2 + 0 = 2.
  4. So G=(522222228)G = \begin{pmatrix} 5 & 2 & 2 \\ 2 & 2 & 2 \\ 2 & 2 & 8 \end{pmatrix}.
  5. Interpretation: the long document d3d_3 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 (1,2,2)(1, 2, 2) and (2,0,1)(2, 0, 1) are rows of a 2×32 \times 3 matrix XX. What is the off-diagonal entry G12G_{12}? Answer: 1⋅2+2⋅0+2⋅1=2+0+2=41 \cdot 2 + 2 \cdot 0 + 2 \cdot 1 = 2 + 0 + 2 = 4.

Common mistakes and tradeoffs

  • Mixing up XX⊤XX^{\top} and X⊤XX^{\top}X. It is tempting to think the Gram matrix is always X⊤XX^{\top}X. In fact X⊤XX^{\top}X is the Gram matrix of the columns of XX, and it is d×dd \times d, while XX⊤XX^{\top} is the Gram matrix of the rows and is n×nn \times n. 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 dd, so when n>dn > d 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 n2n^2 entries. When dd is small, working with XX directly is cheaper. The Gram matrix pays off when dd is large or when a kernel makes dd 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 x1,…,xn\mathbf{x}_1, \ldots, \mathbf{x}_n is the table of all pairwise dot products, G=XX⊤G = XX^{\top}.
  • 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 GijG_{ij} is the dot product of vectors ii and jj, and the dot product does not depend on order: xi⊤xj=xj⊤xi\mathbf{x}_i^{\top} \mathbf{x}_j = \mathbf{x}_j^{\top} \mathbf{x}_i. So Gij=GjiG_{ij} = G_{ji} 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

G11=9G_{11} = 9, G22=16G_{22} = 16, and G12=3⋅0+0⋅4=0G_{12} = 3 \cdot 0 + 0 \cdot 4 = 0. So G=(90016)G = \begin{pmatrix} 9 & 0 \\ 0 & 16 \end{pmatrix}.

QuestionWhy can a Gram matrix of 1,000 points in 5 dimensions have rank at most 5?Show answer

The Gram matrix is XX⊤XX^{\top}, where XX 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 1000×10001000 \times 1000.

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