class
BCEWithLogitsLoss
extends
ModuleBCEWithLogitsLoss(weight: Tensor | None = None, reduction: str = 'mean', pos_weight: Tensor | None = None)Binary cross-entropy loss that accepts raw logits.
Combines a Sigmoid activation with a binary cross-entropy loss in
a single numerically stable expression. Using the identity:
the per-element loss is computed as:
When a positive-class weight is supplied (pos_weight),
the loss becomes:
where is the sigmoid function. A value of `pos_weight
1` up-weights the positive class, useful when positives are rare.
Parameters
weightTensor= NoneElement-wise weight tensor broadcast over input and target.
reductionstr= 'mean''none' | 'mean' (default) | 'sum'.pos_weightTensor= NoneWeight for the positive class, shape
(C,) or scalar.
Provides class-level rebalancing independent of weight.Attributes
weightTensor or NoneElement-wise weight.
reductionstrThe reduction mode.
pos_weightTensor or NonePositive-class weight.
Notes
- Input
x: — raw logits (any real value). - Target
y: — binary labels in . - Output : scalar for
'mean'/'sum'; for'none'.
- Numerically superior to
BCELoss(Sigmoid(x), y)because the log-domain computation avoids squashing gradients to zero near saturation. pos_weightvalues significantly larger than 1 can destabilise training; values in[1, 10]are typically safe.
Examples
Raw logits, no weighting:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.BCEWithLogitsLoss()
>>> logits = lucid.tensor([ 2.0, -1.0, 0.5, -3.0])
>>> targets = lucid.tensor([ 1.0, 0.0, 1.0, 0.0])
>>> loss = criterion(logits, targets)
Up-weighting the positive class by a factor of 10:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.BCEWithLogitsLoss(
... pos_weight=lucid.tensor([10.0])
... )
>>> logits = lucid.tensor([0.2, -0.8, 1.5])
>>> targets = lucid.tensor([1.0, 0.0, 1.0])
>>> loss = criterion(logits, targets)Methods (3)
dunder
__init__
→None__init__(weight: Tensor | None = None, reduction: str = 'mean', pos_weight: Tensor | None = None)Initialise the BCEWithLogitsLoss module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor, target: Tensor)Compute the loss between predictions and targets.
Parameters
xTensorInput tensor.
targetTensorInput tensor.
Returns
TensorScalar loss (or unreduced tensor depending on reduction).
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.