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 either mean, sum, or none. The default mean corresponds to our equation 6.15 in the previous lesson. As expected, sum will 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 is none. 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 ...

Access this course and 1400+ top-rated courses and projects.