class

SoftMarginLoss

extendsModule
SoftMarginLoss(reduction: str = 'mean')
source

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\}:

(x,y)=log ⁣(1+eyx)\ell(x, y) = \log\!\bigl(1 + e^{-y \cdot x}\bigr)

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

reductionstr
The reduction mode.

Notes

  • Input x : ()(*) — real-valued scores.
  • Target y : ()(*) — binary labels in {1,+1}\{-1, +1\}.
  • Output : scalar for 'mean' / 'sum'; ()(*) for 'none'.
  • As x+x \to +\infty with y=+1y = +1 the loss approaches zero; as xx \to -\infty it grows linearly, which provides outlier robustness compared to cross-entropy.
  • This loss is essentially BCEWithLogitsLoss re-labelled for {1,+1}\{-1, +1\} 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')
source

Initialise the SoftMarginLoss module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor, target: Tensor)
source

Compute the loss between predictions and targets.

Parameters

xTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.