Vectors and matrices

vec⁡(W)\operatorname{vec}(W) Vectorization (vec)

Stack a matrix’s columns into one long vector.

In ML papers

vec⁡(A)\operatorname{vec}(A) flattens a matrix into a column vector by stacking its columns one under the other. An m×nm \times n matrix becomes a vector of length mnmn. It lets a paper treat a weight matrix as a single vector, for example to write a Fisher or Hessian matrix over all its entries.

It pairs with the Kronecker product (Tensor / Kronecker product) in the identity vec⁡(AXB)=(B⊤⊗A)vec⁡(X)\operatorname{vec}(AXB) = (B^\top \otimes A)\operatorname{vec}(X), which second-order methods such as K-FAC rely on.

How to type it

LaTeXShows
\operatorname{vec}(A)vec⁡(A)\operatorname{vec}(A)
\mathrm{vec}(A)vec(A)\mathrm{vec}(A)

A worked example

vec⁡(1234)=(1,3,2,4)⊤\operatorname{vec}\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} = (1, 3, 2, 4)^\top

Column by column. Row-major code (NumPy's flatten) would give (1,2,3,4)(1, 2, 3, 4) instead.

Common confusions

Related

a⊗b\mathbf{a} \otimes \mathbf{b}Tensor / Kronecker productW∈Rm×n\mathbf{W} \in \mathbb{R}^{m \times n}Bold uppercase (matrix)x⊤y\mathbf{x}^\top \mathbf{y}Transpose

Decode this example →