📜  Python – tensorflow.grad_pass_through()

📅  最后修改于: 2022-05-13 01:54:40.577000             🧑  作者: Mango

Python – tensorflow.grad_pass_through()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

grad_pass_through()用于通过函数创建具有前向行为传递的 grad-pass-through 操作。

示例 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)


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)


输出:

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

示例 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)

输出:

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