简体   繁体   中英

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

I am working with GradientTape() and jacobian() in Tensorflow 2.0 in Python.

This code executes fine:

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)

But this code breaks:

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)

and throws the error:

InvalidArgumentError: You must feed a value for placeholder tensor 'loop_body/Placeholder' with dtype int32 [[node loop_body/Placeholder (defined at ...Anaconda3\\lib\\site-packages\\tensorflow_core\\python\\framework\\ops.py:1751) ]] [Op:__inference_f_995]

Traceback (most recent call last) ipython-input-32-686c8a0d6e95 in module
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)

Why does the first code work, but the second code doesn't?

The reason is pretty simple,

In the first example, you got

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

and compute dg/dx and g has a direct relationship with x and works fine.

But in the second example,

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

There's no connection between g and x any more, because when you call,

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

it just copies the values from x and doesn't carry a reference to x , so you cannot get derivative dg/dx . But if you try dg/d(gv) it'll work.

PS : I didn't get an error though (for your second example). I just got None .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM