Reputation: 441
I am using Tensorflow 2.0 and computing second derivative. However, tensorflow is returning None
for u_tt
and u_xx
. u_x
, u_t
are computed properly. Variables u,x,t
are defined before and are tf.Tensor
s
import tensorflow as tf
# defining u,x,t
with tf.GradientTape(persistent=True) as tp2:
with tf.GradientTape(persistent=True) as tp1:
tp1.watch(t)
tp1.watch(x)
u_x = tp1.gradient(u, x) # shape (1000,1)
u_t = tp1.gradient(u, t) # shape (1000,1)
u_tt = tp2.gradient(u_t, t) # None??
u_xx = tp2.gradient(u_x, x) # None??
Any idea what is wrong in the second derivative computation?
Upvotes: 1
Views: 168
Reputation: 441
Following solution worked.
with tf.GradientTape(persistent=True) as tp2:
with tf.GradientTape(persistent=True) as tp1:
tp1.watch(t)
tp1.watch(x)
u_x = tp1.gradient(u, x)
u_t = tp1.gradient(u, t)
u_tt = tp1.gradient(u_t, t)
u_xx = tp1.gradient(u_x, x)
It would be helpful to know why original version, as per https://www.tensorflow.org/guide/advanced_autodiff, doesn't work.
Upvotes: 1