Numerical Approximations of Gradients (C2W1L12)

Lecture explaining numerical approximation of derivatives using finite differences as a prelude to gradient checking for backpropagation. Uses F(theta)=theta^3 at theta=1 with epsilon=0.01 to show two-sided (central) difference ( [F(theta+e)-F(theta-e)]/(2e) ) yields an estimate 3.0001 (error 0.0001), much closer than the one-sided difference (3.0301, error 0.03). Discusses that central difference error is O(epsilon^2) versus O(epsilon) for one-sided difference.
Questions this video answers
F(theta) = theta^3
theta = 1
epsilon = 0.01
3.0001
3.0301
Chapters
When you implement back propagation, you find that there's a test called gradient checking that can really help you make sure that your implementation of backprop is correct. Because sometimes you write all these equations and you're just not 100% sure if you got all the details right in implementing backpropagation. So in order to build up to gradient checking, let's first talk about how to numerically approximate computations of gradients. And in the next video we'll talk about how you can implement grad gradient checking to make sure that your implementation of backprop is correct. So let's take the function F and replot it here. And remember, this is F of theta equals theta cubed. And let's again start off with some value of theta. Let's say theta equals one. Now instead of just nudging theta to the right to get theta plus epsilon, we're gonna nudge it to the right and nudge it to the left to get theta minus epsilon as well as theta plus epsilon. So this is 1, this is 1.01, this is 0.99. Where again epsilon is same as before is 0.01. It turns out that rather than taking this little triangle and computing the height over the width, you can get a much better estimate of gradient if you take this point F at theta minus epsilon and this point and you instead compute the height over width of this bigger triangle. So for technical reasons, which I won't go into, the height over width of this bigger green triangle gives you a much better approximation to the derivative at theta. And you want, you know, sort of, instead of taking just this little triangle on the upper right is as if you have two triangles, right, this one on the upper right and this one on the lower left. And you're kind of taking both of them into account by, by using this bigger green triangle. So rather than a one sided difference, you're taking a two sided difference. So let's work on the math. This point here is F of theta plus epsilon. This point here is F of theta minus epsilon. So the height of this big green triangle is F of theta plus epsilon minus F of theta minus epsilon. And at the width, you know, this is 1 epsilon, this is 2 epsilon. So the width of this green triangle is 2 epsilon. So THE HEIGHT of the width is going to be first the height. So that's F of theta plus epsilon minus F of theta minus epsilon divided by the width. So that was two epsilon, which we worked down here. Um, and this should hopefully be close to to G of Theta. So plug in the values. Remember, F of theta is theta cubed. So this is theta plus epsilon is 1.01. So take the cube of that minus now 0.99. Take the cube of that, divide it by 2 times 0.01. Feel free to pause the video and plug this in the calculator. You should get that. This is 3.0001. Whereas from the previous slide we saw that G of um, theta, this was three theta squared. So when theta equals one, this is. So these two values are actually very close to each other. The approximation error is um, now 0.0001. Whereas on the previous slide we're taking the one sided difference just theta and theta plus epsilon. We had gotten 3.0301. And so the approximation error was 0.03, right, rather than 0.0001. But so with this two sided difference way of approximating the derivative, you find that this is extremely close to 3. And so this gives you, you know, much greater confidence that G of theta is a probably a correct implementation of the derivative of F. When you use this method for gradient checking and back propagation, this turns out to run twice as slow as you were to use a one sided difference. It turns out that in practice I think it's worth it to use this other method because it's just much more accurate. Um, little bit of optional theory for those of you that are a little bit more familiar with calculus. It turns out that, and it's okay if you don't get what I'm about to say here. It turns out that the formal definition of the derivative is um, for very small values of epsilon is is f of theta plus epsilon minus f of theta minus epsilon over 2 epsilon. And the formal definition of derivative is in the limit of exactly that formula on the right as epsilon goes to zero. And the definition of a limit is something that you learn if you take a calculus class, but I won't go into that here. And it turns out that for a non zero value of epsilon, you could show that the error of this approximation is is on the order of epsilon squared. And remember, epsilon is a very small number. So if epsilon is, you know, 0.01, which it is here, then Epsilon squared is 0.0001. The big O notation means the error is actually some constant times this. But this is actually exactly our approximation error. So the big O constant happened to be one. Whereas in contrast, if we were to use this formula, the other one, then the error is on the order of epsilon and Again, when epsilon is a number less than 1, then epsilon is actually much bigger than epsilon squared. Which is why this formula here is actually a much less accurate approximation than this formula on the left. Which is why when doing gradient checking, we rather use this two sided difference when you compute F of theta plus epsilon minus F of theta minus epsilon and then divide by two epsilon rather than this one sided difference which is less accurate. If you didn't understand my last few comments, all of these things down here, don't worry about it. Uh, that's really more for those of you that are a bit more familiar with calculus and with numerical approximations. But the takeaway is that this two sided difference formula is much more accurate. And so that's what we're gonna use when we do gradient checking in the next video. So you see in how by taking a two sided difference, you can numerically verify whether or not a function G g of theta that someone else gives you is a correct implementation of the derivative of a function F. Let's now see how we can use this to verify whether or not your back propagation implementation is correct or if you know there might be a bug in there that you need to go in and tease out.










