Skip to content
fn

soft_margin_loss

→Tensor
soft_margin_loss(input: Tensor, target: Tensor, size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean')

Logistic (softplus) loss for binary classification with ±1 labels.

A "soft" variant of the binary hinge loss: instead of the piecewise-linear max⁡(0,1−y x)\max(0, 1 - y\,x), it uses the smooth surrogate log⁡(1+e−y x)\log(1 + e^{-y\,x}) — which is the negative-log-likelihood of a logistic model with labels in {−1,+1}\{-1, +1\}. Equivalent to binary_cross_entropy_with_logits with the {0, 1} labels re-coded as {−1,+1}\{-1, +1\}.

The implementation evaluates softplus(−y x)\mathrm{softplus}(-y\,x), which is numerically stable for large |x| (no overflow, no log of near-zero values).

Parameters

inputTensor
Raw scores (logits), any shape.
targetTensor
Target tensor of the same shape, or one that broadcasts to it, conventionally holding ±1\pm 1 (any real values are accepted). A target that does not broadcast to input's shape, or would enlarge it, raises ValueError.
reductionstr= 'mean'
"mean" (default), "sum", or "none".
size_averagestr= 'mean'
"mean" (default), "sum", or "none".
reducestr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or full-shape per reduction.

Notes

Per-element loss:

Li=log⁡ ⁣(1+exp⁡(−yi xi)).L_i = \log\!\big(1 + \exp(-y_i\,x_i)\big).

Unlike the (non-smooth) hinge, every sample contributes a non-zero gradient — even correctly classified ones — but the contribution decays exponentially as y xy\,x grows. This softness improves optimisation behaviour with first-order methods at the cost of a slightly less sparse solution.

Examples

>>> import lucid
>>> from lucid.nn.functional import soft_margin_loss
>>> x = lucid.tensor([2.0, -1.0])
>>> y = lucid.tensor([1.0, -1.0])
>>> soft_margin_loss(x, y)
tensor(0.2201)

Used by 2