Open In App

Python – tensorflow.grad_pass_through()

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. 

grad_pass_through() is used to create grad-pass-through operation with forward behavior passes by function.

Syntax:  tensorflow.grad_pass_through( f )

Parameters:

  • f:  It is a function which returns a Tensor or nested structure of Tensor.

Returns:  It returns a function h(x) which returns the same values as f(x) and whose gradients are the same as those of an identity function.  

Example 1:

Python3




# Importing the library
import tensorflow as tf
 
# Initializing the Tensor
x = tf.Variable(2.0, name ="x")
z = tf.Variable(4.0, name ="z")
 
with tf.GradientTape() as gfg:
  # y will evaluate to 16.0 i.e 4**2
  y = tf.grad_pass_through(x.assign)(z**2)
 
# res will evaluate to 8.0
res = gfg.gradient(y, z)
 
# Printing result
print("y: ", y)
print("res: ", res)


Output:

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

Example 2:

Python3




# Importing the library
import tensorflow as tf
 
# Initializing the Tensor
x = tf.Variable(3.0, name ="x")
 
with tf.GradientTape() as gfg:
  # y will evaluate to 9.0 i.e 3**2
  y = tf.grad_pass_through(x.assign)(x**2)
 
# res will evaluate to 6.0
res = gfg.gradient(y, x)
 
# Printing result
print("y: ", y)
print("res: ", res)


Output:

y:  tf.Tensor(9.0, shape=(), dtype=float32)
res:  tf.Tensor(6.0, shape=(), dtype=float32)


Last Updated : 23 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads