Where We Are
We just learned Matrix Backpropagation: dW, db, dX by Hand.
But using it creates a new question.
What problem does this idea still leave us unable to solve?
That question leads naturally to Tensors: Numbers in Many Dimensions.
Next → Tensors: Numbers in Many Dimensions.
1. The Problem: One Neuron Was Too Small
Our previous network had
with one input and one weight. Real layers look more like this:
Suppose two students each have three scores — maths, science and English — and we want two outputs: a science-strength score and an overall readiness score.
For a batch of two students:
and let the layer have two outputs:
The forward pass is easy. But now ask the training question:
If the final loss changes, how much should every entry of , every entry of , and every input value in change?
There are already numbers involved. We need a rule that works for all of them, not fourteen separate tricks.
2. What Would a Solution Need?
A useful backward calculation must:
- give one gradient for every weight;
- give one gradient for every bias;
- give one gradient for every input value;
- preserve matrix shapes so a wrong formula can be caught before arithmetic;
- reuse the same intermediate values rather than recomputing every path separately.
The last requirement is the reason backpropagation becomes efficient rather than a giant collection of repeated algebra.
3. First Attempt: Different Formula for Every Weight
One tempting strategy is to differentiate with respect to , then , then , and so on.
For six weights this is annoying. For six million it is absurd.
A Tempting Wrong Idea
“Because every weight is different, every derivative needs a different derivation.”
No. The local computation is the same for every connection. The only thing that changes is which input and which downstream error signal meet at that connection.
So the real task is to discover the pattern.
4. The Discovery: One Weight Receives Two Pieces of Evidence
Start with a single entry of the matrix.
For one output neuron,
Pick one weight, .
Changing changes in proportion to the corresponding input :
Backpropagation also tells us how much the loss cares about :
Chain rule joins the two pieces:
That is the key pattern:
weight gradient = input × downstream gradient.
The formula is simple because the computation is simple: a weight only acts by multiplying its input.
5. Three Levels of the Same Idea
| Level | The same idea |
|---|---|
| 💡 Intuition | Each connection asks: “How much error was waiting downstream, and how much input was flowing through me?” Multiply those two numbers. |
| ✏️ Tiny numbers | If and , then . |
| 🎓 Abstraction | For every connection, is built by pairing each input with the corresponding output-side gradient. |
Now the scalar pattern is visible. The only remaining question is how to write all those pairings at once.
6. Two Students Instead of One: Why a Matrix Appears
Return to our batch.
The output is
so
Suppose the gradient arriving from the next part of the network is
Read in plain English:
entry says how sensitive the loss is to output .
Now inspect one weight gradient by hand.
For , the first input of each student contributes:
For :
Notice what happened: we multiplied input values by output gradients and added over the batch.
That is exactly matrix multiplication.
7. The Discovery:
The transpose appears because we want the dimensions to line up.
and
which is exactly the shape of .
So the whole weight gradient is
For our numbers:
therefore
Check the first column by hand:
The matrix formula did not appear from nowhere. It is just the scalar chain rule repeated and packed efficiently.
8. What About the Bias?
The bias is easier because
and
So every example simply contributes its downstream gradient.
For our batch:
Hence
If has shape , then has shape . The sum is across the batch axis because each example touches the same shared bias.
Think — Why do we sum the bias gradient but not the weight gradient in exactly the same notation?
The weight already has an input-specific multiplier. The bias has no input. Every example pushes directly on the same bias value, so their effects add.
9. Now Reverse the Question: What Is ?
A weight can ask, “How much did I affect the loss?”
The input asks the opposite:
“How much did the downstream layer care about me?”
Take one input entry .
Because
we have
There are several output neurons, so their effects add:
Pack the sums into a matrix product:
Check the shapes:
which matches the shape of .
Using our values:
so
Again, the shape is a proof-check. If someone wrote here, the dimensions would be — wrong shape, wrong answer.
10. One Layer, Three Gradients
We have reached the compact backward rule for a dense layer:
The forward pass was
The backward pass mirrors it:
flowchart LR
X[Input X] --> M[Z = XW + b]
W[Weights W] --> M
b[Bias b] --> M
M --> L[Loss downstream]
L -. dZ .-> BW[dW = Xᵀ dZ]
L -. dZ .-> BB[db = sum dZ]
L -. dZ .-> BX[dX = dZ Wᵀ]
This is the matrix version of the scalar chain rule from Chapter 083.
11. Shapes Catch Mistakes Before Arithmetic
Write the shapes first:
| Quantity | Shape |
|---|---|
Then verify:
and
This is more than notation. Shape algebra is a debugging tool.
12. 🔬 The Experiment: Change One Number
Take the same layer and change only the first student’s first feature.
The first column of tells you how that feature influences the corresponding weights. Predict:
What happens to when doubles while everything else stays fixed?
The answer should not require running code. From
the contribution from the first sample doubles. Other samples are unchanged.
This is exactly what a derivative should mean: a local sensitivity with respect to one quantity while the others are held fixed.
13. History Lens — Rumelhart, Hinton and Williams
📜 History Lens — the 1980s
Imagine having the chain rule but no convenient matrix notation for a layered neural network. David Rumelhart, Geoffrey Hinton and Ronald Williams published the influential 1986 paper Learning representations by back-propagating errors, showing a practical procedure for propagating error derivatives through multilayer networks.
The important historical lesson is not that the chain rule was new. It was not. The breakthrough was turning a familiar mathematical idea into a systematic training method for layered representations.
Our matrix formulas are the same philosophy made explicit: reuse local derivatives and arrange the repeated arithmetic as matrix operations.
14. 🎯 Machine Learning Connection
Every dense layer in a neural network performs a transformation like
During training, the framework repeatedly needs exactly these three quantities:
The optimizer usually updates parameters using
The input gradient is not normally updated by the optimizer. It is passed backward to the previous layer so that the previous layer can compute its gradients.
That is why deep networks can have dozens or hundreds of layers without requiring a new kind of mathematics for every layer.
Distinctions That Matter
| Do not confuse | With |
|---|---|
| the weights themselves | |
| the input data | |
| the output activations | |
| backpropagation | parameter update |
| transpose | inverse |
| shape compatibility | numerical correctness |
A transpose changes how a matrix is arranged; it is not an inverse.
What We Discovered
- Every scalar weight gradient is input × downstream gradient.
- A matrix is a compact way to compute all those scalar products at once.
- The transpose in is forced by which dimensions must pair.
- Bias gradients add over examples because one shared bias affects every example.
- is the downstream gradient moved backward through the transpose of the weight matrix.
- Shapes let us reject many wrong formulas before calculating a single number.
Mathematics We Built
Forward:
Backward:
These are not three unrelated memorized formulas. They are one chain-rule idea expressed for a matrix operation.
What Each Symbol Means
| Symbol | Read it as | Meaning | In code |
|---|---|---|---|
| input | batch of feature rows | X | |
| weights | learned connections | W | |
| bias | one offset per output | b | |
| pre-activation | layer output before activation | Z | |
| loss gradient wrt | downstream sensitivity | dZ | |
| loss gradient wrt | how each weight affects loss | dW | |
| loss gradient wrt | how each bias affects loss | db | |
| loss gradient wrt | signal sent to previous layer | dX | |
| batch size | number of examples | batch_size | |
| input width | number of input features | input_features | |
| output width | number of neurons | output_features |
One-Minute Explanation
A dense layer takes a matrix of inputs, multiplies it by a weight matrix, and adds a bias. During backpropagation, the layer receives a matrix of “how much the loss cares” about each output.
To find how much the loss cares about each weight, pair the input that flowed through that weight with the downstream gradient, and sum across examples: .
To find the bias gradient, sum the downstream gradients: .
To send the error toward the previous layer, multiply the downstream gradient by the transposed weights: .
The formulas are just the chain rule organized so a matrix library can compute many derivatives together.
Exercises
Level 1 — Observe
Look at
What shape must have? What shape must have?
Level 2 — Calculate
For one connection, and . Calculate .
Level 3 — Derive
Starting from
derive using the chain rule.
Level 4 — Investigate
In the notebook, double one row of while holding , and fixed. Predict which rows or columns of change, then test your prediction.
Level 5 — Design
Design a tiny two-layer network where you can compute every gradient by hand. Choose shapes that make matrix multiplication possible, then verify the gradients with a numerical finite-difference check.
Common Mistakes
| Mistake | Why it is wrong |
|---|---|
| Writing | shapes usually do not match; the input features must be summed in the correct orientation |
| Forgetting the batch sum for | every example uses the same bias |
| Using instead of for | the gradient must have the same shape as |
| Treating as an updated weight | a gradient tells the optimizer which direction to move |
| Ignoring shapes | a formula can look plausible while being mathematically impossible |
Socratic Questions
Why does each weight gradient contain the corresponding input?
Why do examples add together in the bias gradient?
Why is the transpose forced in the formula for ?
Why can a wrong gradient formula sometimes still produce numbers without immediately crashing?
What would happen if the layer had no bias?
🔭 Bridge to Chapter 086
We can now differentiate a matrix layer completely by hand.
But a real image is not naturally represented by one matrix. A batch of RGB images has four meaningful axes: batch, channel, height and width. Sequences, videos and feature maps need even more.
Next question: What single mathematical container can hold numbers in any number of dimensions while still giving us shapes and operations?
That container is the tensor.