Backpropagation

Neural networks learn by adjusting their weights and biases to reduce errors in their predictions.

Backpropagation computes the gradients of the loss with respect to those parameters. An optimizer, such as gradient descent or Adam, uses the gradients to update the weights and biases.

This Lesson Will Cover
  • ๐Ÿ”„ The need for backpropagation in neural networks
  • ๐Ÿ“‰ Cost functions for measuring prediction errors
  • ๐Ÿ”— The chain rule and its role in backpropagation
  • ๐Ÿ—๏ธ Step-by-step walkthrough of the backpropagation algorithm
  • ๐Ÿงฎ Detailed gradient calculations with a practical example

Why Do We Need Backpropagation?

Imagine the task of manually changing the parameters of a neural network in order to improve performance. To make this clearer, imaging that we already have a good set of weights and biases, and we are simply trying to fine-tune them to produce slightly more accurate results, on some training data.

We know the network is making errors, but how do we adjust the weights and biases across multiple layers to fix these errors?

By propagating errors backward through the network, layer by layer, and computing gradients for parameter updates iteratively, backpropagation provides the solution.

Cost Function

In order to optimize the weights of our neural network, we need a way to quantify prediction errors.

For binary classification, we typically use binary cross-entropy loss (log loss):

J(W,b)=โˆ’1mโˆ‘l=1m[y[l]logโก(y^[l])+(1โˆ’y[l])logโก(1โˆ’y^[l])] J(\mathbf{W}, \mathbf{b}) = -\frac{1}{m}\sum_{l=1}^m [y^{[l]}\log(\hat{y}^{[l]}) + (1-y^{[l]})\log(1-\hat{y}^{[l]})]

Where:

  • mm is the number of training examples
  • y[l]y^{[l]} is the true label for example ll
  • y^[l]\hat{y}^{[l]} is our prediction (equal to a[3]\mathbf{a}^{[3]} in our example)
Binary Cross-Entropy Loss Curves
Binary cross-entropy loss for positive (purple) and negative (orange) class labels as a function of predicted probability. The curves show how the loss skyrockets when the model is confidently wrong and drops to zero for perfect predictions.

This should look familiar from our earlier lesson on optimization techniques.

The Chain Rule

Backpropagation relies heavily on the chain rule from calculus.

The chain rule states that:

โˆ‚โˆ‚xf(g(x))=โˆ‚fโˆ‚gโ‹…โˆ‚gโˆ‚x \frac{\partial}{\partial x}f(g(x)) = \frac{\partial f}{\partial g} \cdot \frac{\partial g}{\partial x}

In neural networks, we use this to calculate how changes in weights and biases affect the final cost:

โˆ‚Jโˆ‚W[l]=โˆ‚Jโˆ‚a[l]โ‹…โˆ‚a[l]โˆ‚z[l]โ‹…โˆ‚z[l]โˆ‚W[l] \frac{\partial J}{\partial \mathbf{W}^{[l]}} = \frac{\partial J}{\partial \mathbf{a}^{[l]}} \cdot \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} \cdot \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{W}^{[l]}}

Where:

  • JJ is the cost/loss function
  • W[l]\mathbf{W}^{[l]} represents the weight matrix for layer ll
  • a[l]\mathbf{a}^{[l]} is the activation output from layer ll
  • z[l]\mathbf{z}^{[l]} is the weighted input to layer ll before the activation function
  • โˆ‚โˆ‚x\frac{\partial}{\partial x} denotes the partial derivative with respect to variable xx

The Backpropagation Algorithm

