soft_margin_loss
→Tensorsoft_margin_loss(input: Tensor, target: Tensor, reduction: str = 'mean')Logistic (softplus) loss for binary classification with ±1 labels.
A "soft" variant of the binary hinge loss: instead of the
piecewise-linear , it uses the smooth
surrogate — which is the
negative-log-likelihood of a logistic model with labels in
. Equivalent to
binary_cross_entropy_with_logits with the {0, 1}
labels re-coded as .
The implementation evaluates ,
which is numerically stable for large |x| (no overflow,
no log of near-zero values).
Parameters
inputTensortargetTensorreductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or full-shape per reduction.
Notes
Per-element loss:
Unlike the (non-smooth) hinge, every sample contributes a non-zero gradient — even correctly classified ones — but the contribution decays exponentially as 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...)