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 ) = โ 1 m โ l = 1 m [ 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]})] J ( W , b ) = โ m 1 โ โ l = 1 m โ [ y [ l ] log ( y ^ โ [ l ] ) + ( 1 โ y [ l ] ) log ( 1 โ y ^ โ [ l ] )] Where:
m m m is the number of training examples
y [ l ] y^{[l]} y [ l ] is the true label for example l l l
y ^ [ l ] \hat{y}^{[l]} y ^ โ [ l ] is our prediction (equal to a [ 3 ] \mathbf{a}^{[3]} a [ 3 ] in our example)
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:
โ โ x f ( g ( x ) ) = โ f โ g โ
โ g โ x
\frac{\partial}{\partial x}f(g(x)) = \frac{\partial f}{\partial g} \cdot \frac{\partial g}{\partial x} โ x โ โ f ( g ( x )) = โ g โ f โ โ
โ x โ g โ 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]}} โ W [ l ] โ J โ = โ a [ l ] โ J โ โ
โ z [ l ] โ a [ l ] โ โ
โ W [ l ] โ z [ l ] โ Where:
J J J is the cost/loss function
W [ l ] \mathbf{W}^{[l]} W [ l ] represents the weight matrix for layer l l l
a [ l ] \mathbf{a}^{[l]} a [ l ] is the activation output from layer l l l
z [ l ] \mathbf{z}^{[l]} z [ l ] is the weighted input to layer l l l before the activation function
โ โ x \frac{\partial}{\partial x} โ x โ โ denotes the partial derivative with respect to variable x x x
The Backpropagation Algorithm
A training step combines forward propagation, backpropagation and an optimizer update:
Forward Propagation
Compute activations for each layer moving forward
Store intermediate values for backpropagation
Compute Output Error
Calculate the difference between predicted and actual outputs
This gives us our initial error signal
Backward Propagation
Propagate error backward through the network
Compute the gradients of the loss with respect to weights and biases.
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]}} W [ l ] โ W [ l ] โ ฮฑ โ W [ l ] โ J โ b [ l ] โ b [ l ] โ ฮฑ โ J โ b [ l ]
\mathbf{b}^{[l]} \leftarrow \mathbf{b}^{[l]} - \alpha \frac{\partial J}{\partial \mathbf{b}^{[l]}} b [ l ] โ b [ l ] โ ฮฑ โ b [ l ] โ J โ 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 โ W [ l ] โ J โ and โ b [ l ] โ J โ 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]}} โ W [ l ] โ J โ = โ a [ l ] โ J โ โ
โ z [ l ] โ a [ l ] โ โ
โ W [ l ] โ z [ 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]}} โ b [ l ] โ J โ = โ a [ l ] โ J โ โ
โ z [ l ] โ a [ l ] โ โ
โ b [ l ] โ z [ l ] โ ere:
โ J โ a [ l ] \frac{\partial J}{\partial \mathbf{a}^{[l]}} โ a [ l ] โ J โ 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]}} โ z [ l ] โ a [ l ] โ is the derivative of the activation function
โ z [ l ] โ W [ l ] \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{W}^{[l]}} โ W [ l ] โ z [ l ] โ is how the pre-activation changes with respect to weights
โ z [ l ] โ b [ l ] \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{b}^{[l]}} โ b [ l ] โ z [ 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 architecture diagram for our binary classification example.
We can visualize our network as follows:
Input Layer (x) [ x 1 x 2 x 3 x 4 x 5 ] โ W 1 , b 1 , ReLU [ a 1 [ 1 ] a 2 [ 1 ] a 3 [ 1 ] a 4 [ 1 ] ] โ W 2 , b 2 , Step [ a 1 [ 2 ] a 2 [ 2 ] a 3 [ 2 ] a 4 [ 2 ] a 5 [ 2 ] a 6 [ 2 ] a 7 [ 2 ] ] โ W 3 , b 3 [ y 1 y 2 ]
\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} โ Input Layer (x) โ x 1 โ x 2 โ x 3 โ x 4 โ x 5 โ โ โ W 1 โ , b 1 โ , ReLU โ โ a 1 [ 1 ] โ a 2 [ 1 ] โ a 3 [ 1 ] โ a 4 [ 1 ] โ โ โ W 2 โ , b 2 โ , Step โ โ a 1 [ 2 ] โ a 2 [ 2 ] โ a 3 [ 2 ] โ a 4 [ 2 ] โ a 5 [ 2 ] โ a 6 [ 2 ] โ a 7 [ 2 ] โ โ โ W 3 โ , b 3 โ โ [ y 1 โ y 2 โ โ ] โ 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]}} โ W [ l ] โ J โ = โ a [ l ] โ J โ โ
โ z [ l ] โ a [ l ] โ โ
โ W [ l ] โ z [ 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]}} โ b [ l ] โ J โ = โ a [ l ] โ J โ โ
โ z [ l ] โ a [ l ] โ โ
โ b [ l ] โ z [ l ] โ
For the activation function derivative โ a [ l ] โ z [ l ] \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{z}^{[l]}} โ z [ l ] โ a [ l ] โ :
For ReLU: โ ReLU ( z ) โ z = { 1 if z > 0 0 if z โค 0 \frac{\partial \text{ReLU}(z)}{\partial z} = \begin{cases} 1 & \text{if } z > 0 \\ 0 & \text{if } z \leq 0 \end{cases} โ z โ ReLU ( z ) โ = { 1 0 โ if z > 0 if z โค 0 โ
For step function: โ step ( z ) โ z = 0 \frac{\partial \text{step}(z)}{\partial z} = 0 โ z โ step ( z ) โ = 0 (except at z = 0 z=0 z = 0 where itโs undefinedโuh oh, weโll come back to this soon)
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]} โ W [ l ] โ z [ l ] โ = a [ l โ 1 ] (or x \mathbf{x} x for l = 1 l=1 l = 1 )
โ z [ l ] โ b [ l ] = 1 \frac{\partial \mathbf{z}^{[l]}}{\partial \mathbf{b}^{[l]}} = 1 โ b [ l ] โ z [ l ] โ = 1
For the loss derivative โ J โ a [ l ] \frac{\partial J}{\partial \mathbf{a}^{[l]}} โ a [ l ] โ J โ :
For output layer: โ J โ a [ L ] = ฮด [ l ] = a [ L ] โ y \frac{\partial J}{\partial \mathbf{a}^{[L]}} = \delta^{[l]} = \mathbf{a}^{[L]} - \mathbf{y} โ a [ L ] โ J โ = ฮด [ l ] = a [ L ] โ 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]} โ a [ L ] โ J โ = ฮด [ l ] = ( W [ l + 1 ] ) T ฮด [ l + 1 ]
We perform our calculation of the gradients by working backwards:
Layer 3 (Output Layer, 2 nodes)
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} โ a [ 3 ] โ J โ = a [ 3 ] โ y This is the error at the output layer , calculated as the difference between our predictions a [ 3 ] \mathbf{a}^{[3]} a [ 3 ] and the true labels y \mathbf{y} 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 โ 1 0.3 โ 0 ] = [ โ 0.2 0.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} โ a [ 3 ] โ J โ = [ 0.8 โ 1 0.3 โ 0 โ ] = [ โ 0.2 0.3 โ ] Pugging this in to the derivative of the cost function with respect to the weights , we now have:
โ J โ W [ 3 ] = [ โ 0.2 0.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]}} โ W [ 3 ] โ J โ = [ โ 0.2 0.3 โ ] โ
โ z [ 3 ] โ a [ 3 ] โ โ
โ W [ 3 ] โ z [ 3 ] โ And we can also plug in โ z [ 3 ] / โ W [ 3 ] = a [ 2 ] \partial \mathbf{z}^{[3]} / \partial \mathbf{W}^{[3]} = \mathbf{a}^{[2]} โ z [ 3 ] / โ W [ 3 ] = a [ 2 ] :
โ J โ W [ 3 ] = [ โ 0.2 0.3 ] โ
โ a [ 3 ] โ z [ 3 ] โ
[ a 1 [ 2 ] a 2 [ 2 ] a 3 [ 2 ] a 4 [ 2 ] a 5 [ 2 ] a 6 [ 2 ] a 7 [ 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} โ W [ 3 ] โ J โ = [ โ 0.2 0.3 โ ] โ
โ z [ 3 ] โ a [ 3 ] โ โ
[ a 1 [ 2 ] โ โ a 2 [ 2 ] โ โ a 3 [ 2 ] โ โ a 4 [ 2 ] โ โ a 5 [ 2 ] โ โ a 6 [ 2 ] โ โ a 7 [ 2 ] โ โ ] Where a [ 2 ] \mathbf{a}^{[2]} 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]} โ a [ 3 ] / โ 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 ] = [ 1 1 ] \partial \mathbf{a}^{[3]} / \partial \mathbf{z}^{[3]} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} โ a [ 3 ] / โ z [ 3 ] = [ 1 1 โ ] .
Using this derivative assumption (which is technically incorrect, but useful for the purposes of demonstration) we get:
โ J โ W [ 3 ] = [ โ 0.2 0.3 ] โ
[ 1 1 ] โ
[ a 1 [ 2 ] a 2 [ 2 ] a 3 [ 2 ] a 4 [ 2 ] a 5 [ 2 ] a 6 [ 2 ] a 7 [ 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} โ W [ 3 ] โ J โ = [ โ 0.2 0.3 โ ] โ
[ 1 1 โ ] โ
[ a 1 [ 2 ] โ โ a 2 [ 2 ] โ โ a 3 [ 2 ] โ โ a 4 [ 2 ] โ โ a 5 [ 2 ] โ โ a 6 [ 2 ] โ โ a 7 [ 2 ] โ โ ] This matrix multiplication works out as:
โ J โ W [ 3 ] = [ โ 0.2 0.3 ] โ
[ 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 ] ] = [ โ 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 ] ) ]
\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} โ W [ 3 ] โ J โ โ = [ โ 0.2 0.3 โ ] โ
[ a 1 [ 2 ] โ a 1 [ 2 ] โ โ a 2 [ 2 ] โ a 2 [ 2 ] โ โ a 3 [ 2 ] โ a 3 [ 2 ] โ โ a 4 [ 2 ] โ a 4 [ 2 ] โ โ a 5 [ 2 ] โ a 5 [ 2 ] โ โ a 6 [ 2 ] โ a 6 [ 2 ] โ โ a 7 [ 2 ] โ a 7 [ 2 ] โ โ ] = [ โ 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 ] โ ) โ ] โ 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.2 0.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]}} โ b [ 3 ] โ J โ = [ โ 0.2 0.3 โ ] โ
โ b [ 3 ] โ z [ 3 ] โ โ
โ z [ 3 ] โ a [ 3 ] โ Plugging in โ z [ l ] / โ b [ l ] = 1 \partial \mathbf{z}^{[l]} / \partial \mathbf{b}^{[l]} = 1 โ z [ l ] / โ b [ l ] = 1 and the (technically incorrect) assertion that โ a [ 3 ] / โ z [ 3 ] = 1 \partial \mathbf{a}^{[3]} / \partial \mathbf{z}^{[3]} = 1 โ a [ 3 ] / โ z [ 3 ] = 1 , we simply get:
โ J โ b [ 3 ] = [ โ 0.2 0.3 ] โ
[ 1 1 ] โ
[ 1 1 ] = [ โ 0.2 โ
( 1 + 1 ) 0.3 โ
( 1 + 1 ) ] = [ โ 0.4 0.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} โ b [ 3 ] โ J โ = [ โ 0.2 0.3 โ ] โ
[ 1 โ 1 โ ] โ
[ 1 1 โ ] = [ โ 0.2 โ
( 1 + 1 ) 0.3 โ
( 1 + 1 ) โ ] = [ โ 0.4 0.6 โ ] 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]} โ J / โ 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 ( 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 ] ) ]
\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} W [ 3 ] โ W [ 3 ] โ ฮฑ โ
[ โ 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 ] โ ) โ ] b [ 3 ] โ b [ 3 ] โ ฮฑ โ
[ โ 0.4 0.6 ]
\mathbf{b}^{[3]} \leftarrow \mathbf{b}^{[3]} - \alpha \cdot \begin{bmatrix} -0.4 \\ 0.6 \end{bmatrix} b [ 3 ] โ b [ 3 ] โ ฮฑ โ
[ โ 0.4 0.6 โ ] For example, if ฮฑ = 0.01 \alpha = 0.01 ฮฑ = 0.01 , then our update for the biases looks like this:
b [ 3 ] โ b [ 3 ] โ [ โ 0.004 0.006 ]
\mathbf{b}^{[3]} \leftarrow \mathbf{b}^{[3]} - \begin{bmatrix} -0.004 \\ 0.006 \end{bmatrix} b [ 3 ] โ b [ 3 ] โ [ โ 0.004 0.006 โ ] 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 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.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 ] \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} W [ 3 ] = [ 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 โ ]
Weโll need the layer-1 activations . Since we have 4 nodes in the first layer, weโll can use:
a [ 1 ] = [ 0.5 0.3 0.8 0.1 ] \mathbf{a}^{[1]} = \begin{bmatrix} 0.5 & 0.3 & 0.8 & 0.1 \end{bmatrix} a [ 1 ] = [ 0.5 โ 0.3 โ 0.8 โ 0.1 โ ]
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.2 0.4 0.8 โ 0.3 0.5 0.7 ] \mathbf{z}^{[2]} = \begin{bmatrix} 0.6 \\ -0.2 \\ 0.4 \\ 0.8 \\ -0.3 \\ 0.5 \\ 0.7 \end{bmatrix} z [ 2 ] = โ 0.6 โ 0.2 0.4 0.8 โ 0.3 0.5 0.7 โ โ
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]}} โ W [ 2 ] โ J โ = โ a [ 2 ] โ J โ โ
โ z [ 2 ] โ a [ 2 ] โ โ
โ W [ 2 ] โ z [ 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]} โ a [ 2 ] โ J โ = ( W [ 3 ] ) T ฮด [ 3 ] Where ฮด [ 3 ] = [ โ 0.2 0.3 ] \delta^{[3]} = \begin{bmatrix} -0.2 \\ 0.3 \end{bmatrix} ฮด [ 3 ] = [ โ 0.2 0.3 โ ]
Plugging in our values:
โ J โ a [ 2 ] = [ 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 ] [ โ 0.2 0.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} โ a [ 2 ] โ J โ = โ 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 โ โ [ โ 0.2 0.3 โ ] 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.1 0.05 โ 0.03 โ 0.07 0.04 0.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} โ a [ 2 ] โ J โ = โ ( 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.1 0.05 โ 0.03 โ 0.07 0.04 0.13 โ โ Next, we need the ReLU derivative for z [ 2 ] \mathbf{z}^{[2]} z [ 2 ] .
Since this derivative is 0 for negative values and 1 otherwise, we get:
โ ReLU โ z [ 2 ] = [ 1 0 1 1 0 1 1 ] \frac{\partial \text{ReLU}}{\partial \mathbf{z}^{[2]}} = \begin{bmatrix} 1 \\ 0 \\ 1 \\ 1 \\ 0 \\ 1 \\ 1 \end{bmatrix} โ z [ 2 ] โ ReLU โ = โ 1 0 1 1 0 1 1 โ โ
Plugging everything into the gradient , we get:
โ J โ W [ 2 ] = [ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 ] โ
[ 1 0 1 1 0 1 1 ] โ
[ 0.5 0.3 0.8 0.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} โ W [ 2 ] โ J โ = โ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 โ โ โ
โ 1 0 1 1 0 1 1 โ โ โ
[ 0.5 โ 0.3 โ 0.8 โ 0.1 โ ] Evaluating this matrix multiplication from left-to-right:
โ J โ W [ 2 ] = [ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 ] โ
[ 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 ] = [ 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 ]
\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} โ W [ 2 ] โ J โ โ = โ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 โ โ โ
โ 0.5 0 0.5 0.5 0 0.5 0.5 โ 0.3 0 0.3 0.3 0 0.3 0.3 โ 0.8 0 0.8 0.8 0 0.8 0.8 โ 0.1 0 0.1 0.1 0 0.1 0.1 โ โ = โ 0.035 0 0.025 โ 0.015 0 0.020 0.065 โ 0.021 0 0.015 โ 0.009 0 0.012 0.039 โ 0.056 0 0.040 โ 0.024 0 0.032 0.104 โ 0.007 0 0.005 โ 0.003 0 0.004 0.013 โ โ โ For the biases, we plug in and evaluate as follows:
โ J โ b [ 2 ] = [ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 ] โ
[ 1 0 1 1 0 1 1 ] โ
[ 1 1 1 1 ] = [ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 ] โ
[ 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 ] = [ 0.07 0 0.05 โ 0.03 0 0.04 0.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} โ b [ 2 ] โ J โ โ = โ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 โ โ โ
โ 1 0 1 1 0 1 1 โ โ โ
[ 1 โ 1 โ 1 โ 1 โ ] = โ 0.07 โ 0.1 0.05 โ 0.03 โ 0.07 0.04 0.13 โ โ โ
โ 1 0 1 1 0 1 1 โ 1 0 1 1 0 1 1 โ 1 0 1 1 0 1 1 โ 1 0 1 1 0 1 1 โ โ = โ 0.07 0 0.05 โ 0.03 0 0.04 0.13 โ โ โ With learning rate ฮฑ = 0.01 \alpha = 0.01 ฮฑ = 0.01 , our updates would be:
W [ 2 ] โ W [ 2 ] โ 0.01 ร [ 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 ]
\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} W [ 2 ] โ W [ 2 ] โ 0.01 ร โ 0.035 0 0.025 โ 0.015 0 0.020 0.065 โ 0.021 0 0.015 โ 0.009 0 0.012 0.039 โ 0.056 0 0.040 โ 0.024 0 0.032 0.104 โ 0.007 0 0.005 โ 0.003 0 0.004 0.013 โ โ b [ 2 ] โ b [ 2 ] โ 0.01 ร [ 0.07 0 0.05 โ 0.03 0 0.04 0.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} b [ 2 ] โ b [ 2 ] โ 0.01 ร โ 0.07 0 0.05 โ 0.03 0 0.04 0.13 โ โ So for example, w 32 [ 2 ] w_{32}^{[2]} w 32 [ 2 ] โ would be updated as:
w 32 [ 2 ] โ w 32 [ 2 ] โ 0.01 ร 0.015 w_{32}^{[2]} \leftarrow w_{32}^{[2]} - 0.01 \times 0.015 w 32 [ 2 ] โ โ w 32 [ 2 ] โ โ 0.01 ร 0.015
And b 4 [ 2 ] b_4^{[2]} b 4 [ 2 ] โ would be updated as:
b 4 [ 2 ] โ b 4 [ 2 ] โ 0.01 ร โ 0.03 b_4^{[2]} \leftarrow b_4^{[2]} - 0.01 \times -0.03 b 4 [ 2 ] โ โ b 4 [ 2 ] โ โ 0.01 ร โ 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}} W = W โ ฮฑ โ
โ W โ J โ
b = b โ ฮฑ โ
โ J โ b \mathbf{b} = \mathbf{b} - \alpha \cdot \frac{\partial J}{\partial \mathbf{b}} b = b โ ฮฑ โ
โ b โ J โ
โก 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.