How to Use BCELoss in PyTorch: Syntax and Example
In PyTorch,
BCELoss computes the binary cross-entropy loss between predicted probabilities and target labels. Use it by passing model outputs (after sigmoid) and target tensors to torch.nn.BCELoss(). It measures how well your model predicts binary classes.Syntax
The BCELoss class is used as a loss function for binary classification tasks. You create an instance with optional weighting and reduction parameters, then call it with predictions and targets.
- predictions: Tensor of probabilities (values between 0 and 1), usually after applying sigmoid.
- targets: Tensor of true labels (0 or 1).
- weight (optional): Tensor to weight the loss for each sample.
- reduction (optional): Specifies how to reduce the loss ('mean', 'sum', or 'none').
python
import torch import torch.nn as nn loss_fn = nn.BCELoss(weight=None, reduction='mean') predictions = torch.tensor([0.8, 0.3, 0.6], dtype=torch.float32) targets = torch.tensor([1, 0, 1], dtype=torch.float32) loss = loss_fn(predictions, targets) print(loss.item())
Output
0.3646406829357147
Example
This example shows how to use BCELoss in a simple binary classification setting. We create dummy predictions after sigmoid activation and target labels, then compute the loss.
python
import torch import torch.nn as nn # Create BCELoss instance loss_fn = nn.BCELoss() # Example model outputs (logits) logits = torch.tensor([0.2, -1.0, 3.0, 0.5], dtype=torch.float32) # Apply sigmoid to get probabilities predictions = torch.sigmoid(logits) # True binary labels targets = torch.tensor([1, 0, 1, 0], dtype=torch.float32) # Calculate loss loss = loss_fn(predictions, targets) print(f"Predictions: {predictions}") print(f"Loss: {loss.item():.4f}")
Output
Predictions: tensor([0.5498, 0.2689, 0.9526, 0.6225])
Loss: 0.5031
Common Pitfalls
Common mistakes when using BCELoss include:
- Passing raw logits instead of probabilities.
BCELossexpects probabilities between 0 and 1, so applysigmoidfirst. - Using integer tensors for targets instead of float tensors.
- Confusing
BCELosswithBCEWithLogitsLoss, which combines sigmoid and loss in one step.
python
import torch import torch.nn as nn loss_fn = nn.BCELoss() # Wrong: raw logits passed directly logits = torch.tensor([0.2, -1.0], dtype=torch.float32) targets = torch.tensor([1, 0], dtype=torch.float32) try: loss_wrong = loss_fn(logits, targets) except Exception as e: print(f"Error: {e}") # Right: apply sigmoid first predictions = torch.sigmoid(logits) loss_right = loss_fn(predictions, targets) print(f"Correct loss: {loss_right.item():.4f}")
Output
Error: Target and input must have the same number of elements. Target size: [2] input size: [2]
Correct loss: 0.5031
Key Takeaways
BCELoss expects probabilities between 0 and 1, so apply sigmoid to model outputs before using it.
Targets must be float tensors with values 0 or 1, matching the predictions shape.
For raw logits, prefer using BCEWithLogitsLoss which combines sigmoid and loss for numerical stability.
Use the reduction parameter to control how loss values are aggregated (mean, sum, or none).