A training step combines forward propagation, backpropagation and an optimizer update:

  1. Forward Propagation

    • Compute activations for each layer moving forward
    • Store intermediate values for backpropagation
  2. Compute Output Error

    • Calculate the difference between predicted and actual outputs
    • This gives us our initial error signal
  3. Backward Propagation

    • Propagate error backward through the network
    • Compute the gradients of the loss with respect to weights and biases.
  4. Optimizer Update

    • Use those gradients to update weights and biases. For gradient descent:
    W[l]โ†W[l]โˆ’ฮฑโˆ‚Jโˆ‚W[l] \mathbf{W}^{[l]} \leftarrow \mathbf{W}^{[l]} - \alpha \frac{\partial J}{\partial \mathbf{W}^{[l]}}
    b[l]โ†b[l]โˆ’ฮฑโˆ‚Jโˆ‚b[l] \mathbf{b}^{[l]} \leftarrow \mathbf{b}^{[l]} - \alpha \frac{\partial J}{\partial \mathbf{b}^{[l]}}

    Where ฮฑ\alpha is the learning rate that controls the size of weight updates.

Computing the Gradient

Letโ€™s walk through an example of doing a gradient update using backpropagation.

For a multi-layer network, we need to compute:

โˆ‚Jโˆ‚W[l]โ€…โ€Šandโ€…โ€Šโˆ‚Jโˆ‚b[l]โ€…โ€Š for l=1,2,3 \frac{\partial J}{\partial \mathbf{W}^{[l]}} \; \text{and} \; \frac{\partial J}{\partial \mathbf{b}^{[l]}} \; \text{ for } l = 1,2,3

Using the chain rule, these derivatives break down as:

For weights in layer l:

โˆ‚Jโˆ‚W[l]=โˆ‚Jโˆ‚a[l]โ‹…โˆ‚a[l]โˆ‚z[l]โ‹…โˆ‚z[l]โˆ‚W[l] \frac{\partial J}{\partial \mathbf{W}^{[l]}} = \frac{\partial J}{\partial \mathbf{a}^{[l]}} \cdot \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} \cdot \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{W}^{[l]}}

For biases in layer l:

โˆ‚Jโˆ‚b[l]=โˆ‚Jโˆ‚a[l]โ‹…โˆ‚a[l]โˆ‚z[l]โ‹…โˆ‚z[l]โˆ‚b[l] \frac{\partial J}{\partial \mathbf{b}^{[l]}} = \frac{\partial J}{\partial \mathbf{a}^{[l]}} \cdot \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} \cdot \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{b}^{[l]}}

ere:

  • โˆ‚Jโˆ‚a[l]\frac{\partial J}{\partial \mathbf{a}^{[l]}} is how the loss changes with respect to the layerโ€™s activation
  • โˆ‚a[l]โˆ‚z[l]\frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} is the derivative of the activation function
  • โˆ‚z[l]โˆ‚W[l]\frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{W}^{[l]}} is how the pre-activation changes with respect to weights
  • โˆ‚z[l]โˆ‚b[l]\frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{b}^{[l]}} is how the pre-activation changes with respect to biases

Example: Forward Propagation for a Multi-layer Neural Network

Letโ€™s return to the example that we were working through in the previous lesson.

As a reminder, this is what our architecture looks like:

  • Input Layer: 5-dimensional input vector
  • Hidden Layer 1: 4-dimensional, activation is ReLU
  • Hidden Layer 2: 7-dimensional, activation is ReLU
  • Output Layer: 2-dimensional, activation is Step function
Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Multi-layer neural network architecture diagram for our binary classification example.

We can visualize our network as follows:

Input Layer (x)[x1x2x3x4x5]โ†’W1,b1,ReLU[a1[1]a2[1]a3[1]a4[1]]โ†’W2,b2,Step[a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]]โ†’W3,b3[y1y2] \begin{aligned} &\text{Input Layer (x)} \\ &\begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} \quad \xrightarrow{W_1, b_1, \text{ReLU}} \quad \begin{bmatrix} a_1^{[1]} \\ a_2^{[1]} \\ a_3^{[1]} \\ a_4^{[1]} \end{bmatrix} \quad \xrightarrow{W_2, b_2, \text{Step}} \quad \begin{bmatrix} a_1^{[2]} \\ a_2^{[2]} \\ a_3^{[2]} \\ a_4^{[2]} \\ a_5^{[2]} \\ a_6^{[2]} \\ a_7^{[2]} \end{bmatrix} \quad \xrightarrow{W_3, b_3} \quad \begin{bmatrix} y_1 \\ y_2 \end{bmatrix} \end{aligned}

For our network, we can calculate these terms explicitly:

What to Calculate

Letโ€™s define exactly what we need to calculate for our network to evaluate the gradients for backpropagation.

For weights in layer l:

โˆ‚Jโˆ‚W[l]=โˆ‚Jโˆ‚a[l]โ‹…โˆ‚a[l]โˆ‚z[l]โ‹…โˆ‚z[l]โˆ‚W[l] \frac{\partial J}{\partial \mathbf{W}^{[l]}} = \frac{\partial J}{\partial \mathbf{a}^{[l]}} \cdot \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} \cdot \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{W}^{[l]}}

For biases in layer l:

โˆ‚Jโˆ‚b[l]=โˆ‚Jโˆ‚a[l]โ‹…โˆ‚a[l]โˆ‚z[l]โ‹…โˆ‚z[l]โˆ‚b[l] \frac{\partial J}{\partial \mathbf{b}^{[l]}} = \frac{\partial J}{\partial \mathbf{a}^{[l]}} \cdot \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} \cdot \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{b}^{[l]}}
  1. For the activation function derivative โˆ‚a[l]โˆ‚z[l]\frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}}:

    • For ReLU: โˆ‚ReLU(z)โˆ‚z={1if z>00if zโ‰ค0\frac{\partial \text{ReLU}(z)}{\partial z} = \begin{cases} 1 & \text{if } z > 0 \\ 0 & \text{if } z \leq 0 \end{cases}
    • For step function: โˆ‚step(z)โˆ‚z=0\frac{\partial \text{step}(z)}{\partial z} = 0 (except at z=0z=0 where itโ€™s undefinedโ€”uh oh, weโ€™ll come back to this soon)
  2. For the pre-activation derivatives:

    • โˆ‚z[l]โˆ‚W[l]=a[lโˆ’1]\frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{W}^{[l]}} = \mathbf{a}^{[l-1]} (or x\mathbf{x} for l=1l=1)
    • โˆ‚z[l]โˆ‚b[l]=1\frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{b}^{[l]}} = 1
  3. For the loss derivative โˆ‚Jโˆ‚a[l]\frac{\partial J}{\partial \mathbf{a}^{[l]}}:

    • For output layer: โˆ‚Jโˆ‚a[L]=ฮด[l]=a[L]โˆ’y\frac{\partial J}{\partial \mathbf{a}^{[L]}} = \delta^{[l]} = \mathbf{a}^{[L]} - \mathbf{y} (for mean squared error)
    • For hidden layers: Computed through backpropagation as โˆ‚Jโˆ‚a[L]=ฮด[l]=(W[l+1])Tฮด[l+1]\frac{\partial J}{\partial \mathbf{a}^{[L]}} = \delta^{[l]} = (\mathbf{W}^{[l+1]})^T\delta^{[l+1]}

We perform our calculation of the gradients by working backwards:

Layer 3 (Output Layer, 2 nodes)

Output Layer - Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Output layer of our example neural network architecture.
โˆ‚Jโˆ‚a[3]=a[3]โˆ’y \frac{\partial J}{\partial \mathbf{a}^{[3]}} = \mathbf{a}^{[3]} - \mathbf{y}

This is the error at the output layer, calculated as the difference between our predictions a[3]\mathbf{a}^{[3]} and the true labels y\mathbf{y}.

For example, if we predicted [0.8, 0.3] and the true label was [1, 0], then we have:

โˆ‚Jโˆ‚a[3]=[0.8โˆ’10.3โˆ’0]=[โˆ’0.20.3] \frac{\partial J}{\partial \mathbf{a}^{[3]}} = \begin{bmatrix} 0.8-1 \\ 0.3-0 \end{bmatrix} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix}

Pugging this in to the derivative of the cost function with respect to the weights, we now have:

