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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。