tensorflow
tf.GradientTape
tf.gradients
TensorFlow 2.0
machine learning

Is tf.GradientTape in TF 2.0 equivalent to tf.gradients?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

tf.GradientTape and tf.gradients serve the same broad purpose, computing derivatives, but they are not a drop-in conceptual equivalent. tf.gradients belongs to the older TensorFlow 1.x graph-building style and returns symbolic gradients inside a static graph. tf.GradientTape is the TensorFlow 2.x eager-style automatic differentiation API that records operations dynamically and computes gradients from the recorded tape.

What tf.gradients Did

In TensorFlow 1.x, you typically built a graph first and executed it later in a session. tf.gradients created gradient tensors symbolically.

python
1import tensorflow.compat.v1 as tf
2
3tf.disable_eager_execution()
4
5x = tf.Variable(3.0)
6y = x * x + 2.0 * x
7grad = tf.gradients(y, x)[0]
8
9with tf.Session() as sess:
10    sess.run(tf.global_variables_initializer())
11    print(sess.run(grad))

The key point is that grad is not the numeric gradient value yet. It is a graph node representing how to compute that gradient later.

What tf.GradientTape Does

In TensorFlow 2.x, eager execution is the default. GradientTape records operations as they happen and then computes gradients from that recorded execution.

python
1import tensorflow as tf
2
3x = tf.Variable(3.0)
4
5with tf.GradientTape() as tape:
6    y = x * x + 2.0 * x
7
8grad = tape.gradient(y, x)
9print(grad.numpy())

This feels much closer to ordinary Python execution. You compute values directly, then ask for the gradient of one value with respect to another.

Similar Goal, Different Programming Model

This is the real answer to the equivalence question:

  • both compute gradients
  • they are not the same API model
  • they encourage different coding styles

tf.gradients fits static graph construction. GradientTape fits eager execution and dynamic program structure.

So if someone asks whether GradientTape is "equivalent," the precise answer is: it is the modern replacement for most TF 1.x gradient use cases, but not a literal one-for-one API translation.

Important Behavioral Differences

1. Execution Style

tf.gradients works on symbolic tensors in a graph.

GradientTape works on actual operations executed under the tape context.

2. Watching Values

With GradientTape, trainable variables are watched automatically, but plain tensors are not unless you call tape.watch.

python
1import tensorflow as tf
2
3x = tf.constant(3.0)
4
5with tf.GradientTape() as tape:
6    tape.watch(x)
7    y = x * x
8
9print(tape.gradient(y, x).numpy())

That explicit watch step is part of the TF 2.x model.

3. Persistent Tapes

A tape is normally consumed after one gradient call. If you need multiple gradients from the same recorded execution, make it persistent.

python
1import tensorflow as tf
2
3x = tf.Variable(2.0)
4
5with tf.GradientTape(persistent=True) as tape:
6    y = x * x
7    z = x * x * x
8
9print(tape.gradient(y, x).numpy())
10print(tape.gradient(z, x).numpy())

That is a different usage pattern from building many symbolic gradient tensors up front.

Where tf.function Fits In

GradientTape is eager-friendly, but it also works inside tf.function. That means you can still get optimized graph execution while writing in the TF 2.x style.

So the real migration path from tf.gradients is often:

  • move to eager-style model code
  • use GradientTape for autodiff
  • wrap performance-critical functions with tf.function when needed

That is not the same mental model as TF 1.x sessions and graph construction, even if the end goal is still gradient computation.

Common Pitfalls

The biggest mistake is assuming GradientTape will automatically watch every tensor, including constants. It does not.

Another mistake is treating GradientTape as if it returned reusable symbolic graph nodes like tf.gradients did.

A third issue is forgetting that a non-persistent tape can be used only once.

Summary

  • 'tf.GradientTape and tf.gradients both compute derivatives, but they are not the same programming model'
  • 'tf.gradients is a graph-building TF 1.x API, while GradientTape is the tape-based TF 2.x autodiff API'
  • 'GradientTape works naturally with eager execution and can also be used inside tf.function'
  • Plain tensors must be watched explicitly with tape.watch if you need gradients with respect to them
  • For most TF 2.x code, GradientTape is the right replacement for old tf.gradients workflows

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.