---
title: Relative Squared Error (RSE)
description: Computes the relative squared error (RSE).
canonical: 'https://lightning.ai/docs/torchmetrics/stable/regression/rse'
---
# Relative Squared Error (RSE) {#relative-squared-error-rse}

## Module Interface {#module-interface}

### `class torchmetrics.RelativeSquaredError(num_outputs=1, squared=True, **kwargs)` {#torchmetrics.RelativeSquaredError}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/rse.py#L30-L145)

Computes the relative squared error (RSE).

```math
\text{RSE} = \frac{\sum_i^N(y_i - \hat{y_i})^2}{\sum_i^N(y_i - \overline{y})^2}
```

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

If num\_outputs > 1, the returned value is averaged over all the outputs.

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.15)")): Predictions from model in float tensor with shape `(N,)` or `(N, M)` (multioutput)
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Ground truth values in float tensor with shape `(N,)` or `(N, M)` (multioutput)
    

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

*   `rse` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): A tensor with the RSE score(s)
    

Parameters:

*   **num\_outputs** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Number of outputs in multioutput setting
    
*   **squared** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – If True returns RSE value, if False returns RRSE value.
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Example

```python
>>> from torchmetrics.regression import RelativeSquaredError
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> relative_squared_error = RelativeSquaredError()
>>> relative_squared_error(preds, target)
tensor(0.0514)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/rse.py#L105-L145)

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.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – 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.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`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/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

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

[![../../\_images/rse-1.png](/docs/torchmetrics/1.9.0/static/_images/rse-1.png)](/docs/torchmetrics/1.9.0/static/_images/rse-1.png)

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

[![../../\_images/rse-2.png](/docs/torchmetrics/1.9.0/static/_images/rse-2.png)](/docs/torchmetrics/1.9.0/static/_images/rse-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.relative_squared_error(preds, target, squared=True)` {#torchmetrics.functional.relative_squared_error}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/regression/rse.py#L54-L80)

Computes the relative squared error (RSE).

```math
\text{RSE} = \frac{\sum_i^N(y_i - \hat{y_i})^2}{\sum_i^N(y_i - \overline{y})^2}
```

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

If preds and targets are 2D tensors, the RSE is averaged over the second dim.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – estimated labels
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – ground truth labels
    
*   **squared** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – returns RRSE value if set to False
    

Return type:

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

Returns:

Tensor with RSE

Example

```python
>>> from torchmetrics.functional.regression import relative_squared_error
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> relative_squared_error(preds, target)
tensor(0.0514)
```

---

**Navigation**

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