โˆ‚Jโˆ‚W[3]=[โˆ’0.20.3]โ‹…โˆ‚a[3]โˆ‚z[3]โ‹…โˆ‚z[3]โˆ‚W[3] \frac{\partial J}{\partial \mathbf{W}^{[3]}} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} \cdot \frac{\partial \mathbf{a}^{[3]}}{\partial \mathbf{z}^{[3]}} \cdot \frac{\partial \mathbf{z}^{[3]}}{\partial \mathbf{W}^{[3]}}

And we can also plug in โˆ‚z[3]/โˆ‚W[3]=a[2]\partial \mathbf{z}^{[3]} / \partial \mathbf{W}^{[3]} = \mathbf{a}^{[2]}:

โˆ‚Jโˆ‚W[3]=[โˆ’0.20.3]โ‹…โˆ‚a[3]โˆ‚z[3]โ‹…[a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]] \frac{\partial J}{\partial \mathbf{W}^{[3]}} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} \cdot \frac{\partial \mathbf{a}^{[3]}}{\partial \mathbf{z}^{[3]}} \cdot \begin{bmatrix} a_1^{[2]} & a_2^{[2]} & a_3^{[2]} & a_4^{[2]} & a_5^{[2]} & a_6^{[2]} & a_7^{[2]} \end{bmatrix}

Where a[2]\mathbf{a}^{[2]} is a 7-dimensional vector representing the activations for the second layer.

Now we just need the derivative of the activation โˆ‚a[3]/โˆ‚z[3]\partial \mathbf{a}^{[3]} / \partial \mathbf{z}^{[3]}.

Hereโ€™s where s*** hits the fan a little bit for our example:

  • We are using the step function for our final activation
  • But the step function is not differentiable
  • As noted earlier, itโ€™s value is either 0 or undefined

This is one reason why the step function is not actually used in neural networks. A good alternative would be the sigmoid function, but we arenโ€™t talking about that until the next lesson.

So, for now, weโ€™re just going to set โˆ‚a[3]/โˆ‚z[3]=[11]\partial \mathbf{a}^{[3]} / \partial \mathbf{z}^{[3]} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}.

Using this derivative assumption (which is technically incorrect, but useful for the purposes of demonstration) we get:

โˆ‚Jโˆ‚W[3]=[โˆ’0.20.3]โ‹…[11]โ‹…[a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]] \frac{\partial J}{\partial \mathbf{W}^{[3]}} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} a_1^{[2]} & a_2^{[2]} & a_3^{[2]} & a_4^{[2]} & a_5^{[2]} & a_6^{[2]} & a_7^{[2]} \end{bmatrix}

This matrix multiplication works out as:

โˆ‚Jโˆ‚W[3]=[โˆ’0.20.3]โ‹…[a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]]=[โˆ’0.2(a1[2]+a2[2]+a3[2]+a4[2]+a5[2]+a6[2]+a7[2])0.3(a1[2]+a2[2]+a3[2]+a4[2]+a5[2]+a6[2]+a7[2])] \begin{aligned} \frac{\partial J}{\partial \mathbf{W}^{[3]}} &= \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} \cdot \begin{bmatrix} a_1^{[2]} & a_2^{[2]} & a_3^{[2]} & a_4^{[2]} & a_5^{[2]} & a_6^{[2]} & a_7^{[2]} \\ a_1^{[2]} & a_2^{[2]} & a_3^{[2]} & a_4^{[2]} & a_5^{[2]} & a_6^{[2]} & a_7^{[2]} \end{bmatrix} \\ &= \begin{bmatrix} -0.2(a_1^{[2]} + a_2^{[2]} + a_3^{[2]} + a_4^{[2]} + a_5^{[2]} + a_6^{[2]} + a_7^{[2]}) \\ 0.3(a_1^{[2]} + a_2^{[2]} + a_3^{[2]} + a_4^{[2]} + a_5^{[2]} + a_6^{[2]} + a_7^{[2]}) \end{bmatrix} \end{aligned}

