# Automatic differentiation and gradient tape

In the previous tutorial we introduced Tensors and operations on them. In this tutorial we will cover automatic differentiation, a key technique for optimizing machine learning models.

## Setup

import tensorflow as tf

tf.enable_eager_execution()

tfe = tf.contrib.eager # Shorthand for some symbols


## Derivatives of a function

TensorFlow provides APIs for automatic differentiation - computing the derivative of a function. The way that more closely mimics the math is to encapsulate the computation in a Python function, say f, and use tfe.gradients_function to create a function that computes the derivatives of f with respect to its arguments. If you're familiar with autograd for differentiating numpy functions, this will be familiar. For example:

from math import pi

def f(x):
return tf.square(tf.sin(x))

assert f(pi/2).numpy() == 1.0

# grad_f will return a list of derivatives of f
# with respect to its arguments. Since f() has a single argument,
# grad_f will return a list with a single element.


The same API can be used to differentiate as many times as you like:

def f(x):
return tf.square(tf.sin(x))

x = tf.lin_space(-2*pi, 2*pi, 100)  # 100 points between -2π and +2π

import matplotlib.pyplot as plt

plt.plot(x, f(x), label="f")
plt.legend()
plt.show()


Every differentiable TensorFlow operation has an associated gradient function. For example, the gradient function of tf.square(x) would be a function that returns 2.0 * x. To compute the gradient of a user-defined function (like f(x) in the example above), TensorFlow first "records" all the operations applied to compute the output of the function. We call this record a "tape". It then uses that tape and the gradients functions associated with each primitive operation to compute the gradients of the user-defined function using reverse mode differentiation.

Since operations are recorded as they are executed, Python control flow (using ifs and whiles for example) is naturally handled:

def f(x, y):
output = 1
# Must use range(int(y)) instead of range(y) in Python 3 when
# using TensorFlow 1.10 and earlier. Can use range(y) in 1.11+
for i in range(int(y)):
output = tf.multiply(output, x)
return output

def g(x, y):
# Return the gradient of f with respect to it's first parameter

assert f(3.0, 2).numpy() == 9.0   # f(x, 2) is essentially x * x
assert g(3.0, 2).numpy() == 6.0   # And its gradient will be 2 * x
assert f(4.0, 3).numpy() == 64.0  # f(x, 3) is essentially x * x * x
assert g(4.0, 3).numpy() == 48.0  # And its gradient will be 3 * x * x


At times it may be inconvenient to encapsulate computation of interest into a function. For example, if you want the gradient of the output with respect to intermediate values computed in the function. In such cases, the slightly more verbose but explicit tf.GradientTape context is useful. All computation inside the context of a tf.GradientTape is "recorded".

For example:

x = tf.ones((2, 2))

# a single t.gradient() call when the bug is resolved.
t.watch(x)
y = tf.reduce_sum(x)
z = tf.multiply(y, y)

# Use the same tape to compute the derivative of z with respect to the
# intermediate value y.
assert dz_dy.numpy() == 8.0

# Derivative of z with respect to the original input tensor x
for i in [0, 1]:
for j in [0, 1]:
assert dz_dx[i][j].numpy() == 8.0


Operations inside of the GradientTape context manager are recorded for automatic differentiation. If gradients are computed in that context, then the gradient computation is recorded as well. As a result, the exact same API works for higher-order gradients as well. For example:

x = tf.Variable(1.0)  # Convert the Python 1.0 to a Tensor object