简体   繁体   中英

How to implement a custom cost function in keras?

I have a following cost function= argmin L1+L2, where L1 is Mean Squared Error and L2 is -λ Summation( Square((y) x (z) )) where y is the predicted output image and z is the given input image to model. Elementwise multiplication of y and z and then taking square of it. λ is a trade off parameter between L1 and L2. I am not sure how to implement in, I did it as follows

def custom_loss(i):

        def loss(y_true, y_pred):

            y_true=K.cast(y_true, dtype='float32')
            y_pred=K.cast(y_pred, dtype='float32')
            input_image=K.cast(i, dtype='float32')

            mul=tf.math.multiply(input_image,y_pred)
            L1=K.mean(K.square(mul),axis=1)
            L2=K.mean(K.square(y_pred - y_true), axis=-1)
            closs=L1-L2
            return closs

        return loss

To break your question part by part

where L1 is Mean Squared Error

Thus, L1 = np.square(np.subtract(y_true,y_pred)).mean()

L2 is -λ Summation( Square((y) x (z) )) where y is the predicted output image and z is the given input image to model. Elementwise multiplication of y and z and then taking square of it

Thus, L2 = np.sum(np.concatenate(np.square(np.multiply(y_true,y_pred)))) . You realize that L2 will be a very big number for a loss.

To Summarize this is how your loss function looks like -

def custom_loss(y_true,y_pred):
    def loss(y_true, y_pred):
        y_true = img_to_array(y_true)
        y_pred = img_to_array(y_pred)
        L1 = np.square(np.subtract(y_true,y_pred)).mean()
        L2 = np.sum(np.concatenate(np.square(np.multiply(y_true,y_pred))))
        loss=L1-L2
        return loss

I have written a simple code here to load a image as y_true and crop central part for y_pred and perform the loss you mentioned (doesn't make much meaning as the value is to big) .

Code -

import tensorflow as tf
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array, array_to_img
from matplotlib import pyplot

# Load the image
y_true = load_img('/content/bird.jpg', target_size=(224, 224))

# Convert Image to array
image = img_to_array(y_true)

# Central crop image
image = tf.image.central_crop(image, np.random.uniform(0.50, 1.00))

# Resize to original size of image
image = tf.image.resize(image, (224, 224))

# convert the image to an array
y_pred = array_to_img(image)

# def custom_loss():
#     def loss(y_true, y_pred):
#         y_true = img_to_array(y_true)
#         y_pred = img_to_array(y_pred)
#         L1 = np.square(np.subtract(y_true,y_pred)).mean()
#         L2 = np.sum(np.concatenate(np.square(np.multiply(y_true,y_pred))))
#         loss=L1-L2
#         return loss

def loss(y_true, y_pred):
    y_true = img_to_array(y_true)
    y_pred = img_to_array(y_pred)
    L1 = np.square(np.subtract(y_true,y_pred)).mean()
    L2 = np.sum(np.concatenate(np.square(np.multiply(y_true,y_pred))))
    loss=L1-L2
    return loss

x = loss(y_true,y_pred)
print(x)

Output -

-251577020000000.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM