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


Python tensorflow.GradientTape.reset()用法及代码示例


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。