This 2-dimensional vector represents the rate of change of the cost function, with respect to the two weights in the 3rd layer of the neural network.

We can also work out the derivative of the loss with respect to the biases:

โˆ‚Jโˆ‚b[3]=[โˆ’0.20.3]โ‹…โˆ‚z[3]โˆ‚b[3]โ‹…โˆ‚a[3]โˆ‚z[3] \frac{\partial J}{\partial \mathbf{b}^{[3]}} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} \cdot \frac{\partial \mathbf{z}^{[3]}}{\partial \mathbf{b}^{[3]}} \cdot \frac{\partial \mathbf{a}^{[3]}}{\partial \mathbf{z}^{[3]}}

Plugging in โˆ‚z[l]/โˆ‚b[l]=1\partial \mathbf{z}^{[l]} / \partial \mathbf{b}^{[l]} = 1 and the (technically incorrect) assertion that โˆ‚a[3]/โˆ‚z[3]=1\partial \mathbf{a}^{[3]} / \partial \mathbf{z}^{[3]} = 1, we simply get:

โˆ‚Jโˆ‚b[3]=[โˆ’0.20.3]โ‹…[11]โ‹…[11]=[โˆ’0.2โ‹…(1+1)0.3โ‹…(1+1)]=[โˆ’0.40.6] \frac{\partial J}{\partial \mathbf{b}^{[3]}} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} -0.2 \cdot (1 + 1) \\ 0.3 \cdot (1 + 1) \end{bmatrix} = \begin{bmatrix} -0.4 \\ 0.6 \end{bmatrix}

This is the rate of change of the cost function with respect to the biases in the 3rd layer.

Together with the weights gradient โˆ‚J/โˆ‚b[3]\partial J / \partial \mathbf{b}^{[3]} that we calculated above, these vectors tell us how to adjust the weights and biases in to reduce the prediction error!

In particular, we can perform an update using gradient descent:

For some learning rate ฮฑ\alpha, we update:

W[3]โ†W[3]โˆ’ฮฑโ‹…[โˆ’0.2(a1[2]+a2[2]+a3[2]+a4[2]+a5[2]+a6[2]+a7[2])0.3(a1[2]+a2[2]+a3[2]+a4[2]+a5[2]+a6[2]+a7[2])] \mathbf{W}^{[3]} \leftarrow \mathbf{W}^{[3]} - \alpha \cdot \begin{bmatrix} -0.2(a_1^{[2]} + a_2^{[2]} + a_3^{[2]} + a_4^{[2]} + a_5^{[2]} + a_6^{[2]} + a_7^{[2]}) \\ 0.3(a_1^{[2]} + a_2^{[2]} + a_3^{[2]} + a_4^{[2]} + a_5^{[2]} + a_6^{[2]} + a_7^{[2]}) \end{bmatrix}
b[3]โ†b[3]โˆ’ฮฑโ‹…[โˆ’0.40.6] \mathbf{b}^{[3]} \leftarrow \mathbf{b}^{[3]} - \alpha \cdot \begin{bmatrix} -0.4 \\ 0.6 \end{bmatrix}

For example, if ฮฑ=0.01\alpha = 0.01, then our update for the biases looks like this:

b[3]โ†b[3]โˆ’[โˆ’0.0040.006] \mathbf{b}^{[3]} \leftarrow \mathbf{b}^{[3]} - \begin{bmatrix} -0.004 \\ 0.006 \end{bmatrix}

As we discussed in an earlier lesson on differentiation and gradient descent, these updates will slightly adjust the weights and biases to reduce the prediction error.

We can continue to layer 2:

Layer 2 (Hidden layer, 7 nodes)

Hidden Layer 2 - Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Hidden layer 2 of our example neural network architecture.

Letโ€™s work through this with actual numbers, so we can see exactly whatโ€™s happening.

The layer-3 weights are a 2x7 matrix because we have 2 outputs and 7 inputs. Letโ€™s use:

