Python – tensorflow.GradientTape.stop_recording()
TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。
stop_recording()用于暂时停止录制操作。如果磁带未录制,则会引发错误。
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.
示例 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)
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)
输出:
res: None
示例 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)
输出:
res: tf.Tensor(8.0, shape=(), dtype=float32)