Derive Backpropagation for Matrix-Product Layers
OpenAIMachine Learning Engineer · Technical ScreenLast updated
This question evaluates understanding of backpropagation through linear blocks, matrix calculus, and the computation of gradients with respect to individual weight matrices in a product of trainable matrices, including reasoning about tensor shapes and matrix differentials.
Consider a neural-network block whose output is produced by multiplying a sequence of trainable weight matrices together, then applying the resulting composite matrix to an input. There is no nonlinearity inside this block — it is a pure chain of matrix multiplications.
Let the trainable matrices be $W_1, W_2, \ldots, W_n$. Their cumulative product defines a single effective linear map
$$ C = W_1 W_2 \cdots W_n . $$
Given an input vector or mini-batch $X$, the forward pass is
$$ Z = C,X = W_1 W_2 \cdots W_n, X . $$
There is a scalar loss $\mathcal{L}$, and the upstream gradient
$$ G = \frac{\partial \mathcal{L}}{\partial Z} $$
is supplied by the loss or by later layers (so $G$ has the same shape as $Z$).
Derive the backward pass for this block. Specifically:
- Express the gradient $\dfrac{\partial \mathcal{L}}{\partial W_j}$ with respect to each individual matrix $W_j$, for every $1 \le j \le n$.
- Show explicitly how the multivariate chain rule (or the matrix differential) applies to the matrix product — i.e. why the answer has the form it does, not just the final formula.
- Verify that the resulting gradient $\dfrac{\partial \mathcal{L}}{\partial W_j}$ has the same shape as $W_j$.
- Describe an efficient implementation that avoids recomputing the same prefix and suffix matrix products for every $j$.
Constraints & Assumptions
- Shapes: let $W_j \in \mathbb{R}^{d_{j-1} \times d_j}$, so the composite $C \in \mathbb{R}^{d_0 \times d_n}$. For a mini-batch, $X \in \mathbb{R}^{d_n \times B}$ and $Z, G \in \mathbb{R}^{d_0 \times B}$; for a single example take $B = 1$.
- The matrices need not be square; adjacent inner dimensions just have to match so the product is defined.
- There is no nonlinearity or bias term inside this block — it is a pure chain of matrix multiplications. Any activation lives in the layers that produce $G$.
- "Efficient" means linear in the number of matrices $n$: the whole backward pass should cost $O(n)$ matrix products, not $O(n^2)$, and should avoid materializing large $d_0 \times d_{j-1}$ prefix matrices when $B$ is small.
Clarifying Questions to Ask
- Is $X$ a single vector or a mini-batch, and is the loss summed or averaged over the batch (which scales every $dW_j$)?
- Should I treat $X$ as fixed input, or is the gradient $\partial\mathcal{L}/\partial X$ also required?
- Are the matrices guaranteed non-singular / square, or fully general rectangular?
- Is there any nonlinearity or bias term between the matrices, or is the block a pure linear chain as stated?
- What's the target metric — closed-form gradient formula, an $O(n)$ algorithm, or working code/pseudocode?
- Do we ever need to materialize the composite $C$ at inference, or only its action $CX$?
Hints
Hint 1
Isolate one matrix: $W_j$ sits in the *middle* of the product, which makes the chain rule awkward. Try freezing every other matrix and viewing $Z$ as the composition of three pieces: a left **prefix** built from the matrices before $W_j$, the target $W_j$ itself, and a right **suffix** built from the matrices after it (the suffix can absorb $X$). Relative to $W_j$ alone, what kind of map is this — and what does that buy you?
Hint 2
Differentiate the isolated form: Once $W_j$ is isolated, perturb only $W_j$ and ask how $Z$ changes to first order. For a *scalar* loss, the contraction of the upstream gradient against a matrix change is naturally written with a trace: $d\mathcal{L} = \operatorname{tr}(G^\top\, dZ)$. The trace's invariance under cyclic shifts is the tool that lets you collect everything multiplying $dW_j$ into one factor. Be careful about which side each frozen piece lands on and where transposes appear.
Hint 3
Avoid the $O(n^2)$ trap: The per-matrix formula re-uses overlapping prefix/suffix products, so recomputing them for each $j$ is quadratic. Ask: do you ever need the prefix as a full *matrix*, or only its action on the upstream gradient? And can the suffix pieces all be obtained from a single pass in one direction? Two coordinated sweeps in opposite directions should be enough to produce every gradient in linear time.
Follow-up questions
- How does the cost and the formula change if you also need $\partial\mathcal{L}/\partial X$?
- Numerically, why is composing the matrices into a single $C$ and backpropping through $C$ alone *not* equivalent — what information about the individual $W_j$ would you lose?
- If $n$ is large, what conditioning / vanishing-or-exploding-gradient issues arise from the repeated products, and how would you mitigate them?
- Suppose two of the matrices are tied (the *same* parameter reused at positions $j$ and $k$). How does the gradient w.r.t. that shared parameter change?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.