Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom and Distributed Training with Tensorflow/Week 1 - Differentiation and Gradients/C2_W1_Lab_2_gradient-tape-basics.ipynb
Views: 13371
Gradient Tape Basics
In this ungraded lab, you'll get familiar with Tensorflow's built in API called Gradient Tape which helps in performing automatic differentiation.
Imports
Exercise on basics of Gradient Tape
Let's explore how you can use tf.GradientTape() to do automatic differentiation.
Gradient tape expires after one use, by default
If you want to compute multiple gradients, note that by default, GradientTape is not persistent (persistent=False
). This means that the GradientTape will expire after you use it to calculate a gradient.
To see this, set up gradient tape as usual and calculate a gradient, so that the gradient tape will be 'expired'.
Gradient tape has expired
See what happens if you try to calculate another gradient after you've already used gradient tape once.
Make the gradient tape persistent
To make sure that the gradient tape can be used multiple times, set persistent=True
Now that it's persistent, you can still reuse this tape!
Try calculating a second gradient on this persistent tape.
Great! It still works! Delete the tape variable t
once you no longer need it.
Nested Gradient tapes
Now let's try computing a higher order derivative by nesting the GradientTapes:
Acceptable indentation of the first gradient calculation
Keep in mind that you'll want to make sure that the first gradient calculation of dy_dx
should occur at least inside the outer with
block.
The first gradient calculation can also be inside the inner with block.
Where not to indent the first gradient calculation
If the first gradient calculation is OUTSIDE of the outer with
block, it won't persist for the second gradient calculation.
Notice how the d2y_dx2
calculation is now None
. The tape has expired. Also note that this still won't work even if you set persistent=True for both gradient tapes.
Proper indentation for the second gradient calculation
The second gradient calculation d2y_dx2
can be indented as much as the first calculation of dy_dx
but not more.
This is also acceptable
This is also acceptable