Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns:
(loss, weight=None, reduction='mean', avg_factor=None)
| 25 | |
| 26 | |
| 27 | def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): |
| 28 | """Apply element-wise weight and reduce loss. |
| 29 | |
| 30 | Args: |
| 31 | loss (Tensor): Element-wise loss. |
| 32 | weight (Tensor): Element-wise weights. |
| 33 | reduction (str): Same as built-in losses of PyTorch. |
| 34 | avg_factor (float): Average factor when computing the mean of losses. |
| 35 | |
| 36 | Returns: |
| 37 | Tensor: Processed loss values. |
| 38 | """ |
| 39 | # if weight is specified, apply element-wise weight |
| 40 | if weight is not None: |
| 41 | loss = loss * weight |
| 42 | |
| 43 | # if avg_factor is not specified, just reduce the loss |
| 44 | if avg_factor is None: |
| 45 | loss = reduce_loss(loss, reduction) |
| 46 | else: |
| 47 | # if reduction is mean, then average the loss by avg_factor |
| 48 | if reduction == 'mean': |
| 49 | loss = loss.sum() / avg_factor |
| 50 | # if reduction is 'none', then do nothing, otherwise raise an error |
| 51 | elif reduction != 'none': |
| 52 | raise ValueError('avg_factor can not be used with reduction="sum"') |
| 53 | return loss |
| 54 | |
| 55 | |
| 56 | def weighted_loss(loss_func): |
no test coverage detected