Foundations

Matrix multiplication as transformation

Matrix multiplication composes learned changes of coordinates, while shape and rank reveal what information can pass through.

Updated

1

Concept

Matrix multiplication is easiest to understand as a transformation, not a grid-filling ritual. A matrix WW takes coordinates in one space and produces coordinates in another. For an input vector xx, each output component of WxWx is a dot product between one row of WW and xx. The rows therefore ask different learned weighted questions about the same input.

Consider

W=[2001/2].W=\begin{bmatrix}2&0\\0&1/2\end{bmatrix}.

Applied to (x1,x2)(x_1,x_2), it produces (2x1,x2/2)(2x_1,x_2/2). A square drawn in the plane becomes a rectangle: the horizontal direction stretches while the vertical direction shrinks. Other matrices rotate, reflect, shear, or project. In higher-dimensional neural networks, the pictures disappear but the operation remains a learned change of representation.

A rectangular matrix can also change dimensionality. An m×nm\times n matrix maps nn input features to mm output features. If m<nm<n, it may compress information; if m>nm>n, it embeds the input in a larger coordinate space but does not magically create independent information. The matrix’s rank measures how many independent directions survive. A low-rank map forces outputs into a smaller subspace, an idea later exploited by parameter-efficient fine-tuning.

Matrix-matrix multiplication performs many transformations or examples together. Entry (i,j)(i,j) of ABAB is the dot product of row ii of AA with column jj of BB. More importantly, ABAB represents composition: BB acts first on a vector, then AA. Matrix multiplication is generally not commutative. ABAB and BABA can differ, and one may be defined when the other is not. Shape records which compositions make sense.

Neural layers commonly use y=Wx+by=Wx+b. The matrix supplies a linear transformation; the bias vector translates every result. This is technically an affine transformation because zero can map to bb rather than zero. Bias lets a decision boundary move away from the origin. Implementations may store examples as rows and write XW+bXW+b, but this is only a convention. Clear shape annotations prevent transpose confusion.

If we stack two linear layers without anything between them, W2(W1x)=(W2W1)xW_2(W_1x)=(W_2W_1)x. The stack is equivalent to one matrix. Biases also combine into one affine map. Depth becomes expressive only when nonlinear functions, gating, normalization, or input-dependent routing interrupt that collapse. This is why activation functions appear between learned projections.

Matrix multiplication dominates neural computation because hardware can execute dense blocks efficiently. A batch of token vectors multiplied by one weight matrix reuses the same parameters across positions. GPUs and specialized accelerators divide large products into tiles, move them through memory hierarchies, and accumulate partial sums. The algebra is simple; performance often depends on data movement, shapes, precision, and how well operations are fused.

Transformers repeatedly apply this pattern. Embedding matrices map token identities to features. Attention projections create queries, keys, and values. Output projections recombine heads. Feed-forward layers expand and contract feature dimensions. The language-model head maps hidden states to vocabulary logits. Thinking in transformations makes the architecture legible: at every matrix, ask which space enters, which space leaves, what directions may be preserved, and what information could be lost.

2

Explain it like I am five

A theater lighting desk maps a few control sliders to many lamps. One matrix says how each slider affects red, green, and blue channels across the stage. A second matrix maps the resulting colors to camera sensors. Multiplying the matrices builds one combined cue from slider to camera. The order cannot be swapped: adjusting lights and then recording them is not the same operation as trying to light a recording.

3

Teach it back

Describe matrix multiplication as composition of transformations, explain why order matters, and state what a bias adds to a neural layer.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

A matrix maps input coordinates to output coordinates through weighted sums. If A acts first and B second, BA is the combined transformation; AB usually means a different operation or may not be shape-valid. A purely linear map must send zero to zero. Adding a bias translates every output by the same learned vector, producing an affine layer y=Wx+b. Nonlinear activations between such layers prevent the whole stack from collapsing into one matrix.

4

Check your understanding

1. If A acts on x and B acts on the result, what is the combined expression?
Answer and explanation

B(Ax)=(BA)x — Function composition reads from right to left: A transforms the input first, then B transforms that output.

2. Why does stacking only linear matrix multiplications not create a deep nonlinear model?
Answer and explanation

Their product is another linear transformation — Without nonlinearities, multiple matrices compose into a single equivalent matrix.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Ian Goodfellow, Yoshua Bengio, and Aaron Courville (2016). Deep Learning.