Matrix transpose and inverse matrix
Matrix transpose and inverse matrix
Transpose frequently appears in attention mechanisms, and the inverse matrix helps us understand "invertible transformations"—such as coordinate transformations in PCA.
Transpose: swapping rows and columns
A (2×3)
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
AT (3x2)
| 1 | 4 |
|---|---|
| 2 | 5 |
| 3 | 6 |
The value at position (i, j) originally moves to position (j, i) after transposition. The shape changes from m×n to n×m.
Inverse matrix: undo the transformation
Matrix A represents a linear transformation. ( A^{-1} ) represents "undoing the effect of A".
Mathematical expression: ( AA^{-1} = A^{-1}A = I ) (I is the identity matrix, equivalent to the number 1).
OnlySquare matrix and full rankOnly square matrices have an inverse matrix.
Non-square matrices or square matrices with insufficient rank are "singular matrices," which have no inverse—equivalent to an irreversible operation.
Key properties
- Transpose of a product reverses the order: ( (\mathbf{AB})^T = \mathbf{B}^T\mathbf{A}^T )
- Inverse of a product also reverses the order: ( (\mathbf{AB})^{-1} = \mathbf{B}^{-1}\mathbf{A}^{-1} )
Real-life examples
**Transpose: Swapping rows and columns in an Excel spreadsheet
After transposing a sales table with "names in rows and months in columns," it becomes "names in columns and months in rows."
The data hasn't changed, only the arrangement—that is transposition.
**Inverse Matrix: Ctrl+Z Undo
You performed a rotation + scaling operation (matrix A) and want to revert it (A^{-1}).
Two operations composed = nothing changed (AA^{-1} = I, identity transformation).
Mathematical definition
Transpose
[ (\mathbf{A}^T){ij} = \mathbf{A}{ji} ]
Inverse matrix
For an n×n matrix A, if there exists B such that ( \mathbf{AB} = \mathbf{BA} = \mathbf{I}_n ) then B = A^{-1}$.
Python hands-on practice
Example
import
numpy
as
np
A
=
np.
array
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
)
(
"A (2x3):\n"
,
A
)
(
"A.T (3x2):\n"
,
A.
T
)
(
)
Verify (AB)^T = B^T @ A^T
B
=
np.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
,
[
5
,
6
]
]
)
3x2
AB
=
A
@
B
(2,3) @ (3,2) = (2,2)
(
"(AB)^T:\n"
,
AB.
T
)
(
"B^T @ A^T:\n"
,
B.
T
@
A.
T
)
(
"Equal:"
,
np.
allclose
(
AB.
T
,
B.
T
@
A.
T
)
)
(
)
Inverse matrix
C
=
np.
array
(
[
[
2
,
1
]
,
[
5
,
3
]
]
)
2x2 array
C_inv
=
np.
linalg
.
inv
(
C
)
(
"C:\n"
,
C
)
(
"C^{-1}:\n"
,
C_inv
)
(
"C @ C^{-1} = I:\n"
,
C
@
C_inv
)
Singular matrices have no inverse
D
=
np.
array
(
[
[
1
,
2
]
,
[
2
,
4
]
]
)
The second row is twice the first row
try
:
np.
linalg
.
inv
(
D
)
except
np.
linalg
.
LinAlgError
:
(
"\nSingular matrix D has no inverse matrix (row 2 = 2 * row 1)"
)
Original prompt (Chinese, left untranslated)
A (2x3):
[[1 2 3]
[4 5 6]]
A.T (3x2):
[[1 4]
[2 5]
[3 6]]
C @ C^{-1} = I:
[[1. 0.]
[0. 1.]]
奇异矩阵 D 没有逆矩阵(第2行=2×第1行)
Application scenarios in AI
K^T operation of the attention mechanism
Transposing the Key matrix in (QK^T) allows Q of shape (seq_len, d_k) to perform matrix multiplication with K of shape (seq_len, d_k) — the transpose changes K to (d_k, seq_len), matching the inner dimensions. This K^T is the most critical dimensional transformation in all Transformer model attention calculations.
Closed-form solution for linear regression
( \hat{\beta} = (X^TX)^{-1}X^Ty ) — both transpose and inverse matrices appear. X^T X ensures the result is a symmetric positive definite matrix (invertible), and then the inverse is taken to get the analytical solution. sklearn's LinearRegression calculates this internally without regularization.
Parameter update in gradient descent
During backpropagation, the shape of the weight gradient matrix must match the weight matrix to perform subtraction updates. For example, if W is (d_out, d_in), its gradient \nabla_W L is also (d_out, d_in) — the transpose helps the gradient correctly propagate dimensions in the computation graph.