我在 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)
为什么第一个代码有效,而第二个代码无效?