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
# Importing the library import tensorflow as tf x = tf.constant( 4.0 ) # Using GradientTape with tf.GradientTape() as gfg: # Starting the recording x 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)
Example 2:
Python3
# Importing the library import tensorflow as tf x = tf.constant( 4.0 ) z = tf.constant( 5.0 ) # Using GradientTape with tf.GradientTape(persistent = True ) as gfg: # Starting the recording x and z gfg.watch([x, z]) y = z * z u = x * x # Computing gradient grad_y = gfg.gradient(y, z) grad_u = gfg.gradient(u, x) # Printing result 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)