Search

Search papers and lessons

Outer Product

Level 1 · 11 min read

The outer product takes two vectors and makes a grid of all their pairwise products, so a column of length m and a row of length n give an m-by-n matrix.

On this page

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

TL;DR

The outer product takes two vectors and produces a grid that holds every product of one entry from the first with one entry from the second. If the first vector has mm numbers and the second has nn, the grid is an m×nm \times n Matrix. It is the natural way to build a matrix from two lists, and it is the basic piece from which many larger matrices are assembled.

Builds on: Vector, Matrix, Matrix Transpose · Leads to: Low-Rank Matrix, Covariance Matrix, Singular Value Decomposition (SVD)

Where it's used

Covariance matrices in statistics. When you measure several quantities for many people, say height and weight, the covariance matrix records how each pair of quantities moves together. Each person's centred measurements form a vector, and the covariance matrix is the average of that vector's outer product with itself, summed over people. Financial analysts use the same construction to study how stocks move together.

Learning in neural networks. In a dense layer that maps an input vector x\mathbf{x} to an output vector, the Gradient of the loss with respect to the weight matrix is an outer product of the output error and the input. Training the weights therefore adds many outer products. Backpropagation computes these pieces efficiently.

Compressing images and data. A Singular Value Decomposition (SVD) writes a matrix as a sum of outer products, each built from a pair of singular vectors and weighted by a size. Keeping only the largest few terms gives a Low-Rank Matrix that is close to the original, which is how a picture can be stored with far fewer numbers.

How it works

Take two small vectors. Let u=(1,2)\mathbf{u} = (1, 2) be a column with two entries, and v=(3,4,5)\mathbf{v} = (3, 4, 5) a row with three entries. We build a grid with two rows and three columns.

  1. Write u\mathbf{u} as a column and v\mathbf{v} as a row. u\mathbf{u} has 2 entries, so it becomes a column of height 2. v\mathbf{v} has 3 entries, so it becomes a row of width 3. Check: the grid will have 2 rows and 3 columns.
  2. Fill row 1 by multiplying the first entry of u\mathbf{u} by each entry of v\mathbf{v}. The first entry of u\mathbf{u} is 1, so row 1 is 1×3=31 \times 3 = 3, 1×4=41 \times 4 = 4, 1×5=51 \times 5 = 5. Check: row 1 is (3,4,5)(3, 4, 5), which is just v\mathbf{v} itself.
  3. Fill row 2 the same way with the second entry of u\mathbf{u}. The second entry is 2, so row 2 is 2×3=62 \times 3 = 6, 2×4=82 \times 4 = 8, 2×5=102 \times 5 = 10. Check: row 2 is (6,8,10)(6, 8, 10), which is 2v2\mathbf{v}.
  4. Read off the result. The outer product is uv⊤=(3456810).\mathbf{u}\mathbf{v}^\top = \begin{pmatrix} 3 & 4 & 5 \\ 6 & 8 & 10 \end{pmatrix}. Check: it has 2 rows and 3 columns, one entry for each pair (row index of u\mathbf{u}, column index of v\mathbf{v}).
  5. Notice the pattern. Every row is a multiple of the same row vector, v⊤\mathbf{v}^\top. The first row is 1 times it and the second is 2 times it. Check: all rows point in one direction, so the matrix has only one independent row.

The outer product is the grid of all pairwise products. Its entry in row ii and column jj is simply the ii-th entry of the column times the jj-th entry of the row. Nothing is added up. This is what separates it from the Dot Product, which multiplies entries pairwise and then adds them into one number.

flowchart LR
  U["u as a column (2 entries)"] --> G[Multiply every u entry by every v entry]
  V["v as a row (3 entries)"] --> G
  G --> M["2 by 3 matrix"]

The math

Definition: Outer product

For a column vector u\mathbf{u} with mm entries and a column vector v\mathbf{v} with nn entries, the outer product is the m×nm \times n matrix uv⊤,(uv⊤)ij=ui vj,\mathbf{u}\mathbf{v}^\top, \qquad \left(\mathbf{u}\mathbf{v}^\top\right)_{ij} = u_i\, v_j, where uiu_i is the ii-th entry of u\mathbf{u}, vjv_j is the jj-th entry of v\mathbf{v}, and (⋅)ij(\cdot)_{ij} denotes the entry in row ii and column jj.

Step 1 in symbols: the shapes

u\mathbf{u} is an m×1m \times 1 column and v⊤\mathbf{v}^\top is a 1×n1 \times n row. Multiplying an m×1m \times 1 matrix by a 1×n1 \times n matrix gives an m×nm \times n matrix, because the inner dimensions, both equal to 1, cancel. In the example, m=2m = 2 and n=3n = 3.

Step 2 and 3 in symbols: filling the grid

