I want to define a custom loss function using Keras, which contains the gradient of the difference between y_true and y_pred.
I found numpy.gradient
can help me get the gradient of an array.
So part of my code for loss function looks like this:
def loss(y_true, y_pred):
d = y_true - y_pred
gradient_x = np.gradient(d, axis=0)
gradient_y = np.gradient(d, axis=1)
but it turns out d
is a Tensorflow tensor class and numpy.gradient
can't process it.
I'm kind of new to Keras and Tensorflow.
Is there any other function can help me do this? Or I have to compute the gradient by myself?