fn

soft_margin_loss

Tensor
soft_margin_loss(input: Tensor, target: Tensor, reduction: str = 'mean')
source

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

A "soft" variant of the binary hinge loss: instead of the piecewise-linear max(0,1yx)\max(0, 1 - y\,x), it uses the smooth surrogate log(1+eyx)\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(yx)\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, conventionally holding ±1\pm 1 (any real values are accepted).
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or full-shape per reduction.

Notes

Per-element loss:

Li=log ⁣(1+exp(yixi)).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 yxy\,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.2284...)