VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-measure-the-binary-cross-entropy-between-the-target-and-the-input-probabilities-in-pytorch/

⇱ How to Measure the Binary Cross Entropy Between the Target and the Input Probabilities in PyTorch? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Measure the Binary Cross Entropy Between the Target and the Input Probabilities in PyTorch?

Last Updated : 23 Jul, 2025

In this article, we are going to see how to Measure the Binary Cross Entropy between the target and the input probabilities in PyTorch using Python. We can measure this by using the BCELoss() method of torch.nn module.

BCELoss() method

The BCELoss() method measures the Binary Cross Entropy between the target and the input probabilities by creating a criterion. This method is used for measuring the error of reconstruction, an auto-encoder is a good example of it. The input and target must be tensors of any number of dimensions and the target should be between 0 to 1. before moving further let's see the syntax of the given method.

Syntax: torch.nn.BCELoss()

Example 1:

In this example, we measure the Binary Cross Entropy between the target and the input probabilities of the 1D tensor.

Output:

👁 Image
 

Example 2:

In this example, we measure the Binary Cross Entropy between the target and the input probabilities of the 2D tensor.

Output:

👁 Image
 
Comment
Article Tags: