Python – Custom Keras loss (not of the form f(y_true, y_pred))

Custom Keras loss (not of the form f(y_true, y_pred))… here is a solution to the problem.

Custom Keras loss (not of the form f(y_true, y_pred))

def charbonnier(I_x, I_y, I_t, U, V, e):
    loss = K.sqrt(K.pow((U*I_x + V*I_y + I_t), 2) + e)
    return K.sum(loss)

I want to use this cost function and optimize it for you and V. I’m currently working on getting it to work with Keras because the Keras loss function can only have the form f(y_true, y_pred).

loss

My model is completely unsupervised and I have no basic facts. I_x, I_y, and I_t are constants, and the goal of the model is to learn U and V that minimize E(F). So my question is:
What is the correct way to implement this loss function (without the form f(y_true, y_pred) in Keras?

Solution

Define the loss function as follows:

def charbonnier(I_x, I_y, I_t, U, V, e)
    def loss_fun(y_true, y_pred):
        loss = K.sqrt(K.pow((U*I_x + V*I_y + I_t), 2) + e)
        return K.sum(loss)
    return loss_fun

Related Problems and Solutions