pyiqa.losses.loss_util ====================== .. py:module:: pyiqa.losses.loss_util Module Contents --------------- .. py:function:: reduce_loss(loss, reduction) Reduce loss as specified. :param loss: Elementwise loss tensor. :type loss: Tensor :param reduction: Options are 'none', 'mean' and 'sum'. :type reduction: str :returns: Reduced loss tensor. :rtype: Tensor .. py:function:: weight_reduce_loss(loss, weight=None, reduction='mean') Apply element-wise weight and reduce loss. :param loss: Element-wise loss. :type loss: Tensor :param weight: Element-wise weights. Default: None. :type weight: Tensor :param reduction: Same as built-in losses of PyTorch. Options are 'none', 'mean' and 'sum'. Default: 'mean'. :type reduction: str :returns: Loss values. :rtype: Tensor .. py:function:: weighted_loss(loss_func) Create a weighted version of a given loss function. To use this decorator, the loss function must have the signature like `loss_func(pred, target, **kwargs)`. The function only needs to compute element-wise loss without any reduction. This decorator will add weight and reduction arguments to the function. The decorated function will have the signature like `loss_func(pred, target, weight=None, reduction='mean', **kwargs)`. :Example: >>> import torch >>> @weighted_loss >>> def l1_loss(pred, target): >>> return (pred - target).abs() >>> pred = torch.Tensor([0, 2, 3]) >>> target = torch.Tensor([1, 1, 1]) >>> weight = torch.Tensor([1, 0, 1]) >>> l1_loss(pred, target) tensor(1.3333) >>> l1_loss(pred, target, weight) tensor(1.5000) >>> l1_loss(pred, target, reduction='none') tensor([1., 1., 2.]) >>> l1_loss(pred, target, weight, reduction='sum') tensor(3.)