The Transpose
The transpose of a matrix turns its rows into columns. It is a simple operation with many uses: it writes dot products as matrix products, defines symmetric matrices, and appears in every gradient formula of a neural network.
Definition
The transpose of an matrix is the matrix with
Row 1 of becomes column 1 of , and so on. Geometrically, a square matrix is reflected across its main diagonal; the diagonal stays put. The transpose of a column vector is a row vector, which is why textbooks often write column vectors as to save space.
Rules
| Rule | Comment |
|---|---|
| transposing twice restores | |
| the order reverses | |
| for invertible |
Proof of the reversal rule. Let be and be . Then
The sizes also force the order: is and is , so is , as must be, while is usually undefined, and when it is defined (as in the example below, where ) it has the wrong size, instead of .
from sympy import Matrix
A = Matrix([[1, 2, 3], [4, 5, 6]])
B = Matrix([[1, 0], [-1, 2], [3, 1]])
print("A^T =", A.T.tolist())
print("(AB)^T == B^T A^T:", (A * B).T == B.T * A.T)
print("shape of (AB)^T:", (A * B).T.shape, " shape of A^T B^T:", (A.T * B.T).shape)A^T = [[1, 4], [2, 5], [3, 6]]
(AB)^T == B^T A^T: True
shape of (AB)^T: (2, 2) shape of A^T B^T: (3, 3)
Dot products as matrix products
For column vectors in , is a matrix whose only entry is the dot product:
The other order, , is an matrix called the outer product. Combining the two ideas with the reversal rule gives a useful identity: . The transpose is the matrix that "moves across" a dot product.
Symmetric and skew-symmetric matrices
A square matrix is symmetric if (mirror image across the diagonal) and skew-symmetric if (which forces zeros on the diagonal).
- For any matrix , the products and are symmetric: .
- Every square matrix splits uniquely as symmetric plus skew-symmetric: .
from sympy import Matrix
X = Matrix([[1, 2], [0, 1], [3, -1]])
G = X.T * X
print("X^T X =", G.tolist(), " symmetric:", G == G.T)
A = Matrix([[4, 1, -2], [5, 0, 3], [2, 7, 1]])
S = (A + A.T) / 2
K = (A - A.T) / 2
print("S =", S.tolist())
print("K =", K.tolist())
print("S symmetric:", S == S.T, " K skew:", K == -K.T, " S + K == A:", S + K == A)X^T X = [[10, -1], [-1, 6]] symmetric: True
S = [[4, 3, 0], [3, 0, 5], [0, 5, 1]]
K = [[0, -2, -2], [2, 0, -2], [2, 2, 0]]
S symmetric: True K skew: True S + K == A: True
Symmetric matrices have especially nice eigenvalues and eigenvectors (Diagonalization of Real Symmetric Matrices).
In software
.Tin NumPy is free. It returns a view with swapped strides; no data is copied. A row-major array viewed as transposed is laid out column-major, which is how one buffer can serve both C (row-major) and Fortran/GLSL (column-major) code. Uploading a matrix to OpenGL with the wrong convention transposes it, a classic graphics bug.- Gram matrices in ML. For a data matrix (), is ; after centering the columns, is the covariance matrix used by PCA, and the least-squares normal equations are (least squares).
- Backpropagation. For a layer , the gradients are and . The transposes are exactly what make the shapes match (Backpropagation).
- Normals in graphics. When a mesh is transformed by with non-uniform scaling, surface normals must be transformed by , the inverse transpose, to stay perpendicular to the surface.
import numpy as np
A = np.arange(6).reshape(2, 3)
At = A.T
print("shares memory:", np.shares_memory(A, At))
print("strides A:", A.strides, " strides A.T:", At.strides)
print("C-contiguous A.T:", At.flags["C_CONTIGUOUS"], " F-contiguous A.T:", At.flags["F_CONTIGUOUS"])shares memory: True
strides A: (12, 4) strides A.T: (4, 12)
C-contiguous A.T: False F-contiguous A.T: True
Exercises
Exercise 1: Transpose by hand
Write the transpose of and give its size.
Answer
, size .
Solution
Row 1 becomes column 1 and row 2 becomes column 2, so has 4 rows and 2 columns.
Exercise 2: Symmetric completion
Fill in the blanks so that is symmetric.
Answer
, , .
Solution
Symmetric means : , , .
Exercise 3: Prove a product is symmetric
Show that if is symmetric then is symmetric for any of compatible size.
Answer
.
Solution
from sympy import Matrix
A = Matrix([[2, 1], [1, 3]])
B = Matrix([[1, 4, 0], [2, -1, 5]])
M = B.T * A * B
print(M.tolist(), M == M.T)[[18, 9, 35], [9, 27, 5], [35, 5, 75]] True
Exercise 4: The gradient shapes
A layer computes with of shape (32, 10) and of shape (10, 4). The upstream gradient has shape (32, 4). Compute the shapes of and in NumPy and say which gradient each is.
Answer
is (10, 4), the gradient for ; is (32, 10), the gradient for .
Solution
Each gradient must have the shape of the thing it is the gradient of, and the transposes are what make the inner sizes match.
import numpy as np
X = np.ones((32, 10)); W = np.ones((10, 4)); dY = np.ones((32, 4))
print("dW:", (X.T @ dY).shape, " W:", W.shape)
print("dX:", (dY @ W.T).shape, " X:", X.shape)dW: (10, 4) W: (10, 4)
dX: (32, 10) X: (32, 10)
Test yourself
is . What is the size of ?
- Undefined
What is ?
Which matrix is skew-symmetric?