Skip to content
class

SoftMarginLoss

extends_Loss
SoftMarginLoss(size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = '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\}:

ℓ(x,y)=log⁡ ⁣(1+e−y⋅x)\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 x→−∞x \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,)

Used by 1

Instance methods

2
fn

extra_repr

→strfrom _Loss
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor, target: Tensor)

Compute the loss between predictions and targets.

Parameters

inputTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).