oneflow.nn.BCEWithLogitsLoss¶
-
class
oneflow.nn.
BCEWithLogitsLoss
(weight: Optional[oneflow.Tensor] = None, reduction: str = 'mean', pos_weight: Optional[oneflow.Tensor] = None)¶ This operator combines the Sigmoid and BCELoss together. For numerical stability, we apply some math tricks instead of using Sigmoid layer with BCELoss.
The equation is:
if
reduction
="none"
:\[out = -weight*[Pos\_weight*y*log\sigma({x}) + (1-y)*log(1-\sigma(x))]\]if
reduction
="mean"
:\[out = -\frac{weight}{n}\sum_{i=1}^n[Pos\_weight*y*log\sigma({x}) + (1-y)*log(1-\sigma(x))]\]if
reduction
="sum"
:\[out = -weight*\sum_{i=1}^n[Pos\_weight*y*log\sigma({x}) + (1-y)*log(1-\sigma(x))]\]- Parameters
weight (Tensor, optional) – The manual rescaling weight to the loss. Default:
None
size_average (bool, optional) – Deprecated (see
reduction
). Default:True
reduce (bool, optional) – Deprecated (see
reduction
). Default:True
reduction (str, optional) – The reduce type, it can be one of
"none"
,"mean"
,"sum"
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:"mean"
pos_weight (Tensor, optional) – The manual rescaling weight to the positive examples. Default:
None
- Shape:
Input: \((N,*)\) where * means, any number of additional dimensions
Target: \((N,*)\), same shape as the input
Output: scalar. If
reduction
is"none"
, then \((N,*)\), same shape as input.
For example:
>>> import oneflow as flow >>> input = flow.tensor([[1.2, 0.2, -0.3], [0.7, 0.6, -2], [0.7, 0.6, -2]], dtype=flow.float32) >>> target = flow.tensor([[0, 1, 0], [1, 0, 1], [1, 0, 1]], dtype=flow.float32) >>> weight = flow.tensor([[2, 2, 2], [2, 2, 2], [2, 2, 2]], dtype=flow.float32) >>> pos_weight = flow.tensor([1.2, 1.3, 1.4], dtype=flow.float32) >>> m = flow.nn.BCEWithLogitsLoss(weight=weight, pos_weight=pos_weight, reduction="none") >>> out = m(input, target) >>> out tensor([[2.9266, 1.5552, 1.1087], [0.9676, 2.0750, 5.9554], [0.9676, 2.0750, 5.9554]], dtype=oneflow.float32) >>> m = flow.nn.BCEWithLogitsLoss(weight=weight, pos_weight=pos_weight, reduction="mean") >>> out = m(input, target) >>> out tensor(2.6207, dtype=oneflow.float32) >>> m = flow.nn.BCEWithLogitsLoss(weight=weight, pos_weight=pos_weight, reduction="sum") >>> out = m(input, target) >>> out tensor(23.5865, dtype=oneflow.float32)