I am trying to use a custom loss function in my Keras
model (TensorFlow 2). This custom loss (ideally) will calculate the data loss plus the residual of a physical equation (say, diffusion equation, Navier Stokes, etc.). This residual error is based on the model output derivative wrt its inputs and I want to use GradientTape
.
In this MWE, I removed the data loss term and other equation losses, and just used the derivative of the output wrt its first input. The dataset can be found here.
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf #tf.__version__ = '2.3.0'
# tf.compat.v1.disable_eager_execution()
# load the dataset
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8] #X.shape = (768, 8)
y = dataset[:,8]
def customLoss(yTrue,yPred):
x_tensor = tf.convert_to_tensor(model.input, dtype=tf.float32)
x_tensor = tf.cast(x_tensor, tf.float32)
with tf.GradientTape() as t:
t.watch(x_tensor)
output = model(x_tensor)
DyDX = t.gradient(output, x_tensor)
dy_t = DyDX[:, 5:6][0]
R_pred=dy_t
# loss_data = tf.reduce_mean(tf.square(yTrue - yPred), axis=-1)
loss_PDE = tf.reduce_mean(tf.square(R_pred))
return loss_PDE
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss=customLoss, optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=15, batch_size=10)
After execution, I get this _SymbolicException
:
_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'dense_6_input:0' shape=(None, 8) dtype=float32>]
When I uncomment tf.compat.v1.disable_eager_execution()
, the issue seems to vanish and the model starts training. I was wondering why I am getting this _SymbolicException
and how can I work it out without disabling eager execution. Any ideas?