Path: blob/main/C2 - Advanced Learning Algorithms/week2/optional-labs/backprop/C2_W2_Derivatives.ipynb
3589 views
Optional Lab - Derivatives
This lab will give you a more intuitive understanding of derivatives. It will show you a simple way of calculating derivatives arithmetically. It will also introduce you to a handy Python library that allows you to calculate derivatives symbolically.
Informal definition of derivatives
The formal definition of derivatives can be a bit daunting with limits and values 'going to zero'. The idea is really much simpler.
The derivative of a function describes how the output of a function changes when there is a small change in an input variable.
Let's use the cost function as an example. The cost is the output and is the input variable. Let's give a 'small change' a name epsilon or . We use these Greek letters because it is traditional in mathematics to use epsilon() or delta () to represent a small value. You can think of it as representing 0.001 or some other small value.
This just says if you change the input to the function by a little bit and the output changes by times that little bit, then the derivative of is equal to .
Let's try this out. Let's look at the derivative of the function at the point and
We have increased the input value a little bit (0.001), causing the output to change from 9 to 9.006001, an increase of 6 times the input increase. Referencing (1) above, this says that , so . If you are familiar with calculus, you know, written symbolically, . With this is 6. Our calculation above is not exactly 6 because to be exactly correct would need to be infinitesimally small or really, really small. That is why we use the symbols or ~= rather than =. Let's see what happens if we make smaller.
The value gets close to exactly 6 as we reduce the size of . Feel free to try reducing the value further.
Finding symbolic derivatives
In backprop it is useful to know the derivative of simple functions at any input value. Put another way, we would like to know the 'symbolic' derivative rather than the 'arithmetic' derivative. An example of a symbolic derivative is, , the derivative of above. With the symbolic derivative you can find the value of the derivative at any input value .
If you have taken a calculus course, you are familiar with the many differentiation rules that mathematicians have developed to solve for a derivative given an expression. Well, it turns out this process has been automated with symbolic differentiation programs. An example of this in python is the SymPy library. Let's take a look at how to use this.
Define the python variables and their symbolic names.
Define and print the expression. Note SymPy produces a latex string which generates a nicely readable equation.
Use SymPy's diff
to differentiate the expression for with respect to . Note the result matches our earlier example.
Evaluate the derivative at a few points by 'substituting' numeric values for the symbolic values. In the first example, is replaced by .
Compare this with the arithmetic calculation
For the function , it is easy to see that any change in will result in 2 times that amount of change in the output , regardless of the starting value of . Our NumPy and arithmetic results confirm this.
Compare this with the arithmetic calculation
Compare this with the arithmetic calculation
If you have time, try to repeat the above steps on the function and evaluate at w=4
Compare this with the arithmetic calculation
Click for hints
Congratulations!
If you have run through the above examples, you understand a derivative describes the change in the output of a function that is a result of a small change in an input to that function. You also can use SymPy in python to find the symbolic derivative of functions.