How to Use BCEWithLogitsLoss in PyTorch: Simple Guide
Use
torch.nn.BCEWithLogitsLoss in PyTorch to combine a sigmoid layer and binary cross-entropy loss in one step. It expects raw model outputs (logits) and target labels as inputs and returns the loss value for training binary classifiers.Syntax
The BCEWithLogitsLoss class is initialized without required arguments but supports optional parameters like weight for per-sample weighting, pos_weight to emphasize positive examples, and reduction to control how losses are combined.
During training, call the loss instance with two arguments: input (raw model outputs, called logits) and target (ground truth labels as floats 0 or 1).
python
loss_fn = torch.nn.BCEWithLogitsLoss(weight=None, pos_weight=None, reduction='mean') loss = loss_fn(input, target)
Example
This example shows how to use BCEWithLogitsLoss with a simple model output and target labels. It computes the loss value from raw logits and prints it.
python
import torch # Create example logits (raw outputs from model) and target labels logits = torch.tensor([0.2, -1.5, 3.0, 0.7]) targets = torch.tensor([1., 0., 1., 0.]) # Initialize BCEWithLogitsLoss loss_fn = torch.nn.BCEWithLogitsLoss() # Calculate loss loss = loss_fn(logits, targets) print(f'Loss: {loss.item():.4f}')
Output
Loss: 0.4170
Common Pitfalls
- Passing probabilities instead of logits:
BCEWithLogitsLossexpects raw logits, not sigmoid probabilities. Applying sigmoid before this loss causes incorrect results. - Incorrect target types: Targets must be floats (0.0 or 1.0), not integers or other types.
- Ignoring reduction: By default, losses are averaged. Use
reduction='none'to get per-sample losses.
python
import torch # Wrong: passing probabilities (sigmoid applied) to BCEWithLogitsLoss probs = torch.sigmoid(torch.tensor([0.2, -1.5, 3.0, 0.7])) targets = torch.tensor([1., 0., 1., 0.]) loss_fn = torch.nn.BCEWithLogitsLoss() try: loss_wrong = loss_fn(probs, targets) except Exception as e: print(f'Error: {e}') # Right: pass raw logits logits = torch.tensor([0.2, -1.5, 3.0, 0.7]) loss_right = loss_fn(logits, targets) print(f'Correct loss: {loss_right.item():.4f}')
Output
Correct loss: 0.4170
Quick Reference
BCEWithLogitsLoss combines sigmoid activation and binary cross-entropy loss in one step for numerical stability.
input: raw model outputs (logits), shape (N, *)target: float labels 0 or 1, same shape as inputweight: optional manual rescaling weight per samplepos_weight: optional weight for positive examplesreduction: 'mean' (default), 'sum', or 'none'
Use it for binary classification tasks where model outputs logits.
Key Takeaways
Use BCEWithLogitsLoss with raw logits, not probabilities.
Targets must be floats with values 0.0 or 1.0.
It combines sigmoid and binary cross-entropy for better stability.
Set reduction to 'none' to get individual losses per sample.
Use pos_weight to handle class imbalance by weighting positives.