TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。
reset()用於清除磁帶存儲的所有信息。
用法:reset()
參數:它不接受任何參數。
返回:它不返回任何內容。
範例1:
Python3
# Importing the library
import tensorflow as tf
x = tf.constant(4.0)
# Using GradientTape
with tf.GradientTape() as gfg:
gfg.watch(x)
y = x * x * x
y+=x*x
# Computing gradient without reset
res = gfg.gradient(y, x)
# Printing result
print("res(y = x*x*x + x*x):",res)
# Using GradientTape
with tf.GradientTape() as gfg:
gfg.watch(x)
y = x * x * x
# Resetting the Tape
gfg.reset()
gfg.watch(x)
y+=x*x
# Computing gradient with reset
res = gfg.gradient(y, x)
# Printing result
print("res(y = x*x):",res)
輸出:
res(y = x*x*x + x*x): tf.Tensor(56.0, shape=(), dtype=float32) res(y = x*x): tf.Tensor(8.0, shape=(), dtype=float32)
範例2:
Python3
# Importing the library
import tensorflow as tf
x = tf.constant(3.0)
# Using GradientTape
with tf.GradientTape() as gfg:
gfg.watch(x)
y = x * x
y+=x*x
# Computing gradient without reset
res = gfg.gradient(y, x)
# Printing result
print("res(y = x*x + x*x):",res)
# Using GradientTape
with tf.GradientTape() as gfg:
gfg.watch(x)
y = x * x
# Resetting the Tape
gfg.reset()
gfg.watch(x)
y+=x
# Computing gradient with reset
res = gfg.gradient(y, x)
# Printing result
print("res(y = x):",res)
輸出:
res(y = x*x + x*x): tf.Tensor(12.0, shape=(), dtype=float32) res(y = x): tf.Tensor(1.0, shape=(), dtype=float32)
相關用法
注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.GradientTape.reset()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。