OpenAI · ML & AI Fundamentals
Derive Backpropagation for Matrix-Product Layers
TrueInterview
October 7, 2026 · 4 min read
Think of a neural-network block that forms its output by first multiplying a sequence of trainable weight matrices, then applying that composite matrix to an input. The block has no nonlinearity inside it — it is purely a chain of matrix multiplications.
Denote the trainable matrices by . Their product defines one effective linear map
For an input vector or mini-batch , the forward computation is
A scalar loss is given, and the upstream gradient
is provided by the loss or by downstream layers (so has the same shape as ).
Derive the backward pass for this block. In particular:
- Give the gradient for each individual matrix , for all .
- Show explicitly how the multivariate chain rule (or matrix differential) applies to the matrix product — that is, why the answer takes the form it does, not merely the final formula.
- Check that the resulting gradient has the same shape as .
- Describe an efficient implementation that does not recompute the same prefix and suffix matrix products for every .
Hint: Isolate one matrix. sits in the middle of the product, which makes the chain rule awkward. Try holding every other matrix fixed and viewing as a composition of three pieces: a left prefix formed by the matrices before , the target itself, and a right suffix formed by the matrices after it (the suffix may absorb ). Relative to alone, what kind of map is this — and what does that give you?
Hint: Differentiate the isolated form. Once is isolated, perturb only and ask how changes to first order. For a scalar loss, the contraction of the upstream gradient with a matrix change is naturally written as a trace: . The cyclic invariance of the trace is what lets you gather everything multiplying into a single factor. Be careful about which side each frozen piece ends up on and where transposes appear.
Hint: Avoid the trap. The per-matrix formula reuses overlapping prefix/suffix products, so recomputing them for every is quadratic. Ask: do you ever need the prefix as a full matrix, or only its action on the upstream gradient? And can all suffix pieces 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.
Constraints & Assumptions
- Shapes: let , so the composite . For a mini-batch, and ; for a single example take .
- The matrices need not be square; adjacent inner dimensions only 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 .
- "Efficient" means linear in the number of matrices : the whole backward pass should cost matrix products, not , and should avoid materializing large prefix matrices when is small.
Clarifying Questions to Ask
- Is a single vector or a mini-batch, and is the loss summed or averaged over the batch (which scales every )?
- Should I treat as fixed input, or is the gradient 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 is the target deliverable — closed-form gradient formula, an algorithm, or working code/pseudocode?
- Do we ever need to materialize the composite at inference, or only its action ?
What a Strong Answer Covers
- Cleanly isolates a single by grouping the rest of the product into a left prefix and a right suffix, and states the empty-product-is-identity convention.
- A genuine derivation (matrix differential + trace cyclic property, or an equivalent index/chain-rule argument) that produces the per-matrix gradient rather than asserting it, with transposes and left/right placement justified.
- A closed-form expression for in terms of the prefix, the upstream gradient , and the suffix.
- An explicit dimension check confirming the gradient has the same shape as (i.e. ), with the batch dimension correctly contracted away.
- A linear-time () algorithm that computes all gradients without recomputing overlapping products or materializing the large prefix matrices — i.e. coordinated sweeps in both directions, with correct transpose placement.
- Awareness of practical concerns: batch averaging applied exactly once, and the link to standard reverse-mode autodiff over a linear chain.
Follow-up Questions
- How do the cost and the formula change if you also need ?
- Numerically, why is composing the matrices into a single and backpropagating through alone not equivalent — what information about the individual would you lose?
- If 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 and ). How does the gradient with respect to that shared parameter change?
Overview: This question tests understanding of backpropagation through linear blocks, matrix calculus, and computing gradients with respect to individual weight matrices in a product of trainable matrices, including reasoning about tensor shapes and matrix differentials.