Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python – tensorflow.GradientTape()

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

GradientTape() is used to record operations for automatic differentiation.

Syntax: tensorflow.GradientTape( persistent, watch_accessed_variables)

Parameters:

  • persistent(optional): It can either be True or False with default value False. It defines whether persistent gradient tape is created or not.
  • watch_accessed_variables:  It is a boolean defining whether the tape will automatically watch any (trainable) variables accessed while the tape is active or not.

Example 1:

Python3




# 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)

Output:

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

Example 2:

Python3




# 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)

Output:

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



My Personal Notes arrow_drop_up
Last Updated : 10 Jul, 2020
Like Article
Save Article
Similar Reads
Related Tutorials