BCEWithLogitsLoss
-
class torch.nn.BCEWithLogitsLoss(weight=None, size_average=None, reduce=None, reduction='mean', pos_weight=None)
[source] -
This loss combines a
Sigmoid
layer and theBCELoss
in one single class. This version is more numerically stable than using a plainSigmoid
followed by aBCELoss
as, by combining the operations into one layer, we take advantage of the log-sum-exp trick for numerical stability.The unreduced (i.e. with
reduction
set to'none'
) loss can be described as:where is the batch size. If
reduction
is not'none'
(default'mean'
), thenThis is used for measuring the error of a reconstruction in for example an auto-encoder. Note that the targets
t[i]
should be numbers between 0 and 1.It’s possible to trade off recall and precision by adding weights to positive examples. In the case of multi-label classification the loss can be described as:
where is the class number ( for multi-label binary classification, for single-label binary classification), is the number of the sample in the batch and is the weight of the positive answer for the class .
increases the recall, increases the precision.
For example, if a dataset contains 100 positive and 300 negative examples of a single class, then
pos_weight
for the class should be equal to . The loss would act as if the dataset contains positive examples.Examples:
>>> target = torch.ones([10, 64], dtype=torch.float32) # 64 classes, batch size = 10 >>> output = torch.full([10, 64], 1.5) # A prediction (logit) >>> pos_weight = torch.ones([64]) # All weights are equal to 1 >>> criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight) >>> criterion(output, target) # -log(sigmoid(1.5)) tensor(0.2014)
- Parameters
-
-
weight (Tensor, optional) – a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size
nbatch
. -
size_average (bool, optional) – Deprecated (see
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the fieldsize_average
is set toFalse
, the losses are instead summed for each minibatch. Ignored whenreduce
isFalse
. Default:True
-
reduce (bool, optional) – Deprecated (see
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending onsize_average
. Whenreduce
isFalse
, returns a loss per batch element instead and ignoressize_average
. Default:True
-
reduction (string, optional) – Specifies the reduction to apply to the output:
'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. Note:size_average
andreduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction
. Default:'mean'
- pos_weight (Tensor, optional) – a weight of positive examples. Must be a vector with length equal to the number of classes.
-
weight (Tensor, optional) – a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size
- Shape:
-
- Input: where means, any number of additional dimensions
- Target: , same shape as the input
- Output: scalar. If
reduction
is'none'
, then , same shape as input.
Examples:
>>> loss = nn.BCEWithLogitsLoss() >>> input = torch.randn(3, requires_grad=True) >>> target = torch.empty(3).random_(2) >>> output = loss(input, target) >>> output.backward()
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.8.0/generated/torch.nn.BCEWithLogitsLoss.html