Deep Learning Fundamentals - Classic Edition

A newer version of this course is available! Check here for details!

Backpropagation explained | Part 4 - Calculating the gradient

video

expand_more chevron_left

text

expand_more chevron_left

Backpropagation explained | Part 4 - Calculating the gradient

Hey, what's going on everyone? In this episode, we're finally going to see how backpropagation calculates the gradient of the loss function with respect to the weights in a neural network.

So let's get to it.

Our Task

We're now on episode number four in our journey through understanding backpropagation. In the last episode, we focused on how we can mathematically express certain facts about the training process.

Now we're going to be using these expressions to help us differentiate the loss of the neural network with respect to the weights.

Recall from the episode that covered the intuition for backpropagation that for stochastic gradient descent to update the weights of the network, it first needs to calculate the gradient of the loss with respect to these weights.

Calculating this gradient is exactly what we'll be focusing on in this episode.

We're first going to start out by checking out the equation that backprop uses to differentiate the loss with respect to weights in the network.

Then, we'll see that this equation is made up of multiple terms. This will allow us to break down and focus on each of these terms individually.

Lastly, we'll take the results from each term and combine them to obtain the final result, which will be the gradient of the loss function.

Alright, let's begin.

Derivative of the Loss Function with Respect to the Weights

Let's look at a single weight that connects node 2 in layer L1 to node 1 in layer L.

This weight is denoted as

w12(L).

The derivative of the loss C0 with respect to this particular weight w12(L) is denoted as

C0w12(L).

Since C0 depends on a1(L), and a1(L) depends on z1(L), and z1(L) depends on w12(L), the chain rule tells us that to differentiate C0 with respect to w12(L), we take the product of the derivatives of the composed function.

This is expressed as

C0w12(L)=(C0a1(L))(a1(L)z1(L))(z1(L)w12(L)).

Let's break down each term from the expression on the right hand side of the above equation.

The first term: C0a1(L)

We know that

C0=j=0n1(aj(L)yj)2.

Therefore,

C0a1(L)=a1(L)(j=0n1(aj(L)yj)2).

Expanding the sum, we see

a1(L)(j=0n1(aj(L)yj)2)=a1(L)((a0(L)y0)2+(a1(L)y1)2+(a2(L)y2)2+(a3(L)y3)2)=a1(L)((a0(L)y0)2)+a1(L)((a1(L)y1)2)+a1(L)((a2(L)y2)2)+a1(L)((a3(L)y3)2)=2(a1(L)y1).

Observe that the loss from the network for a single input sample will respond to a small change in the activation output from node 1 in layer L by an amount equal to two times the difference of the activation output a1 for node 1 and the desired output y1 for node 1.

The second term: a1(L)z1(L)

We know that for each node j in the output layer L, we have

aj(L)=g(L)(zj(L)),

and since j=1, we have

a1(L)=g(L)(z1(L)).

Therefore,

a1(L)z1(L)=z1(L)(g(L)(z1(L)))=g(L)(z1(L)).

Therefore, this is just the direct derivative of a1(L) since a1(L) is a direct function of z1(L).

The third term: z1(L)w12(L)

We know that, for each node j in the output layer L, we have

zj(L)=k=0n1wjk(L)ak(L1).

Since j=1, we have

z1(L)=k=0n1w1k(L)ak(L1).

Therefore,

z1(L)w12(L)=w12(L)(k=0n1w1k(L)ak(L1)).

Expanding the sum, we see

w12(L)(k=0n1w1k(L)ak(L1))=w12(L)(w10(L)a0(L1)+w11(L)a1(L1)+w12(L)a2(L1)++w15(L)a5(L1))=w12(L)w10(L)a0(L1)+w12(L)w11(L)a1(L1)+w12(L)w12(L)a2(L1)++w12(L)w15(L)a5(L1)=a2(L1)

The input for node 1 in layer L will respond to a change in the weight w12(L) by an amount equal to the activation output for node 2 in the previous layer, L1.

Combining terms

Combining all terms, we have

C0w12(L)=(C0a1(L))(a1(L)z1(L))(z1(L)w12(L))=2(a1(L)y1)(g(L)(z1(L)))(a2(L1))

We Conclude

We've seen how to calculate the derivative of the loss with respect to one individual weight for one individual training sample.

To calculate the derivative of the loss with respect to this same particular weight, w12, for all n training samples, we calculate the average derivative of the loss function over all n training samples.

This can be expressed as

Cw12(L)=1ni=0n1Ciw12(L).

We would then do this same process for each weight in the network to calculate the derivative of C with respect to each weight.

Wrapping Up

At this point, we should now understand mathematically how backpropagation calculates the gradient of the loss with respect to the weights in the network.

