log_cosh#
- class braintools.metric.log_cosh(predictions, targets=None, axis=None, reduction='none')#
Calculate the log-cosh loss for a set of predictions.
log(cosh(x))is approximately(x ** 2) / 2for smallxandabs(x) - log(2)for largex. It is a twice differentiable alternative to the Huber loss.- Parameters:
predictions (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – A vector of arbitrary shape[...].targets (
Array|ndarray|bool|number|bool|int|float|complex|Quantity|None) – A vector with a shape broadcastable topredictions. If not provided then it is assumed to be a vector of zeros. Inputs must be dimensionless (log(cosh(x))is only defined for a dimensionless argument).axis (
int|tuple[int,...] |None) – Axis or axes along which to reduce whenreductionis'mean'or'sum'. If None, reduction (if any) is over all elements.reduction (
str) –Reduction operation to apply:
'none': return element-wise losses with the same shape aspredictions(backward-compatible default),'mean': return the mean of the losses,'sum': return the sum of the losses.
- Returns:
The log-cosh loss with the same shape as
predictionswhenreduction='none'; otherwise the reduced value.- Return type:
Array|ndarray|bool|number|bool|int|float|complex|Quantity
Notes
The loss is computed with the numerically stable, gradient-safe identity
\[\log(\cosh(x)) = |x| + \operatorname{softplus}(-2 |x|) - \log 2,\]which avoids overflow in
cosh/expand yields finite gradients even for very large|x|(e.g.|x| = 500).References
Examples
>>> import jax.numpy as jnp >>> import braintools >>> predictions = jnp.array([0.0, 1.0, -1.0]) >>> braintools.metric.log_cosh(predictions) Array([0. , 0.4337808, 0.4337808], dtype=float32)