Skip to content

Instantly share code, notes, and snippets.

@Pythonista7
Last active October 13, 2021 13:18
Show Gist options
  • Select an option

  • Save Pythonista7/3de4d98977409deaa158bd49d7b07caa to your computer and use it in GitHub Desktop.

Select an option

Save Pythonista7/3de4d98977409deaa158bd49d7b07caa to your computer and use it in GitHub Desktop.
x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as g:
  g.watch(x)
  y = x * x
  z = y * y
dz_dx = g.gradient(z, x)  # (4*x^3 at x = 3)
print(dz_dx)
# tf.Tensor(108.0, shape=(), dtype=float32)
dy_dx = g.gradient(y, x)
print(dy_dx)
# tf.Tensor(6.0, shape=(), dtype=float32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment