i have problems in the computation of gradients using automatic differentiation in TensorFlow. Basically i want to create a neural network which has just one output-value f and get an input of two values (x,t). The network should act like a mathematical function, so in this case f(x,t) where x and t are the input-variables and i want to compute partial derivatives, for example df_dx, d2f/dx2
or df_dt
. I need those partial derivatives later for a specific loss-function.
Here is my simplified code:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.flatten = Flatten(input_shape=(2, 1))
self.d1 = Dense(28)
self.f = Dense(1)
def call(self, y):
y = self.flatten(y)
y = self.d1(y)
y = self.f(y)
return y
if __name__ == "__main__":
#inp contains the input-variables (x,t)
inp = np.random.rand(1,2,1)
inp_tf = tf.convert_to_tensor(inp, np.float32)
#Create a Model
model = MyModel()
#Here comes the important part:
x = inp_tf[0][0]
t = inp_tf[0][1]
with tf.GradientTape(persistent=True) as tape:
tape.watch(inp_tf[0][0])
tape.watch(inp_tf)
f = model(inp_tf)
df_dx = tape.gradient(f, inp_tf[0][0]) #Derivative df_dx
grad_f = tape.gradient(f, inp_tf)
tf.print(f) #--> [[-0.0968768075]]
tf.print(df_dx) #--> None
tf.print(grad_f) #--> [[[0.284864038]
# [-0.243642956]]]
What i expected was that i get df_dx = [0.284864038]
(the first component of grad_f), but it results in None
. My questions are:
- Is it possible to get partial derivatives of f to only one input-variable?
- If yes: What i have to change in my code that the computation df_dx doesn't result
None
?
What i think could do is to modify the architecture of the class MyModel
that i use two different Inputlayer (one for x and one for t) so that i can call the model like f = model(x,t)
but that seems unnatural for me and i think there should be an easier way.
Another point is that i don't get an Error when i change the input_shape of the Flattenlayer for example to self.flatten = Flatten(input_shape=(5,1)
but my inputvector has shape(1,2,1), so i expect to get an error but that's not the case, why? I'm grateful for your help :)
I use the following configurations:
- Visual Studio Code with Python-Extension as IDE
- Python-Version: 3.7.6
- TensorFlow-Version: 2.1.0
- Keras-Version: 2.2.4-tf