---
title: Log Cosh Error
description: Compute the LogCosh Error.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/regression/log_cosh_error'
---
# Log Cosh Error {#log-cosh-error}

## Module Interface {#module-interface}

### `class torchmetrics.LogCoshError(num_outputs=1, **kwargs)` {#torchmetrics.LogCoshError}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.8.2/src/torchmetrics/regression/log_cosh.py#L29-L142)

Compute the [LogCosh Error](https://arxiv.org/abs/2101.10427).

```math
\text{LogCoshError} = \log\left(\frac{\exp(\hat{y} - y) + \exp(\hat{y - y})}{2}\right)
```

Where $`y`$ is a tensor of target values, and $`\hat{y}`$ is a tensor of predictions.

As input to `forward` and `update` the metric accepts the following input:

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")): Estimated labels with shape `(batch_size,)` or `(batch_size, num_outputs)`
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")): Ground truth labels with shape `(batch_size,)` or `(batch_size, num_outputs)`
    

As output of `forward` and `compute` the metric returns the following output:

*   `log_cosh_error` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")): A tensor with the log cosh error
    

Parameters:

*   **num\_outputs** ([`int`](https://docs.python.org/3/library/functions.html#int "(in Python v3.14)")) – Number of outputs in multioutput setting
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.14)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.8.2/user-guide/overview.md#metric-kwargs) for more info.
    

Example (single output regression)::

```python
>>> from torchmetrics.regression import LogCoshError
>>> preds = torch.tensor([3.0, 5.0, 2.5, 7.0])
>>> target = torch.tensor([2.5, 5.0, 4.0, 8.0])
>>> log_cosh_error = LogCoshError()
>>> log_cosh_error(preds, target)
tensor(0.3523)
```

Example (multi output regression)::

```python
>>> from torchmetrics.regression import LogCoshError
>>> preds = torch.tensor([[3.0, 5.0, 1.2], [-2.1, 2.5, 7.0]])
>>> target = torch.tensor([[2.5, 5.0, 1.3], [0.3, 4.0, 8.0]])
>>> log_cosh_error = LogCoshError(num_outputs=3)
>>> log_cosh_error(preds, target)
tensor([0.9176, 0.4277, 0.2194])
```

#### `plot(val=None, ax=None)` {#torchmetrics.LogCoshError.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.8.2/src/torchmetrics/regression/log_cosh.py#L102-L142)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.14)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")\], [`None`](https://docs.python.org/3/library/constants.html#None "(in Python v3.14)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.14)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.1)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.14)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.1)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.1)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/library/exceptions.html#ModuleNotFoundError "(in Python v3.14)") – If matplotlib is not installed

```python
>>> from torch import randn
>>> # Example plotting a single value
>>> from torchmetrics.regression import LogCoshError
>>> metric = LogCoshError()
>>> metric.update(randn(10,), randn(10,))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/log\_cosh\_error-1.png](/docs/torchmetrics/1.8.2/static/_images/log_cosh_error-1.png)](/docs/torchmetrics/1.8.2/static/_images/log_cosh_error-1.png)

```python
>>> from torch import randn
>>> # Example plotting multiple values
>>> from torchmetrics.regression import LogCoshError
>>> metric = LogCoshError()
>>> values = []
>>> for _ in range(10):
...     values.append(metric(randn(10,), randn(10,)))
>>> fig, ax = metric.plot(values)
```

[![../../\_images/log\_cosh\_error-2.png](/docs/torchmetrics/1.8.2/static/_images/log_cosh_error-2.png)](/docs/torchmetrics/1.8.2/static/_images/log_cosh_error-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.log_cosh_error(preds, target)` {#torchmetrics.functional.log_cosh_error}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.8.2/src/torchmetrics/functional/regression/log_cosh.py#L63-L95)

Compute the [LogCosh Error](https://arxiv.org/abs/2101.10427).

```math
\text{LogCoshError} = \log\left(\frac{\exp(\hat{y} - y) + \exp(\hat{y - y})}{2}\right)
```

Where $`y`$ is a tensor of target values, and $`\hat{y}`$ is a tensor of predictions.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")) – estimated labels with shape `(batch_size,)` or (batch\_size, num\_outputs)\`
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")) – ground truth labels with shape `(batch_size,)` or (batch\_size, num\_outputs)\`
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")

Returns:

Tensor with LogCosh error

Example (single output regression)::

```python
>>> from torchmetrics.functional.regression import log_cosh_error
>>> preds = torch.tensor([3.0, 5.0, 2.5, 7.0])
>>> target = torch.tensor([2.5, 5.0, 4.0, 8.0])
>>> log_cosh_error(preds, target)
tensor(0.3523)
```

Example (multi output regression)::

```python
>>> from torchmetrics.functional.regression import log_cosh_error
>>> preds = torch.tensor([[3.0, 5.0, 1.2], [-2.1, 2.5, 7.0]])
>>> target = torch.tensor([[2.5, 5.0, 1.3], [0.3, 4.0, 8.0]])
>>> log_cosh_error(preds, target)
tensor([0.9176, 0.4277, 0.2194])
```

---

**Navigation**

- Parent: [Regression](/docs/torchmetrics/1.8.2/regression.md)
