当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tesnsorflow.grad_pass_through()用法及代码示例


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

grad_pass_through()用于创建grad-pass-through操作,并按函数传递正向行为。

Syntax: tensorflow.grad_passs_through( f )

参数:

  • 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.  



范例1:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initiaizing 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)

输出:


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 
  
# Initiaizing 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)




相关用法


注:本文由纯净天空筛选整理自aman neekhara大神的英文原创作品 Python – tesnsorflow.grad_pass_through()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。