...
/Binary Cross-Entropy Loss in PyTorch
Binary Cross-Entropy Loss in PyTorch
Uncover the different ways you can compute the binary cross-entropy loss in PyTorch.
We'll cover the following...
BCELoss
Sure enough, PyTorch implements the binary cross-entropy loss, [nn.BCELoss]. Just like its regression counterpart, MSELoss (introduced in the chapter, A Simple Regression Problem), it is a higher-order function that returns the actual loss function.
The BCELoss higher-order function takes two optional arguments (the others are deprecated, and you can safely ignore them):
-
reduction: It takes eithermean,sum, ornone. The defaultmeancorresponds to our equation 6.15 in the previous lesson. As expected,sumwill return the sum of the errors instead of the average. The last option,none, corresponds to the unreduced form; that is, it returns the full array of errors. -
weight: The default isnone. Meaning, every data point has equal weight. If informed, it needs to be a tensor with a size that equals the number of elements in a mini-batch, representing the weights assigned to each element in the batch. In other words, this argument allows you to ...