W[3]=[0.10.2โˆ’0.10.30.20.1โˆ’0.20.3โˆ’0.20.10.1โˆ’0.10.20.3]\mathbf{W}^{[3]} = \begin{bmatrix} 0.1 & 0.2 & -0.1 & 0.3 & 0.2 & 0.1 & -0.2 \\ 0.3 & -0.2 & 0.1 & 0.1 & -0.1 & 0.2 & 0.3 \end{bmatrix}

Weโ€™ll need the layer-1 activations. Since we have 4 nodes in the first layer, weโ€™ll can use:

a[1]=[0.50.30.80.1]\mathbf{a}^{[1]} = \begin{bmatrix} 0.5 & 0.3 & 0.8 & 0.1 \end{bmatrix}

And weโ€™ll also need the values from layer 2 (the current layer) before they are passed through the activation function. Since there are 7 nodes in this layer, we can use:

z[2]=[0.6โˆ’0.20.40.8โˆ’0.30.50.7]\mathbf{z}^{[2]} = \begin{bmatrix} 0.6 \\ -0.2 \\ 0.4 \\ 0.8 \\ -0.3 \\ 0.5 \\ 0.7 \end{bmatrix}

With those numbers fixed, letโ€™s calculate the gradient for the weight updates in the 2nd layer:

โˆ‚Jโˆ‚W[2]=โˆ‚Jโˆ‚a[2]โ‹…โˆ‚a[2]โˆ‚z[2]โ‹…โˆ‚z[2]โˆ‚W[2] \frac{\partial J}{\partial \mathbf{W}^{[2]}} = \frac{\partial J}{\partial \mathbf{a}^{[2]}} \cdot \frac{\partial \mathbf{a}^{[2]}}{\partial \mathbf{z}^{[2]}} \cdot \frac{\partial \mathbf{z}^{[2]}}{\partial \mathbf{W}^{[2]}}

First, weโ€™ll compute the error term using backpropagation:

โˆ‚Jโˆ‚a[2]=(W[3])Tฮด[3] \frac{\partial J}{\partial \mathbf{a}^{[2]}} = (\mathbf{W}^{[3]})^T\delta^{[3]}

Where ฮด[3]=[โˆ’0.20.3]\delta^{[3]} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix}

Plugging in our values:

โˆ‚Jโˆ‚a[2]=[0.10.30.2โˆ’0.2โˆ’0.10.10.30.10.2โˆ’0.10.10.2โˆ’0.20.3][โˆ’0.20.3] \frac{\partial J}{\partial \mathbf{a}^{[2]}} = \begin{bmatrix} 0.1 & 0.3 \\ 0.2 & -0.2 \\ -0.1 & 0.1 \\ 0.3 & 0.1 \\ 0.2 & -0.1 \\ 0.1 & 0.2 \\ -0.2 & 0.3 \end{bmatrix} \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix}

Computing this matrix multiplication:

โˆ‚Jโˆ‚a[2]=[(0.1ร—โˆ’0.2)+(0.3ร—0.3)(0.2ร—โˆ’0.2)+(โˆ’0.2ร—0.3)(โˆ’0.1ร—โˆ’0.2)+(0.1ร—0.3)(0.3ร—โˆ’0.2)+(0.1ร—0.3)(0.2ร—โˆ’0.2)+(โˆ’0.1ร—0.3)(0.1ร—โˆ’0.2)+(0.2ร—0.3)(โˆ’0.2ร—โˆ’0.2)+(0.3ร—0.3)]=[0.07โˆ’0.10.05โˆ’0.03โˆ’0.070.040.13] \frac{\partial J}{\partial \mathbf{a}^{[2]}} = \begin{bmatrix} (0.1 \times -0.2) + (0.3 \times 0.3) \\ (0.2 \times -0.2) + (-0.2 \times 0.3) \\ (-0.1 \times -0.2) + (0.1 \times 0.3) \\ (0.3 \times -0.2) + (0.1 \times 0.3) \\ (0.2 \times -0.2) + (-0.1 \times 0.3) \\ (0.1 \times -0.2) + (0.2 \times 0.3) \\ (-0.2 \times -0.2) + (0.3 \times 0.3) \end{bmatrix} = \begin{bmatrix} 0.07 \\ -0.1 \\ 0.05 \\ -0.03 \\ -0.07 \\ 0.04 \\ 0.13 \end{bmatrix}

