TensorTau PathToAGI Neural Networks from First Principles Level 06

Level 06 · Neural Networks from First Principles · Chapter 085

Matrix Backpropagation: dW, db, dX by Hand

We just backpropagated through one number at a time. What changes when one layer transforms an entire matrix of numbers at once?

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

z=wxz=wx

with one input and one weight. Real layers look more like this:

Z=XW+bZ = XW + b

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:

X=[123214]X= \begin{bmatrix} 1&2&3\\ 2&1&4 \end{bmatrix}

and let the layer have two outputs:

W=[100211],b=[12]W= \begin{bmatrix} 1&0\\ 0&2\\ 1&1 \end{bmatrix}, \qquad b= \begin{bmatrix} 1&2 \end{bmatrix}

The forward pass is easy. But now ask the training question:

If the final loss changes, how much should every entry of WW, every entry of bb, and every input value in XX change?

There are already 6+2+6=146+2+6=14 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:

  1. give one gradient for every weight;
  2. give one gradient for every bias;
  3. give one gradient for every input value;
  4. preserve matrix shapes so a wrong formula can be caught before arithmetic;
  5. 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 LL with respect to w11w_{11}, then w12w_{12}, then w21w_{21}, 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,

zj=∑ixiwij+bjz_j = \sum_i x_i w_{ij} + b_j

Pick one weight, wijw_{ij}.

Changing wijw_{ij} changes zjz_j in proportion to the corresponding input xix_i:

∂zj∂wij=xi\frac{\partial z_j}{\partial w_{ij}} = x_i

Backpropagation also tells us how much the loss cares about zjz_j:

∂L∂zj\frac{\partial L}{\partial z_j}

Chain rule joins the two pieces:

∂L∂wij=∂L∂zjxi\boxed{\frac{\partial L}{\partial w_{ij}} = \frac{\partial L}{\partial z_j}x_i}

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

LevelThe same idea
💡 IntuitionEach connection asks: “How much error was waiting downstream, and how much input was flowing through me?” Multiply those two numbers.
✏️ Tiny numbersIf xi=3x_i=3 and dL/dzj=−2dL/dz_j=-2, then dL/dwij=3(−2)=−6dL/dw_{ij}=3(-2)=-6.
🎓 AbstractionFor every connection, dWdW 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.

X∈R2×3,W∈R3×2,b∈R1×2X\in\mathbb{R}^{2\times3}, \qquad W\in\mathbb{R}^{3\times2}, \qquad b\in\mathbb{R}^{1\times2}

The output is

Z=XW+bZ=XW+b

so

Z∈R2×2Z\in\mathbb{R}^{2\times2}

Suppose the gradient arriving from the next part of the network is

G=∂L∂Z=[12−13]G=\frac{\partial L}{\partial Z} = \begin{bmatrix} 1&2\\ -1&3 \end{bmatrix}

Read GG in plain English:

entry (r,j)(r,j) says how sensitive the loss is to output ZrjZ_{rj}.

Now inspect one weight gradient by hand.

For w11w_{11}, the first input of each student contributes:

∂L∂w11=1(1)+2(−1)=−1\frac{\partial L}{\partial w_{11}} =1(1)+2(-1)=-1

For w12w_{12}:

∂L∂w12=1(2)+2(3)=8\frac{\partial L}{\partial w_{12}} =1(2)+2(3)=8

Notice what happened: we multiplied input values by output gradients and added over the batch.

That is exactly matrix multiplication.


7. The Discovery: dW=XTdZdW = X^T dZ

The transpose appears because we want the dimensions to line up.

XT∈R3×2X^T\in\mathbb{R}^{3\times2}

and

XTdZ∈R3×2X^TdZ \in \mathbb{R}^{3\times2}

which is exactly the shape of WW.

So the whole weight gradient is

dW=XTdZ\boxed{dW=X^TdZ}

For our numbers:

XT=[122134]X^T= \begin{bmatrix} 1&2\\ 2&1\\ 3&4 \end{bmatrix}

