簡體   English   中英

在 Tensorflow 2.0 中使用 GradientTape() 和 jacobian() 時出錯

[英]Error when working with GradientTape() and jacobian() in Tensorflow 2.0

我正在 Python 中的 Tensorflow 2.0 中使用 GradientTape() 和 jacobian()。

這段代碼執行得很好:

x = tf.Variable(2.0, dtype=tf.float32)
with tf.GradientTape() as gT:
    gT.watch(x)
    g = tf.convert_to_tensor([x, 0.0], dtype=tf.float32)
dg = gT.jacobian(g, x)

但是這段代碼中斷了:

x = tf.Variable(2.0, dtype=tf.float32)
with tf.GradientTape() as gT:
    gT.watch(x)
    gv = tf.Variable([x, 0.0], dtype=tf.float32)
    g = tf.convert_to_tensor(gv , dtype=tf.float32)
dg = gT.jacobian(g, x)

並拋出錯誤:

InvalidArgumentError:您必須使用 dtype int32 [[node loop_body/Placeholder(定義於 ...Anaconda3\\lib\\site-packages\\tensorflow_core\\python\\framework\\ops.py:1751)為占位符張量“loop_body/Placeholder”提供一個值]] [操作:__inference_f_995]

模塊中的回溯(最近一次調用) ipython-input-32-686c8a0d6e95
4 gv = tf.Variable([x, 0.0], dtype=tf.float32)
5 g = tf.convert_to_tensor(gv, dtype=tf.float32)
----> 6 dg = gT.jacobian(g, x)

為什么第一個代碼有效,但第二個代碼不起作用?

原因很簡單,

在第一個例子中,你得到

g = tf.convert_to_tensor([x, 0.0], dtype=tf.float32)

並計算dg/dxgx有直接關系並且工作正常。

但在第二個例子中,

gv = tf.Variable([x, 0.0], dtype=tf.float32)
g = tf.convert_to_tensor(gv , dtype=tf.float32)

gx之間不再有聯系,因為當你打電話時,

gv = tf.Variable([x, 0.0], dtype=tf.float32)

它只是從x復制值並且不攜帶對x的引用,因此您無法獲得導數dg/dx 但是如果你嘗試dg/d(gv)它會起作用。

PS :雖然我沒有收到錯誤(對於你的第二個例子)。 我剛剛得到None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM