Python – tensorflow.GradientTape.gradient()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. 

gradient() is used to computes the gradient using operations recorded in context of this tape.

Syntax: gradient(target, sources, output_gradients, unconnected_gradients)

Parameters:

  • target: It is Tensor or list of Tensor to be differentiated.
  • sources: It is Tensor or list of Tensor. Target values are differentiated against the source.
  • output_gradients: It is a list of gradients with default value None.
  • unconnected_gradients: It’s value can be either none or zero with default value none.

Returns: It returns a list or nested structure of Tensor.



Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the library
import tensorflow as tf
  
x = tf.constant(4.0)
  
# Using GradientTape
with tf.GradientTape() as gfg:
  gfg.watch(x)
  y = x * x * x
  
# Computing gradient
res  = gfg.gradient(y, x) 
  
# Printing result
print("res: ",res)

chevron_right


Output:


res:  tf.Tensor(48.0, shape=(), dtype=float32)

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the library
import tensorflow as tf
  
x = tf.constant(4.0)
  
# Using GradientTape
with tf.GradientTape() as gfg:
  gfg.watch(x)
  
  # Using nested GradientTape for 
  # calculating higher order derivative
  with tf.GradientTape() as gg:
    gg.watch(x)
    y = x * x * x
  
  # Computing first order gradient
  first_order = gg.gradient(y, x)
  
# Computing Second order gradient
second_order  = gfg.gradient(first_order, x) 
  
# Printing result
print("first_order: ",first_order)
print("second_order: ",second_order)

chevron_right


Output:


first_order:  tf.Tensor(48.0, shape=(), dtype=float32)
second_order:  tf.Tensor(24.0, shape=(), dtype=float32)

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.