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]

Traceback(最近一次调用最后一次)模块
4 中的 ipython-input-32-686c8a0d6e95 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)

为什么第一个代码有效,而第二个代码无效?

4

1 回答 1

1

原因很简单,

在第一个例子中,你得到

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

和计算dg/dxg有直接的关系,x工作正常。

但在第二个例子中,

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

于 2020-01-05T19:22:23.220 回答