【发布时间】:2020-04-22 23:49:17
【问题描述】:
我正在使用 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 为占位符张量“loop_body/Placeholder”提供一个值 [[node loop_body/Placeholder(定义在...Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1751)]] [Op:__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)
为什么第一个代码有效,而第二个代码无效?
【问题讨论】:
标签: python tensorflow deep-learning tensorflow2.0 gradienttape