當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.GradientTape.stop_recording()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

stop_recording()用於暫時停止記錄操作。如果磁帶未在錄製,則會引發錯誤。

用法:tensorflow.GradientTape.stop_recording()

參數:它不接受任何參數。

返回:



raise :

  • RunTimeError:如果磁帶沒有及時錄製,它將引發RunTimeError。

範例1:

Python3

# Importing the library 
import tensorflow as tf 
  
x = tf.constant(4.0) 
  
# Using GradientTape 
with tf.GradientTape() as gfg:
  gfg.watch(x) 
  
  # Stop recording 
  with gfg.stop_recording():
    y = x * x * x 
  
# Computing gradient 
res = gfg.gradient(y, x)  
  
# Printing result 
print("res:", res)

輸出:


res: None

範例2:

Python3

# Importing the library 
import tensorflow as tf 
  
x = tf.constant(4.0) 
  
# Using GradientTape 
with tf.GradientTape() as gfg:
  gfg.watch(x) 
  
  # Stop recording 
  with gfg.stop_recording():
    y = x * x * x 
  
  # Starting the recording again 
  gfg.watch(x) 
  y = x * x 
  
# Computing gradient 
res = gfg.gradient(y, x)  
  
# Printing result 
print("res:", res)

輸出:


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




相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.GradientTape.stop_recording()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。