The formula (uv⊤)ij=uivj(\mathbf{u}\mathbf{v}^\top)_{ij} = u_i v_j says that row ii of the result is uiu_i times v⊤\mathbf{v}^\top. In the example, row 1 is u1=1u_1 = 1 times (3,4,5)(3, 4, 5), and row 2 is u2=2u_2 = 2 times (3,4,5)(3, 4, 5), which gives (6,8,10)(6, 8, 10).

Step 5 in symbols: the rank-one pattern

Every row is a scalar multiple of v⊤\mathbf{v}^\top, so the matrix has rank one. Equivalently, applying it to any vector w\mathbf{w} gives (uv⊤)w=u (v⊤w).\left(\mathbf{u}\mathbf{v}^\top\right)\mathbf{w} = \mathbf{u}\,\left(\mathbf{v}^\top \mathbf{w}\right). The term v⊤w\mathbf{v}^\top \mathbf{w} is a dot product and therefore a single number, so the output is always a multiple of u\mathbf{u}. Check with w=(1,0,0)\mathbf{w} = (1, 0, 0): v⊤w=3\mathbf{v}^\top \mathbf{w} = 3, and the output is 3u=(3,6)3\mathbf{u} = (3, 6), which matches the first column of the matrix.

Sums of outer products

Outer products add entry by entry, so a sum of them is also a valid matrix construction. Take two data points, x1=(1,2)\mathbf{x}_1 = (1, 2) and x2=(3,4)\mathbf{x}_2 = (3, 4). Then x1x1⊤+x2x2⊤=(1224)+(9121216)=(10141420).\mathbf{x}_1\mathbf{x}_1^\top + \mathbf{x}_2\mathbf{x}_2^\top = \begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix} + \begin{pmatrix} 9 & 12 \\ 12 & 16 \end{pmatrix} = \begin{pmatrix} 10 & 14 \\ 14 & 20 \end{pmatrix}. This is the kind of sum that gives a covariance matrix once the points are centred and averaged.

Worked examples

Example
Example 1: a tiny case

Let u=(1,0)\mathbf{u} = (1, 0) and v=(2,5)\mathbf{v} = (2, 5).

  1. u\mathbf{u} has 2 entries and v\mathbf{v} has 2 entries, so the result is a 2×22 \times 2 matrix.
  2. Row 1 is u1=1u_1 = 1 times (2,5)(2, 5), which is (2,5)(2, 5).
  3. Row 2 is u2=0u_2 = 0 times (2,5)(2, 5), which is (0,0)(0, 0).
  4. The result is (2500)\begin{pmatrix} 2 & 5 \\ 0 & 0 \end{pmatrix}.
  5. Check: the second row is zero because u2=0u_2 = 0, and the first row is exactly v\mathbf{v}.
Example
Example 2: the standard case with unequal sizes

Let u=(2,1,0)\mathbf{u} = (2, 1, 0) and v=(1,3)\mathbf{v} = (1, 3). Here u\mathbf{u} has three entries and v\mathbf{v} has two.

  1. The result is a 3×23 \times 2 matrix: three rows, two columns.
  2. Row 1 is u1=2u_1 = 2 times (1,3)(1, 3), which is (2,6)(2, 6).
  3. Row 2 is u2=1u_2 = 1 times (1,3)(1, 3), which is (1,3)(1, 3).
  4. Row 3 is u3=0u_3 = 0 times (1,3)(1, 3), which is (0,0)(0, 0).
  5. The result is (261300)\begin{pmatrix} 2 & 6 \\ 1 & 3 \\ 0 & 0 \end{pmatrix}.
  6. Check: the entry in row 2, column 2 should be u2v2=1×3=3u_2 v_2 = 1 \times 3 = 3. It is.
Example
Example 3: the gradient of one layer (realistic)

A neural-network layer takes a two-number input x=(0.5,2)\mathbf{x} = (0.5, 2) and produces outputs. During training, the error at the outputs is δ=(1,−3)\boldsymbol{\delta} = (1, -3), a two-number vector, one entry per output. The gradient of the loss with respect to the layer's weight matrix is the outer product δx⊤\boldsymbol{\delta}\mathbf{x}^\top.

  1. δ\boldsymbol{\delta} has 2 entries and x\mathbf{x} has 2 entries, so the gradient is a 2×22 \times 2 matrix. Each entry is one weight's gradient.
  2. Row 1 is δ1=1\delta_1 = 1 times (0.5,2)(0.5, 2), which is (0.5,2)(0.5, 2).
  3. Row 2 is δ2=−3\delta_2 = -3 times (0.5,2)(0.5, 2), which is (−1.5,−6)(-1.5, -6).
  4. The gradient is (0.52−1.5−6)\begin{pmatrix} 0.5 & 2 \\ -1.5 & -6 \end{pmatrix}.
  5. Interpretation: the weight from input 2 to output 1 has gradient 22, so increasing it raises the loss. The weight from input 1 to output 2 has gradient −1.5-1.5, so increasing it lowers the loss.

