Friday, October 24, 2025
HomeLanguagesPython – tensorflow.GradientTape.reset()

Python – tensorflow.GradientTape.reset()

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

reset() is used to clear all information that is stored by the Tape.

Syntax: reset()

Parameters: It doesn’t accept any parameters.

Returns: It returns none.

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
  y+=x*x
  
# Computing gradient without reset
res  = gfg.gradient(y, x) 
  
# Printing result
print("res(y = x*x*x + x*x): ",res)
  
# Using GradientTape
with tf.GradientTape() as gfg:
  gfg.watch(x)
  y = x * x * x
  
  # Resetting the Tape
  gfg.reset()
    
  gfg.watch(x)
  y+=x*x
  
# Computing gradient with reset
res  = gfg.gradient(y, x) 
  
# Printing result
print("res(y = x*x): ",res)


Output:


res(y = x*x*x + x*x):  tf.Tensor(56.0, shape=(), dtype=float32)
res(y = x*x):  tf.Tensor(8.0, shape=(), dtype=float32)

Example 2:

Python3




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


Output:


res(y = x*x + x*x):  tf.Tensor(12.0, shape=(), dtype=float32)
res(y = x):  tf.Tensor(1.0, shape=(), dtype=float32)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS