I am trying to calculate the second derivative of a simple vector function of a scalar variable f(x) = (x,x^2,x^3)
using TF 2.3 with tf.GradientTape
.
def f_ab(x):
return x, x** 2, x** 3
import tensorflow as tf
in1 = tf.cast(tf.convert_to_tensor(tf.Variable([-1,3,0,6]))[:,None],tf.float64)
with tf.GradientTape(persistent=True) as tape2:
tape2.watch(in1)
with tf.GradientTape(persistent=True) as tape:
tape.watch(in1)
f1,f2,f3 = f_ab(in1)
df1 = tape.gradient(f1, in1)
df2 = tape.gradient(f2, in1)
df3 = tape.gradient(f3, in1)
d2f1_dx2 = tape2.gradient(df1, in1)
d2f2_dx2 = tape2.gradient(df2, in1)
d2f3_dx2 = tape2.gradient(df3, in1)
for some reason, only the last two derivative are correct while the first, d2f1_dx2
, turned out to be None
.
When I changed f_ab
to
def f_ab(x):
return x** 1, x** 2, x**3
I got d2f1_dx2 = <tf.Tensor: shape=(1, 4), dtype=float64, numpy=array([[-0., 0., nan, 0.]])>
which is "almost" the correct result.
only when I changed f_ab
to
def f_ab(inputs_train):
return tf.math.log(tf.math.exp(x) ), x** 2, x**3
I got the correct result: d2f1_dx2 = <tf.Tensor: shape=(1, 4), dtype=float64, numpy=array([[0., 0., 0., 0.]])>
Has anyone encountered this problem before? why is the straight forward way gives None
?