Try it: the output error is δ=(2,0)\boldsymbol{\delta} = (2, 0) and the input is x=(1,−1)\mathbf{x} = (1, -1). Compute δx⊤\boldsymbol{\delta}\mathbf{x}^\top and say which weights have zero gradient. Answer: (2−200)\begin{pmatrix} 2 & -2 \\ 0 & 0 \end{pmatrix}. The second output has zero error, so both of its weights have zero gradient.

Common mistakes and tradeoffs

It is tempting to think that the outer product of two vectors is a number, as with the Dot Product. In fact the dot product u⊤v\mathbf{u}^\top\mathbf{v} adds the pairwise products into one number, while the outer product uv⊤\mathbf{u}\mathbf{v}^\top keeps every pairwise product in its own cell. The two look alike on paper, so check the transpose: it goes on the second vector for the outer product and on the first for the dot product.

It is tempting to think that uv⊤\mathbf{u}\mathbf{v}^\top and vu⊤\mathbf{v}\mathbf{u}^\top are the same. In fact the second is the transpose of the first, so they have different shapes unless m=nm = n, and even then they are generally different. Order matters.

It is tempting to think that an outer product can hold a lot of information. In fact it has rank one: every row is a multiple of one row, so it captures a single pattern. Information in larger matrices comes from adding several outer products, as the low-rank and covariance examples show.

When to use it. Use the outer product when you want a matrix built from two lists, when you are summing per-example updates in learning, or when you are building a rank-one piece of a decomposition.

When not to use it. Do not use it when you only need a single number from two vectors (use the dot product), or when the two vectors are very large and you can avoid storing the full grid. The result has m×nm \times n entries, which can be far more than the m+nm + n numbers that define it.

Alternatives. The Tensor Product generalises the outer product to more than two dimensions and to abstract spaces. Writing the result as a product of a column and a row, as in uv⊤\mathbf{u}\mathbf{v}^\top, is the simplest form.

History

Before the mid-1800s, matrices were mostly studied as tables of coefficients for systems of equations, and the vector and matrix notations we use today were still forming. In the late 1800s, Josiah Willard Gibbs and Oliver Heaviside developed vector analysis as a language for physics. Gibbs called products of two vectors that produce a tensor-like object dyadics, and the outer product is the matrix form of that idea.

The construction caught on in the 20th century because statistics and engineering needed matrices built from data. Covariance matrices, least squares and later neural networks all rely on sums of outer products. Today it is one of the standard operations in numerical libraries.

Recap

  • A vector is a list of numbers, and a matrix is a grid of numbers. The transpose turns a column into a row.
  • The outer product uv⊤\mathbf{u}\mathbf{v}^\top makes an m×nm \times n grid whose entry in row ii, column jj is uivju_i v_j.
  • Every outer product has rank one: its rows are all multiples of one row.
  • Adding outer products builds larger matrices, such as covariance matrices and low-rank approximations.
  • Read next: Low-Rank Matrix for matrices built from a few outer products, and Covariance Matrix for the statistics use.

Check yourself

QuestionWhat is the size of the outer product of a column with 4 entries and a column with 3 entries?Show answer
It is a 4×34 \times 3 matrix. The first vector gives the rows, one per entry, and the second gives the columns. The entry in row ii, column jj is the ii-th entry of the first vector times the jj-th entry of the second.
QuestionCompute uv⊤\mathbf{u}\mathbf{v}^\top for u=(1,3)\mathbf{u} = (1, 3) and v=(2,−1)\mathbf{v} = (2, -1).Show answer
Row 1 is 1×(2,−1)=(2,−1)1 \times (2, -1) = (2, -1). Row 2 is 3×(2,−1)=(6,−3)3 \times (2, -1) = (6, -3). The result is (2−16−3)\begin{pmatrix} 2 & -1 \\ 6 & -3 \end{pmatrix}. Check: the second row is three times the first row, as expected for a rank-one matrix.
QuestionWhy does the outer product have only one independent row, even when the vectors are long?Show answer
Each row is the same vector v⊤\mathbf{v}^\top scaled by one entry of u\mathbf{u}. Every row is therefore a multiple of one row, so only one row carries new information. That is why the matrix has rank one, however long the vectors are.
QuestionA training step produces the output error δ=(0.5,0.5)\boldsymbol{\delta} = (0.5, 0.5) and input x=(4,0)\mathbf{x} = (4, 0). Explain in words what the gradient δx⊤\boldsymbol{\delta}\mathbf{x}^\top says about the weights from the second input.Show answer
The gradient is (2020)\begin{pmatrix} 2 & 0 \\ 2 & 0 \end{pmatrix}. Both weights from the second input have gradient zero, because that input is zero. The training step does not change any weight that multiplies a zero input, since the output does not depend on it in this example.

Sources