therefore

XTdZ=[122134][12−13]=[−1817−118]X^TdZ= \begin{bmatrix} 1&2\\ 2&1\\ 3&4 \end{bmatrix} \begin{bmatrix} 1&2\\ -1&3 \end{bmatrix} = \begin{bmatrix} -1&8\\ 1&7\\ -1&18 \end{bmatrix}

Check the first column by hand:

[1(1)+2(−1)2(1)+1(−1)3(1)+4(−1)]=[−11−1]\begin{bmatrix} 1(1)+2(-1)\\ 2(1)+1(-1)\\ 3(1)+4(-1) \end{bmatrix} = \begin{bmatrix} -1\\1\\-1 \end{bmatrix}

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

zj=⋯+bjz_j=\cdots+b_j

and

∂zj∂bj=1\frac{\partial z_j}{\partial b_j}=1

So every example simply contributes its downstream gradient.

For our batch:

∂L∂b=[1+(−1)2+3]=[05]\frac{\partial L}{\partial b} = \begin{bmatrix} 1+(-1)&2+3 \end{bmatrix} = \begin{bmatrix} 0&5 \end{bmatrix}

Hence

db=∑batchdZ\boxed{db=\sum_{\text{batch}}dZ}

If dZdZ has shape (B,m)(B,m), then dbdb has shape (m,)(m,). 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 dXdX?

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 xix_i.

Because

zj=∑ixiwij+bj, z_j=\sum_i x_iw_{ij}+b_j,

we have

∂zj∂xi=wij\frac{\partial z_j}{\partial x_i}=w_{ij}

There are several output neurons, so their effects add:

∂L∂xi=∑j∂L∂zjwij\frac{\partial L}{\partial x_i} = \sum_j \frac{\partial L}{\partial z_j}w_{ij}

Pack the sums into a matrix product:

dX=dZWT\boxed{dX=dZW^T}

Check the shapes:

(2×2)(2×3)=(2×3)(2\times2)(2\times3)=(2\times3)

which matches the shape of XX.

Using our values:

WT=[101021]W^T= \begin{bmatrix} 1&0&1\\ 0&2&1 \end{bmatrix}

so

dX=[12−13][101021]=[143−162]dX= \begin{bmatrix} 1&2\\ -1&3 \end{bmatrix} \begin{bmatrix} 1&0&1\\ 0&2&1 \end{bmatrix} = \begin{bmatrix} 1&4&3\\ -1&6&2 \end{bmatrix}

Again, the shape is a proof-check. If someone wrote WdZW dZ here, the dimensions would be (3×2)(2×2)=(3×2)(3\times2)(2\times2)=(3\times2) — wrong shape, wrong answer.


10. One Layer, Three Gradients

We have reached the compact backward rule for a dense layer:

dW=XTdZdb=∑batchdZdX=dZWT\boxed{ \begin{aligned} dW &= X^T dZ\\ db &= \sum_{\text{batch}} dZ\\ dX &= dZW^T \end{aligned}}

The forward pass was

Z=XW+bZ=XW+b

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:

QuantityShape
XX(B,n)(B,n)
WW(n,m)(n,m)
bb(m,)(m,)
ZZ(B,m)(B,m)
dZdZ(B,m)(B,m)
dWdW(n,m)(n,m)
dbdb(m,)(m,)
dXdX(B,n)(B,n)

Then verify:

(B,n)T(n,m)=(n,B)(B,m)=(n,m)(B,n)^T(n,m)=(n,B)(B,m)=(n,m)

and

(B,m)(n,m)T=(B,m)(m,n)=(B,n)(B,m)(n,m)^T=(B,m)(m,n)=(B,n)

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 dWdW tells you how that feature influences the corresponding weights. Predict:

What happens to dWdW when x11x_{11} doubles while everything else stays fixed?

The answer should not require running code. From

dW=XTdZ,dW=X^TdZ,

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

Z=XW+b.Z=XW+b.

During training, the framework repeatedly needs exactly these three quantities:

dW,db,dX.dW,\quad db,\quad dX.

The optimizer usually updates parameters using

W←W−ηdW,b←b−ηdb.W\leftarrow W-\eta dW, \qquad b\leftarrow b-\eta db.

The input gradient dXdX 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 confuseWith
dWdWthe weights WW themselves
dXdXthe input data XX
dZdZthe output activations ZZ
backpropagationparameter update
transposeinverse
shape compatibilitynumerical correctness

A transpose changes how a matrix is arranged; it is not an inverse.


What We Discovered

  1. Every scalar weight gradient is input × downstream gradient.
  2. A matrix is a compact way to compute all those scalar products at once.
  3. The transpose in XTdZX^TdZ is forced by which dimensions must pair.
  4. Bias gradients add over examples because one shared bias affects every example.
  5. dXdX is the downstream gradient moved backward through the transpose of the weight matrix.
  6. Shapes let us reject many wrong formulas before calculating a single number.

Mathematics We Built

Forward:

Z=XW+bZ=XW+b

Backward:

dW=XTdZ\boxed{dW=X^TdZ} db=∑batchdZ\boxed{db=\sum_{\text{batch}}dZ} dX=dZWT\boxed{dX=dZW^T}

These are not three unrelated memorized formulas. They are one chain-rule idea expressed for a matrix operation.


What Each Symbol Means

SymbolRead it asMeaningIn code
XXinputbatch of feature rowsX
WWweightslearned connectionsW
bbbiasone offset per outputb
ZZpre-activationlayer output before activationZ
dZdZloss gradient wrt ZZdownstream sensitivitydZ
dWdWloss gradient wrt WWhow each weight affects lossdW
dbdbloss gradient wrt bbhow each bias affects lossdb
dXdXloss gradient wrt XXsignal sent to previous layerdX
BBbatch sizenumber of examplesbatch_size
nninput widthnumber of input featuresinput_features
mmoutput widthnumber of neuronsoutput_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: dW=XTdZdW=X^TdZ.

To find the bias gradient, sum the downstream gradients: db=∑dZdb=\sum dZ.

To send the error toward the previous layer, multiply the downstream gradient by the transposed weights: dX=dZWTdX=dZW^T.

The formulas are just the chain rule organized so a matrix library can compute many derivatives together.


Exercises

Level 1 — Observe

Look at

X∈R8×5,W∈R5×3.X\in\mathbb{R}^{8\times5},\qquad W\in\mathbb{R}^{5\times3}.

What shape must ZZ have? What shape must dWdW have?

Level 2 — Calculate

For one connection, xi=4x_i=4 and dZj=−0.5dZ_j=-0.5. Calculate dWijdW_{ij}.

Level 3 — Derive

Starting from

zj=∑ixiwij+bj,z_j=\sum_i x_iw_{ij}+b_j,

derive ∂L/∂wij\partial L/\partial w_{ij} using the chain rule.

Level 4 — Investigate

In the notebook, double one row of XX while holding WW, bb and dZdZ fixed. Predict which rows or columns of dWdW 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

MistakeWhy it is wrong
Writing dW=XdZdW=X dZshapes usually do not match; the input features must be summed in the correct orientation
Forgetting the batch sum for dbdbevery example uses the same bias
Using WW instead of WTW^T for dXdXthe gradient must have the same shape as XX
Treating dWdW as an updated weighta gradient tells the optimizer which direction to move
Ignoring shapesa 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 dXdX?

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.

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

Exercises should progress from understanding to research behaviour.

Level 1 — Explain it

Explain the idea in plain language without equations.

Level 2 — Calculate it

Solve a tiny numerical example by hand.

Level 3 — Predict it

Predict code output, model behaviour, tensor shapes, or graph movement before running anything.

Level 4 — Break it

Deliberately change an assumption or parameter and explain what fails.

Level 5 — Build it

Implement the core mechanism from scratch before using a high-level library.

Level 6 — Investigate it

Form a hypothesis, design an experiment, compare alternatives, and explain the evidence.