Next, we need the ReLU derivative for z[2]\mathbf{z}^{[2]}.

Since this derivative is 0 for negative values and 1 otherwise, we get:

โˆ‚ReLUโˆ‚z[2]=[1011011]\frac{\partial \text{ReLU}}{\partial \mathbf{z}^{[2]}} = \begin{bmatrix} 1 \\ 0 \\ 1 \\ 1 \\ 0 \\ 1 \\ 1 \end{bmatrix}

Plugging everything into the gradient, we get:

โˆ‚Jโˆ‚W[2]=[0.07โˆ’0.10.05โˆ’0.03โˆ’0.070.040.13]โ‹…[1011011]โ‹…[0.50.30.80.1] \frac{\partial J}{\partial \mathbf{W}^{[2]}} = \begin{bmatrix} 0.07 \\ -0.1 \\ 0.05 \\ -0.03 \\ -0.07 \\ 0.04 \\ 0.13 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 0 \\ 1 \\ 1 \\ 0 \\ 1 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 0.5 & 0.3 & 0.8 & 0.1 \end{bmatrix}

Evaluating this matrix multiplication from left-to-right:

โˆ‚Jโˆ‚W[2]=[0.07โˆ’0.10.05โˆ’0.03โˆ’0.070.040.13]โ‹…[0.50.30.80.100000.50.30.80.10.50.30.80.100000.50.30.80.10.50.30.80.1]=[0.0350.0210.0560.00700000.0250.0150.0400.005โˆ’0.015โˆ’0.009โˆ’0.024โˆ’0.00300000.0200.0120.0320.0040.0650.0390.1040.013] \begin{aligned} \frac{\partial J}{\partial \mathbf{W}^{[2]}} &= \begin{bmatrix} 0.07 \\ -0.1 \\ 0.05 \\ -0.03 \\ -0.07 \\ 0.04 \\ 0.13 \end{bmatrix} \cdot \begin{bmatrix} 0.5 & 0.3 & 0.8 & 0.1 \\ 0 & 0 & 0 & 0 \\ 0.5 & 0.3 & 0.8 & 0.1 \\ 0.5 & 0.3 & 0.8 & 0.1 \\ 0 & 0 & 0 & 0 \\ 0.5 & 0.3 & 0.8 & 0.1 \\ 0.5 & 0.3 & 0.8 & 0.1 \end{bmatrix} \\ &= \begin{bmatrix} 0.035 & 0.021 & 0.056 & 0.007 \\ 0 & 0 & 0 & 0 \\ 0.025 & 0.015 & 0.040 & 0.005 \\ -0.015 & -0.009 & -0.024 & -0.003 \\ 0 & 0 & 0 & 0 \\ 0.020 & 0.012 & 0.032 & 0.004 \\ 0.065 & 0.039 & 0.104 & 0.013 \end{bmatrix} \end{aligned}

For the biases, we plug in and evaluate as follows:

โˆ‚Jโˆ‚b[2]=[0.07โˆ’0.10.05โˆ’0.03โˆ’0.070.040.13]โ‹…[1011011]โ‹…[1111]=[0.07โˆ’0.10.05โˆ’0.03โˆ’0.070.040.13]โ‹…[1111000011111111000011111111]=[0.0700.05โˆ’0.0300.040.13] \begin{aligned} \frac{\partial J}{\partial \mathbf{b}^{[2]}} &= \begin{bmatrix} 0.07 \\ -0.1 \\ 0.05 \\ -0.03 \\ -0.07 \\ 0.04 \\ 0.13 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 0 \\ 1 \\ 1 \\ 0 \\ 1 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} \\ &= \begin{bmatrix} 0.07 \\ -0.1 \\ 0.05 \\ -0.03 \\ -0.07 \\ 0.04 \\ 0.13 \end{bmatrix} \cdot \begin{bmatrix} 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix} \\ &= \begin{bmatrix} 0.07 \\ 0 \\ 0.05 \\ -0.03 \\ 0 \\ 0.04 \\ 0.13 \end{bmatrix} \end{aligned}

With learning rate ฮฑ=0.01\alpha = 0.01, our updates would be:

W[2]โ†W[2]โˆ’0.01ร—[0.0350.0210.0560.00700000.0250.0150.0400.005โˆ’0.015โˆ’0.009โˆ’0.024โˆ’0.00300000.0200.0120.0320.0040.0650.0390.1040.013] \mathbf{W}^{[2]} \leftarrow \mathbf{W}^{[2]} - 0.01 \times \begin{bmatrix} 0.035 & 0.021 & 0.056 & 0.007 \\ 0 & 0 & 0 & 0 \\ 0.025 & 0.015 & 0.040 & 0.005 \\ -0.015 & -0.009 & -0.024 & -0.003 \\ 0 & 0 & 0 & 0 \\ 0.020 & 0.012 & 0.032 & 0.004 \\ 0.065 & 0.039 & 0.104 & 0.013 \end{bmatrix}
b[2]โ†b[2]โˆ’0.01ร—[0.0700.05โˆ’0.0300.040.13] \mathbf{b}^{[2]} \leftarrow \mathbf{b}^{[2]} - 0.01 \times \begin{bmatrix} 0.07 \\ 0 \\ 0.05 \\ -0.03 \\ 0 \\ 0.04 \\ 0.13 \end{bmatrix}

So for example, w32[2]w_{32}^{[2]} would be updated as:
w32[2]โ†w32[2]โˆ’0.01ร—0.015w_{32}^{[2]} \leftarrow w_{32}^{[2]} - 0.01 \times 0.015

And b4[2]b_4^{[2]} would be updated as:
b4[2]โ†b4[2]โˆ’0.01ร—โˆ’0.03b_4^{[2]} \leftarrow b_4^{[2]} - 0.01 \times -0.03

And we can continue to layer 1!

Or ratherโ€ฆ we couldโ€ฆ But I think weโ€™ve all seen enough for one day.

Conclusion

Alright, Iโ€™ll admit it: this section got way out of hand. And whose idea was it to use a 7-node layer in the example anyway?

Oh right, that was me.

Well, Iโ€™m sad to tell you, this lesson was the pinnacle of math in this course.

Ohโ€ฆ waitโ€ฆ whatโ€™s that? We have CNNs, RNNs, and Transformers coming up soon? Well then, I may have spoken too soon.

Key Takeaways

๐Ÿ”„ Backpropagation propagates errors backward through a neural network to compute gradients for weight updates.

๐Ÿ”— The chain rule is essential for calculating how weight changes affect the final cost.

๐Ÿ“Š For each layer, we compute gradients using partial derivatives of:

  • Error terms from later layers
  • Activation function derivatives
  • Layer activations from forward pass

๐Ÿ“‰ Gradient descent updates weights and biases:

  • W=Wโˆ’ฮฑโ‹…โˆ‚Jโˆ‚W\mathbf{W} = \mathbf{W} - \alpha \cdot \frac{\partial J}{\partial \mathbf{W}}
  • b=bโˆ’ฮฑโ‹…โˆ‚Jโˆ‚b\mathbf{b} = \mathbf{b} - \alpha \cdot \frac{\partial J}{\partial \mathbf{b}}

โšก Choice of activation function is criticalโ€”non-differentiable functions like step functions cause issues.

๐Ÿง  Though complex, backpropagation provides a systematic approach to training neural networks.

Next: Activation

Up next, weโ€™ve got a quick lesson on activations.

Comments (0)

No comments yet.