# Tf.gradients() vs tf.gradientTape.gradient() in graph mode

**URL:** <https://discuss.ai.google.dev/t/tf-gradients-vs-tf-gradienttape-gradient-in-graph-mode/32098>\
**Category:** General Discussion\
**Tags:** tfcore, education, help\_request\
**Created:** [August 22, 2021, 3:12am UTC](https://discuss.ai.google.dev/t/tf-gradients-vs-tf-gradienttape-gradient-in-graph-mode/32098 "2021-08-22T03:12:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mihir\_Khambete](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/mihir_khambete/32/8277_2.png) [@Mihir\_Khambete](https://discuss.ai.google.dev/u/Mihir_Khambete)\
**Post date:** [August 22, 2021, 3:12am UTC](https://discuss.ai.google.dev/t/tf-gradients-vs-tf-gradienttape-gradient-in-graph-mode/32098/1 "2021-08-22T03:12:23Z")

</div>

Hello,

I had a question regarding the behavior of tf.gradients() as opposed tf.gradientTape.gradient() in graph mode.

Given a differentiable function y = f(x), where x and y are each single tensorflow tensors, is there any difference between the behavior of tf.gradient(y, x) vs tape.gradient(y, x) where tape is an instance of tf.gradientTape (assuming the use of graph mode) ?

Not sure why tensorflow has two different gradient methods which can be used with graph mode - maybe there are some subtle differences in the implementations? I’ve looked at the documentation for gradientTape and tf.gradients but it’s not clear whether there is any difference between the behavior of these methods for a single (x, y) pair, or whether it’s just that tf.gradients() can be used in this case for a speedup when using graph mode.

Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![Renu\_Patel](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/renu_patel/32/8957_2.png) [@Renu\_Patel](https://discuss.ai.google.dev/u/Renu_Patel)\
**Post date:** [September 22, 2023, 5:15am UTC](https://discuss.ai.google.dev/t/tf-gradients-vs-tf-gradienttape-gradient-in-graph-mode/32098/3 "2023-09-22T05:15:26Z")

</div>

Hi @Mihir_Khambete

Welcome to the TensorFlow Forum!

`tf.gradients()` and `tf.GradientTape.gradient()` both are used for computing the gradients.

[tf.gradients](https://www.tensorflow.org/api_docs/python/tf/gradients) is only valid in a graph context and was mainly used in TF v1. In particular, it is valid in the context of a `tf.function` wrapper, where code is executing as a graph.

```auto
x = tf.Variable(3.0)
@tf.function
def example(): 
    y = x**2   
    return tf.gradients(y, x)
example()

# Output:[<tf.Tensor: shape=(), dtype=float32, numpy=6.0>]

```

`tf.GradientTape.gradient()` is more flexible for automatic differentiaton as it “records” relevant operations executed inside the context of a [`tf.GradientTape`](https://www.tensorflow.org/api_docs/python/tf/GradientTape) onto a “tape” where as `tf.gradients` was used to compute the gradient without using tape.

```auto
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
  y = x**2
  # dy = 2x * dx
dy_dx = tape.gradient(y, x)
dy_dx

# Output: <tf.Tensor: shape=(), dtype=float32, numpy=6.0>

```

Please refer to the mentioned links for the detailed understanding in this. Thank you.
