MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / weight_reduce_loss

Function weight_reduce_loss

detrsmpl/models/losses/utils.py:27–53  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

25
26
27def 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
56def weighted_loss(loss_func):

Callers 6

wrapperFunction · 0.85
cross_entropyFunction · 0.85
binary_cross_entropyFunction · 0.85
py_sigmoid_focal_lossFunction · 0.85
py_focal_loss_with_probFunction · 0.85
sigmoid_focal_lossFunction · 0.85

Calls 1

reduce_lossFunction · 0.85

Tested by

no test coverage detected