Python 如何在keras中实现自定义成本函数?

Python 如何在keras中实现自定义成本函数?,python,python-3.x,tensorflow,keras,deep-learning,Python,Python 3.x,Tensorflow,Keras,Deep Learning,我有一个如下的代价函数=argmin L1+L2,其中L1是均方误差,L2是 -λ和(平方((y)x(z)),其中y是预测输出图像,z是模型的给定输入图像。y和z的元素相乘,然后取其平方。λ是L1和L2之间的折衷参数。我不知道如何在中实现,我按如下方式实现 def custom_loss(i): def loss(y_true, y_pred): y_true=K.cast(y_true, dtype='float32') y_

我有一个如下的代价函数=argmin L1+L2,其中L1是均方误差,L2是 -λ和(平方((y)x(z)),其中y是预测输出图像,z是模型的给定输入图像。y和z的元素相乘,然后取其平方。λ是L1和L2之间的折衷参数。我不知道如何在中实现,我按如下方式实现

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

一部分一部分地打断你的问题

其中L1为均方误差

因此,
L1=np.square(np.subtract(y\u true,y\u pred)).mean()

L2是-λ和(平方((y)x(z)),其中y是预测值 输出图像,z是模型的给定输入图像。元素的 y和z相乘,然后取其平方

因此,
L2=np.sum(np.concatenate(np.square(np.multiply(y\u true,y\u pred)))
您意识到L2将是一个非常大的损失数字。

总而言之,损失函数是这样的-

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
我在这里编写了一个简单的代码,将图像加载为y_true,并裁剪y_pred的中心部分,然后执行您提到的损失(由于值太大,意义不大)

代码-

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)
输出-

-251577020000000.0

一部分一部分地打断你的问题

其中L1为均方误差

因此,
L1=np.square(np.subtract(y\u true,y\u pred)).mean()

L2是-λ和(平方((y)x(z)),其中y是预测值 输出图像,z是模型的给定输入图像。元素的 y和z相乘,然后取其平方

因此,
L2=np.sum(np.concatenate(np.square(np.multiply(y\u true,y\u pred)))
您意识到L2将是一个非常大的损失数字。

总而言之,损失函数是这样的-

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
我在这里编写了一个简单的代码,将图像加载为y_true,并裁剪y_pred的中心部分,然后执行您提到的损失(由于值太大,意义不大)

代码-

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)
输出-

-251577020000000.0

问题是什么?我想知道我的实现是否正确。嗨@Shaleel,你能试着向你的损失函数提供一个输出场景的示例,并检查它是否是你想要的输出吗?如果没有,你能为测试提供一个最小的可复制代码吗?问题是什么?我想知道我的实现是否正确。Hi@Shaleel,你能尝试向你的损失函数提供一个输出场景的示例,并检查它是否是你想要的输出吗?如果没有,你能为测试提供一个最低限度的可复制代码吗?@Shaleel-如果答案回答了你的问题,请你接受并投赞成票。谢谢。@Shaleel-如果答案回答了你的问题,你能接受并投票吗。非常感谢。