How to Measure the Binary Cross Entropy Between the Target and the Input Probabilities in PyTorch? Last Updated : 02 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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. Python # Import required library import torch import torch.nn as nn # define input and target tensor input_tens = torch.tensor( [0.4498, 0.9845, 0.4576, 0.3494, 0.2434], requires_grad=True) target_tens = torch.tensor([0.2345, 0.5565, 0.3468, 0.1444, 0.3546]) # display input and target tensor print('\n input tensor: ', input_tens) print('\n target tensor: ', target_tens) # define criterion to measure binary # cross entropy bce_loss = nn.BCELoss() # compute the binary cross entropy output = bce_loss(input_tens, target_tens) output.backward() # display result print('\n Binary Cross Entropy Loss: ', output) Output:  Example 2: In this example, we measure the Binary Cross Entropy between the target and the input probabilities of the 2D tensor. Python # Import required library import torch import torch.nn as nn # define input and target tensor input_tens = torch.tensor([[0.4576, 0.6496, 0.6783], [0.4895, 0.9454, 0.5443], [0.9491, 0.3825, 0.7235]], requires_grad=True) target_tens = torch.tensor([[0.2432, 0.1579, 0.0325], [0.3464, 0.2442, 0.3847], [0.4528, 0.0876, 0.0499], ]) # display input and target tensor print('\n input tensor: \n', input_tens) print('\n target tensor: \n', target_tens) # define criterion to measure binary cross entropy bce_loss = nn.BCELoss() # compute the binary cross entropy output = bce_loss(input_tens, target_tens) output.backward() # display result print('\n Binary Cross Entropy Loss: \n', output) Output:  Comment More infoAdvertise with us Next Article How to Measure the Binary Cross Entropy Between the Target and the Input Probabilities in PyTorch? mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to compute element-wise entropy of an input tensor in PyTorch In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a 2 min read How to measure the mean absolute error (MAE) in PyTorch? In this article, we are going to see how to measure the Mean Absolute Error (MAE) in PyTorch. \text{MAE} = \sum\limits_{i = 1}^n {\left| {{y_i} - \widehat {{y_i}}} \right|} The Mean absolute error (MAE) is computed as the mean of the sum of absolute differences between the input and target values. T 3 min read How to calculate the F1 score and other custom metrics in PyTorch? Evaluating deep learning models goes beyond just training them; it means rigorously checking their performance to ensure they're accurate, reliable, and efficient for real-world use. This evaluation is critical because it tells us how well a model has learned and how effective it might be in real-li 7 min read How to Estimate the Gradient of a Function in One or More Dimensions in PyTorch? In this article, we are going to see how to estimate the gradient of a function in one or more dimensions in PyTorch. torch.gradient() function torch.gradient() method estimates the gradient of a function in one or more dimensions using the second-order accurate central differences method, and the 2 min read Apply torch.inverse() Function of PyTorch to Every Sample in the Batch PyTorch is a deep learning framework that provides a variety of functions to perform different operations on tensors. One such function is torch.inverse(), which can be used to compute the inverse of a square matrix. Sometimes we may have a batch of matrices, where each matrix represents some data t 3 min read Like