stay Pytorch in ,BCELoss and BCEWithLogitsLoss It is a set of commonly used binary cross entropy loss functions , It is often used in binary classification , The difference is that the input of the former is done
sigmoid Processed value , The latter is sigmoid function 1 1 + exp ⁡ ( − x ) \frac{1}{1+\exp(-x)} 1+exp(−x)1​
In x x x.

Here is a simple example :
import torch import torch.nn as nn predicts = torch.tensor([[0.4,0.7,1.2,0.3],
[1.1,0.6,0.9,1.6]]) labels = torch.tensor([[1,0,1,0],[0,1,1,0]],
dtype=torch.float) # adopt BCELoss calculation sigmoid Processed value criterion1 = nn.BCELoss() loss1=
criterion1(torch.sigmoid(predicts), labels) # adopt BCEWithLogitsLoss Directly calculate the input value
criterion2 = nn.BCEWithLogitsLoss() loss2 = criterion2(predicts, labels) #
You'll find out loss1=loss2
BCELoss and BCEWithLogitsLoss Two important parameters are also provided :

* weight: It can be used to control the weight of each sample , It is often used to align the data mask operation ( Set to 0)
* reduction
: Control loss output mode . Set to "sum" Represents the loss sum of the sample ; Set to "mean" Represents the average of the sample losses ; And set to "none" It means to calculate the loss of samples one by one , The relationship between output and input shape equally .
in addition BCEWithLogitsLoss Parameters are also provided pos_weight For setting losses class weight , To alleviate the imbalance of samples .

Technology