Log-Cosh Loss: Formula, Calculator and Worked Example

Log cosh loss is a regression loss that behaves like half the squared error when a prediction is close and like the absolute error when it is far off. The formula is \( L = \frac{1}{n}\sum_{i=1}^{n} \ln\!\big(\cosh(\hat{y}_i – y_i)\big) \). Paste your true values and predictions below to get the loss, the per-point breakdown and a comparison with MSE and MAE.

L = mean of ln(cosh(y-hat − y))
Log-cosh loss—
MSE—
MAE—
Points—
Per-point working
#yy-haterrorln(cosh(error))gradient tanh(error)
Example loaded. Press Calculate log-cosh loss.

What is log cosh loss?

For one prediction, log-cosh takes the error \( e = \hat{y} - y \), passes it through the hyperbolic cosine, \( \cosh(e) = \frac{e^{e} + e^{-e}}{2} \), and then takes the natural logarithm. Averaging over the data set gives the loss used in training:

\[ L = \frac{1}{n}\sum_{i=1}^{n} \ln\!\big(\cosh(\hat{y}_i - y_i)\big) \]

It is zero when the prediction is exact, it is symmetric (an error of +2 costs the same as an error of −2), and it is never negative. What makes it useful is how it grows. Two approximations describe the whole curve:

  • Small errors: \( \ln(\cosh(e)) \approx \frac{e^2}{2} \). Near zero it is half the squared error, so it is smooth and gives small gradients for small mistakes, like mean squared error.
  • Large errors: \( \ln(\cosh(e)) \approx |e| - \ln 2 \). Far from zero it is a straight line with slope 1, like mean absolute error shifted down by about 0.693.

So one very wrong prediction adds roughly its absolute size to the total, not its square. That is why the loss is described as robust to outliers.

log cosh loss chart

Worked example by hand

Take four true values 3, 5, 2.5, 7 and predictions 2.5, 5, 4, 8 (the example loaded in the calculator). The errors are −0.5, 0, 1.5 and 1.

Error ecosh(e)ln(cosh(e))For comparison: e²/2
−0.51.1276260.1201150.125
0100
1.52.3524100.8554401.125
11.5430810.4337810.5

The four values add up to 1.409336, and dividing by 4 gives a loss of 0.352334. On the same data the MSE is 0.875 and the MAE is 0.75.

Now press Add an outlier. That adds a fifth point with true value 10 and prediction 20, an error of 10. Its contribution is \( \ln(\cosh(10)) = 9.306853 \), almost exactly \( 10 - \ln 2 \). The loss rises to 2.143238. The MSE on the same five points jumps from 0.875 to 20.7, because that one point contributes 100 to the sum of squares. The outlier moved MSE by a factor of about 24 and log-cosh by a factor of about 6.

The gradient: why training stays stable

The derivative of \( \ln(\cosh(e)) \) with respect to the prediction is \( \tanh(e) \). That single fact explains the behaviour during gradient descent:

  • \( \tanh(e) \) always lies between −1 and 1, so no single example can produce a huge gradient. With MSE the gradient is \( 2e \), which grows without limit.
  • Near zero \( \tanh(e) \approx e \), so the gradient shrinks smoothly as the fit improves. MAE keeps a gradient of ±1 right up to the answer, which makes the last steps jumpy unless the learning rate is lowered.
  • The second derivative is \( 1 - \tanh^2(e) \), which exists everywhere. The loss is twice differentiable, with no kink at zero and no switch point.

The per-point table in the calculator shows the gradient next to each loss value. For the error of 1.5 it is 0.905148; for the outlier with error 10 it is 1.000000 to six decimals.

Log-cosh vs Huber, MSE and MAE

LossSmall errorsLarge errorsSetting to tuneSmoothness
MSEquadraticquadraticnonesmooth everywhere
MAElinearlinearnonekink at zero
Huberquadraticlineardelta, the switch pointonce differentiable
Log-coshquadraticlinearnonetwice differentiable

Log-cosh and Huber have the same shape in outline. The difference is control. Huber lets you choose where "small" ends by setting delta; log-cosh fixes the transition at an error of roughly 1 in the units of the target. If your target is house prices in dollars, almost every error is far larger than 1 and the loss behaves like MAE throughout. If the target is standardised, the transition falls in a sensible place. Scaling the target is therefore the practical way to adjust it.

When to use it, and when not to

It is a reasonable choice when the data contain a few bad labels or extreme values that you do not want to dominate training, and you would like a smooth loss without choosing a delta. It is available directly as LogCosh in Keras, and it is easy to write in any framework.

It is a poor choice in three situations. First, when large errors really are much worse than small ones for your application: use MSE or RMSE, which penalise them more. Second, when you need to decide exactly where the linear region begins: use Huber. Third, with methods that divide by the second derivative, such as Newton-style gradient boosting: for large errors \( 1 - \tanh^2(e) \) is close to zero, so those steps become unstable unless the library guards against it.

Common mistakes

  • Computing cosh directly. \( \cosh(e) \) overflows a 64-bit float once \( |e| \) passes about 710. The stable form is \( |e| + \ln(1 + e^{-2|e|}) - \ln 2 \), which is what this calculator and the major libraries use.
  • Comparing it with MSE on the same scale. The numbers are not comparable. Near zero log-cosh is about half the squared error, so a model with MSE 0.02 has a log-cosh loss near 0.01, not 0.02.
  • Forgetting the target's units. Because the bend sits near an error of 1, changing metres to millimetres changes which regime you are in.
  • Reporting it as the final metric. It is a training loss. Report MAE or RMSE as well, since readers can interpret those in the units of the target. See the full list of loss functions for where each one fits.

Frequently asked questions

Is log cosh loss the same as Huber loss?

No. Both are quadratic for small errors and linear for large ones, but Huber switches at a point you choose (delta) and is only once differentiable there. Log-cosh has no setting and is smooth to the second derivative.

What is the derivative of log-cosh loss?

For one example it is \( \tanh(\hat{y} - y) \), which stays between −1 and 1. The second derivative is \( 1 - \tanh^2(\hat{y} - y) \).

Can the loss be negative?

No. \( \cosh(e) \ge 1 \) for every real number, so its logarithm is at least zero, and it equals zero only when the prediction is exact.

Why subtract ln 2 in the large-error approximation?

For large \( |e| \), \( \cosh(e) \approx \frac{e^{|e|}}{2} \). Taking the logarithm gives \( |e| - \ln 2 \), so the straight part of the curve sits about 0.693 below the absolute error.

Does it work for classification?

It is designed for regression, where the output is a number. For class probabilities use cross-entropy loss instead.

Reference: the Keras documentation for tf.keras.losses.LogCosh gives the same definition and the same small-error and large-error approximations.

Scroll to Top