class
SoftMarginLoss
extends
ModuleSoftMarginLoss(reduction: str = 'mean')Soft-margin binary hinge loss.
Computes the element-wise two-class logistic loss between the input
x and the binary label y \in \{-1, +1\}:
This is the logistic (log-sigmoid) surrogate for the binary hinge. It is smooth and differentiable everywhere, unlike the hard hinge.
Parameters
reductionstr= 'mean''none' | 'mean' (default) | 'sum'.Attributes
reductionstrThe reduction mode.
Notes
- Input
x: — real-valued scores. - Target
y: — binary labels in . - Output : scalar for
'mean'/'sum'; for'none'.
- As with the loss approaches zero; as it grows linearly, which provides outlier robustness compared to cross-entropy.
- This loss is essentially
BCEWithLogitsLossre-labelled for targets.
Examples
Positive and negative labels:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.SoftMarginLoss()
>>> x = lucid.tensor([ 1.5, -1.0, 0.5, -2.0])
>>> y = lucid.tensor([ 1.0, 1.0, -1.0, -1.0])
>>> loss = criterion(x, y)
Element-wise output:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.SoftMarginLoss(reduction="none")
>>> x = lucid.tensor([0.0, 2.0])
>>> y = lucid.tensor([1.0, -1.0])
>>> loss = criterion(x, y) # shape (2,)Methods (3)
dunder
__init__
→None__init__(reduction: str = 'mean')Initialise the SoftMarginLoss 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.