---
title: Peak Signal-to-Noise Ratio (PSNR)
description: Compute Peak Signal-to-Noise Ratio (PSNR).
canonical: 'https://lightning.ai/docs/torchmetrics/stable/image/peak_signal_noise_ratio'
---
# Peak Signal-to-Noise Ratio (PSNR) {#peak-signal-to-noise-ratio-psnr}

## Module Interface {#module-interface}

### `class torchmetrics.image.PeakSignalNoiseRatio(data_range, base=10.0, reduction='elementwise_mean', dim=None, **kwargs)` {#torchmetrics.image.PeakSignalNoiseRatio}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/psnr.py#L32-L201)

[Compute Peak Signal-to-Noise Ratio](https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio) (PSNR).

```math
\text{PSNR}(I, J) = 10 * \log_{10} \left(\frac{\max(I)^2}{\text{MSE}(I, J)}\right)
```

Where $`\text{MSE}`$ denotes the [mean-squared-error](https://en.wikipedia.org/wiki/Mean_squared_error) function.

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 of shape `(N,C,H,W)`
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Ground truth values of shape `(N,C,H,W)`
    

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

*   `psnr` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): if `reduction!='none'` returns float scalar tensor with average PSNR value over sample else returns tensor of shape `(N,)` with PSNR values per sample
    

Parameters:

*   **data\_range** (`Union`\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]\]) – the range of the data. If a tuple is provided, then the range is calculated as the difference and input is clamped between the values.
    
*   **base** ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")) – a base of a logarithm to use.
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'elementwise_mean'`, `'sum'`, `'none'`, `None`\]) –
    
    a method to reduce metric score over labels.
    
    *   `'elementwise_mean'`: takes the mean (default)
        
    *   `'sum'`: takes the sum
        
    *   `'none'` or `None`: no reduction will be applied
        
    
*   **dim** (`Union`\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)"), [`...`](https://docs.python.org/3/builtins/constants.html#Ellipsis "(in Python v3.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Dimensions to reduce PSNR scores over, provided as either an integer or a list of integers. Default is None meaning scores will be reduced across all dimensions and all batches.
    
*   **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.image import PeakSignalNoiseRatio
>>> psnr = PeakSignalNoiseRatio(data_range=3.0)
>>> preds = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
>>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]])
>>> psnr(preds, target)
tensor(2.5527)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/psnr.py#L157-L201)

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
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.image import PeakSignalNoiseRatio
>>> metric = PeakSignalNoiseRatio(data_range=1.0)
>>> preds = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
>>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]])
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/peak\_signal\_noise\_ratio-1.png](/docs/torchmetrics/1.9.0/static/_images/peak_signal_noise_ratio-1.png)](/docs/torchmetrics/1.9.0/static/_images/peak_signal_noise_ratio-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.image import PeakSignalNoiseRatio
>>> metric = PeakSignalNoiseRatio(data_range=1.0)
>>> preds = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
>>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]])
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/peak\_signal\_noise\_ratio-2.png](/docs/torchmetrics/1.9.0/static/_images/peak_signal_noise_ratio-2.png)](/docs/torchmetrics/1.9.0/static/_images/peak_signal_noise_ratio-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.image.peak_signal_noise_ratio(preds, target, data_range, base=10.0, reduction='elementwise_mean', dim=None)` {#torchmetrics.functional.image.peak_signal_noise_ratio}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/image/psnr.py#L95-L147)

Compute the peak signal-to-noise ratio.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – estimated signal
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – groun truth signal
    
*   **data\_range** (`Union`\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]\]) – the range of the data. If a tuple is provided then the range is calculated as the difference and input is clamped between the values.
    
*   **base** ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")) – a base of a logarithm to use
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'elementwise_mean'`, `'sum'`, `'none'`, `None`\]) –
    
    a method to reduce metric score over labels.
    
    *   `'elementwise_mean'`: takes the mean (default)
        
    *   `'sum'`: takes the sum
        
    *   `'none'` or None\`\`: no reduction will be applied
        
    
*   **dim** (`Union`\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)"), [`...`](https://docs.python.org/3/builtins/constants.html#Ellipsis "(in Python v3.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Dimensions to reduce PSNR scores over provided as either an integer or a list of integers. Default is None meaning scores will be reduced across all dimensions.
    

Return type:

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

Returns:

Tensor with PSNR score

Example

```python
>>> from torchmetrics.functional.image import peak_signal_noise_ratio
>>> pred = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
>>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]])
>>> peak_signal_noise_ratio(pred, target, data_range=3.0)
tensor(2.5527)
```

Attention

Half precision is only support on GPU for this metric.

---

**Navigation**

- Parent: [Image](/docs/torchmetrics/1.9.0/image.md)
