Skip to content
Related Articles

Related Articles

Python – tensorflow.GradientTape.watch()
  • Last Updated : 16 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

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)


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :