(Keras)——Keras中自定义目标函数(损失函数)

在做kaggle项目时,看到一个人设计了一个unet,使用自定义的iou作为损失函数,才想起来原来可以自己设计损失函数…
为了实现自己的目标函数,自然想到先看下Keras中的目标函数是定义的,查下源码发现在/usr/local/lib/python3.5/dist-packages/keras中(我的系统是ubuntu16.04,使用系统自带的python3.5),Keras已经定义了一系列的目标函数。
当然是要先查看下Keras源码了…

"""Built-in loss functions.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import six
from . import backend as K
from .utils.generic_utils import deserialize_keras_object
from .utils.generic_utils import serialize_keras_object


def mean_squared_error(y_true, y_pred):
    return K.mean(K.square(y_pred - y_true), axis=-1)


def mean_absolute_error(y_true, y_pred):
    return K.mean(K.abs(y_pred - y_true), axis=-1)


def mean_absolute_percentage_error(y_true, y_pred):
    diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
                                            K.epsilon(),
                                            None))
    return 100. * K.mean(diff, axis=-1)


def mean_squared_logarithmic_error(y_true, y_pred):
    first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
    second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)
    return K.mean(K.square(first_log - second_log), axis=-1)


def squared_hinge(y_true, y_pred):
    return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)


def hinge(y_true, y_pred):
    return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)


def categorical_hinge(y_true, y_pred):
    pos = K.sum(y_true * y_pred, axis=-1)
    neg = K.max((1. - y_true) * y_pred, axis=-1)
    return K.maximum(0., neg - pos + 1.)


def logcosh(y_true, y_pred):
    """Logarithm of the hyperbolic cosine of the prediction error.

    `log(cosh(x))` is approximately equal to `(x ** 2) / 2` for small `x` and
    to `abs(x) - log(2)` for large `x`. This means that 'logcosh' works mostly
    like the mean squared error, but will not be so strongly affected by the
    occasional wildly incorrect prediction.

    # Arguments
        y_true: tensor of true targets.
        y_pred: tensor of predicted targets.

    # Returns
        Tensor with one scalar loss entry per sample.
    """
    def _logcosh(x):
        return x + K.softplus(-2. * x) - K.log(2.)
    return K.mean(_logcosh(y_pred - y_true), axis=-1)


def categorical_crossentropy(y_true, y_pred):
    return K.categorical_crossentropy(y_true, y_pred)


def sparse_categorical_crossentropy(y_true, y_pred):
    return K.sparse_categorical_crossentropy(y_true, y_pred)


def binary_crossentropy(y_true, y_pred):
    return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)


def kullback_leibler_divergence(y_true, y_pred):
    y_true = K.clip(y_true, K.epsilon(), 1)
    y_pred = K.clip(y_pred, K.epsilon(), 1)
    return K.sum(y_true * K.log(y_true / y_pred), axis=-1)


def poisson(y_true, y_pred):
    return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)


def cosine_proximity(y_true, y_pred):
    y_true = K.l2_normalize(y_true, axis=-1)
    y_pred = K.l2_normalize(y_pred, axis=-1)
    return -K.sum(y_true * y_pred, axis=-1)


# Aliases.

mse = MSE = mean_squared_error
mae = MAE = mean_absolute_error
mape = MAPE = mean_absolute_percentage_error
msle = MSLE = mean_squared_logarithmic_error
kld = KLD = kullback_leibler_divergence
cosine = cosine_proximity


def serialize(loss):
    return serialize_keras_object(loss)


def deserialize(name, custom_objects=None):
    return deserialize_keras_object(name,
                                    module_objects=globals(),
                                    custom_objects=custom_objects,
                                    printable_module_name='loss function')


def get(identifier):
    """Get the `identifier` loss function.

    # Arguments
        identifier: None or str, name of the function.

    # Returns
        The loss function or None if `identifier` is None.

    # Raises
        ValueError if unknown identifier.
    """
    if identifier is None:
        return None
    if isinstance(identifier, six.string_types):
        identifier = str(identifier)
        return deserialize(identifier)
    if isinstance(identifier, dict):
        return deserialize(identifier)
    elif callable(identifier):
        return identifier
    else:
        raise ValueError('Could not interpret '
                         'loss function identifier:', identifier)

看到源码后,事情就简单多了

首先定义损失函数IoU

## intersection over union
def IoU(y_true, y_pred, eps=1e-6):
    if np.max(y_true) == 0.0:
        return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros
    intersection = K.sum(y_true * y_pred, axis=[1,2,3])
    union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection
    return -K.mean( (intersection + eps) / (union + eps), axis=0)

然后执行编译

seg_model.compile(optimizer=Adam(1e-3, decay=1e-6), loss=IoU, metrics=['binary_accuracy'])

就是这么简单…

### 自定义损失函数的重要性 在机器学习和深度学习中,损失函数用于量化模型预测值与实际目标之间的差距。对于特定应用场景,预设的标准损失函数可能无法完全满足需求,因此自定义损失函数成为必要[^1]。 ### 使用TensorFlow实现自定义损失函数 针对商品销售量预测场景,在此提供一个基于TensorFlow框架下的具体实例: 假设存在两种不同类型的误差惩罚机制——当预测值大于真实值时代表库存积压带来的成本增加;反之则意味着错失潜在收益机会造成利润流失。此时可以构建如下形式化的表达式来表示这种不对称性的代价敏感型损失计算方法: ```python import tensorflow as tf def custom_loss(profit_factor, cost_factor): def loss(y_true, y_pred): excess_cost = tf.maximum(0., (y_pred - y_true)) * cost_factor shortage_profit_loss = tf.maximum(0., (y_true - y_pred)) * profit_factor total_loss = tf.reduce_sum(excess_cost + shortage_profit_loss) return total_loss return loss ``` 上述代码片段展示了如何创建一个接受`profit_factor`(单位短缺造成的平均利润减少额) 和 `cost_factor`(每件多余货物存储费用) 参数化配置的工厂函数,并返回适用于Keras/TensorFlow API 的损失计算器对象[^2]。 ### PyTorch中的自定义损失函数 而在PyTorch环境下,通过继承`nn.Module`类并重写其`forward()`成员函数即可轻松完成相同功能的设计工作: ```python class CustomLossFunction(nn.Module): def __init__(self, alpha=1.0, beta=1.0): super(CustomLossFunction, self).__init__() # 定义超参数alpha(影响过估计)、beta(影响低估) self.alpha = alpha self.beta = beta def forward(self, output, target): diff = output - target overestimate_penalty = F.relu(diff).pow(2).mul_(self.alpha / 2.) underestimate_penalty = F.relu(-diff).pow(2).mul_(self.beta / 2.) losses = overestimate_penalty.add_(underestimate_penalty) avg_loss = losses.mean() return avg_loss ``` 这段程序实现了带有两个调节因子α和β控制正负偏差权重比例关系的新颖版均方差(MSE),其中F.relu()为ReLU激活操作符,它能够有效地处理非线性映射问题[^3]。
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值