Tensorflow GradientTape keeps returning none when trying to define my own loss function

61 Views Asked by At

I am attempting to code a PINN (physics informed neural network) that has a custom loss function based off of partial differential equations that govern fluid flow. I have included a screenshot of the formulas to provide extra context. The values q_hat and h_hat correspond to the outputted values from the neural network that have been estimated from values of distance and time (x and t).

Currently I haven't been able to get a value back from the first term of the first equation, F1 as this returns the error: "Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor". My values of q_hat and t are given as tf.Variable and are the same shape of (380, 1) as there are 380 time steps for this simulation. The cross sectional area is given as a tf.constant.

The segment of the code in question is given below:

t = tf.Variable(t_train_values, dtype=tf.float32)
x = tf.Variable(x_train_values, dtype=tf.float32)
q_hat = tf.reshape(q_hat_a, (380,1))
h_hat = tf.reshape(h_hat_a, (380,1))

def F1(q_hat, h_hat, x, t, Cs_A, g, f, diam):
    with tf.GradientTape(persistent = True) as tape:
    
    tape.watch(q_hat)
    tape.watch(h_hat)
    tape.watch(x)
    tape.watch(t)
    
    term1 = Cs_A * tape.gradient(q_hat, t)
    term2 = q_hat * tape.gradient(q_hat, x)
    term3 = g * Cs_A**2 * tape.gradient(h_hat, x)
    term4 = f*((tf.abs(q_hat) * q_hat) / 2 * diam)
    
return term1 + term2 + term3 + term4

with tf.GradientTape(persistent = True) as tape:

Cs_A = tf.constant(Cs_A0, dtype=tf.float32)
diam = tf.constant(diam0)
f = tf.constant(f0)
g = tf.constant(g0)
a = tf.constant(a0)

tape.watch(q_hat)
tape.watch(h_hat)
tape.watch(x)
tape.watch(t)
    
F1_val = F1(q_hat, h_hat, x ,t, Cs_A, g, f, diam)

Any help is greatly appreciated :). Please let me know if you need more information, thanks.

0

There are 0 best solutions below