Wednesday, January 21, 2026
HomeLanguagesPython – tensorflow.grad_pass_through()

Python – tensorflow.grad_pass_through()

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)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS