Python – tensorflow.GradientTape.watch()
Last Updated :
16 Jul, 2020
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
watch() is used to start tracing Tensor by the Tape.
Syntax: watch( tensor )
Parameter:
- tensor: It is a Tensor or list of tensors to be watched.
Returns: None
Raise:
- ValueError: It will raise ValueError if the passes parameter is not Tensor.
Example 1:
Python3
import tensorflow as tf
x = tf.constant( 4.0 )
with tf.GradientTape() as gfg:
gfg.watch(x)
y = x * x
res = gfg.gradient(y, x)
print ( "res: " , res)
|
Output:
res: tf.Tensor(8.0, shape=(), dtype=float32)
Example 2:
Python3
import tensorflow as tf
x = tf.constant( 4.0 )
z = tf.constant( 5.0 )
with tf.GradientTape(persistent = True ) as gfg:
gfg.watch([x, z])
y = z * z
u = x * x
grad_y = gfg.gradient(y, z)
grad_u = gfg.gradient(u, x)
print ( "grad_y: " , grad_y)
print ( "grad_u: " , grad_u)
|
Output:
grad_y: tf.Tensor(10.0, shape=(), dtype=float32)
grad_u: tf.Tensor(8.0, shape=(), dtype=float32)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...