Open In App

Python – tensorflow.GradientTape.stop_recording()

Last Updated : 20 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

stop_recording() is used to temporarily stop recording operation. If the Tape is not recording it will raise an error.

Syntax: tensorflow.GradientTape.stop_recording()

Parameters: It doesn’t accept any parameters.

Returns: None

Raises:

  • RunTimeError: It will raise RunTimeError if tape is not recording currently.

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)
  
  # Stop recording
  with gfg.stop_recording():
    y = x * x * x
  
# Computing gradient
res = gfg.gradient(y, x) 
  
# Printing result
print("res: ", res)


Output:


res:  None

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)
  
  # Stop recording
  with gfg.stop_recording():
    y = x * x * x
  
  # Starting the recording again
  gfg.watch(x)
  y = x * x
  
# Computing gradient
res = gfg.gradient(y, x) 
  
# Printing result
print("res: ", res)


Output:


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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads