Why is this Tensorflow gradient tape returning None?

Question:

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.Tensors

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?

Asked By: ewr3243

||

Answers:

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.

Answered By: ewr3243
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.