We should also have a solid grip on all of the intermediate steps needed to do this calculation, and we should now be able to generalize the result we obtained for a single weight and a single sample to all the weights in the network for all training samples.

Now, we still haven't hit the point completely home by discussing the math that underlies the backwards movement of backpropagation that we discussed whenever we covered the intuition for backpropagation. Don't worry, we'll be doing that in the next episode. Thanks for reading. See you next time.

quiz

expand_more chevron_left
deeplizard logo DEEPLIZARD Message notifications

This lesson doesn't have any quiz questions yet!

deeplizard logo DEEPLIZARD Message notifications

Quiz Results

resources

expand_more chevron_left
We're now on number 4 in our journey through understanding backpropagation. In our last video, we focused on how we can mathematically express certain facts about the training process. Now we're going to be using these expressions to help us differentiate the loss of the neural network with respect to the weights. Recall from our video that covered the intuition for backpropagation, that, for stochastic gradient descent to update the weights of the network, it first needs to calculate the gradient of the loss with respect to these weights. And calculating this gradient, is exactly what we'll be focusing on in this video. We're first going to start out by checking out the equation that backprop uses to differentiate the loss with respect to weights in the network. We'll see that this equation is made up of multiple terms, so next we'll break down and focus on each of these terms individually. Lastly, we'll take the results from each term and combine them to obtain the final result, which will be the gradient of the loss function. 🕒🦎 VIDEO SECTIONS 🦎🕒 00:00 Welcome to DEEPLIZARD - Go to deeplizard.com for learning resources 00:58 Agenda 01:28 Derivative Calculations 05:45 Calculation Breakdown - First term 07:36 Calculation Breakdown - Second term 08:52 Calculation Breakdown - Third term 11:56 Summary 13:56 Collective Intelligence and the DEEPLIZARD HIVEMIND 💥🦎 DEEPLIZARD COMMUNITY RESOURCES 🦎💥 👋 Hey, we're Chris and Mandy, the creators of deeplizard! 👀 CHECK OUT OUR VLOG: 🔗 https://youtube.com/deeplizardvlog 💪 CHECK OUT OUR FITNESS CHANNEL: 🔗 https://www.youtube.com/channel/UCdCxHNCexDrAx78VfAuyKiA 🧠 Use code DEEPLIZARD at checkout to receive 15% off your first Neurohacker order: 🔗 https://neurohacker.com/shop?rfsn=6488344.d171c6 ❤️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Mano Prime 👀 Follow deeplizard: Our vlog: https://youtube.com/deeplizardvlog Fitness: https://www.youtube.com/channel/UCdCxHNCexDrAx78VfAuyKiA Facebook: https://facebook.com/deeplizard Instagram: https://instagram.com/deeplizard Twitter: https://twitter.com/deeplizard Patreon: https://patreon.com/deeplizard YouTube: https://youtube.com/deeplizard 🎓 Deep Learning with deeplizard: AI Art for Beginners - https://deeplizard.com/course/sdcpailzrd Deep Learning Dictionary - https://deeplizard.com/course/ddcpailzrd Deep Learning Fundamentals - https://deeplizard.com/course/dlcpailzrd Learn TensorFlow - https://deeplizard.com/course/tfcpailzrd Learn PyTorch - https://deeplizard.com/course/ptcpailzrd Natural Language Processing - https://deeplizard.com/course/txtcpailzrd Reinforcement Learning - https://deeplizard.com/course/rlcpailzrd Generative Adversarial Networks - https://deeplizard.com/course/gacpailzrd Stable Diffusion Masterclass - https://deeplizard.com/course/dicpailzrd 🎓 Other Courses: DL Fundamentals Classic - https://deeplizard.com/learn/video/gZmobeGL0Yg Deep Learning Deployment - https://deeplizard.com/learn/video/SI1hVGvbbZ4 Data Science - https://deeplizard.com/learn/video/d11chG7Z-xk Trading - https://deeplizard.com/learn/video/ZpfCK_uHL9Y 🛒 Check out products deeplizard recommends on Amazon: 🔗 https://amazon.com/shop/deeplizard 📕 Get a FREE 30-day Audible trial and 2 FREE audio books using deeplizard's link: 🔗 https://amzn.to/2yoqWRn 🎵 deeplizard uses music by Kevin MacLeod 🔗 https://youtube.com/channel/UCSZXFhRIx6b0dFX3xS8L1yQ ❤️ Please use the knowledge gained from deeplizard content for good, not evil.

updates

expand_more chevron_left
deeplizard logo DEEPLIZARD Message notifications

Update history for this page

Did you know you that deeplizard content is regularly updated and maintained?

  • Updated
  • Maintained

Spot something that needs to be updated? Don't hesitate to let us know. We'll fix it!


All relevant updates for the content on this page are listed below.

e32aca8
Page Created.

Committed by deeplizard on March 6, 2018