---
title: Tweedie Deviance Score
description: Compute the Tweedie Deviance Score.
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/regression/tweedie_deviance_score
---
# Tweedie Deviance Score {#tweedie-deviance-score}

## Module Interface {#module-interface}

### `class torchmetrics.TweedieDevianceScore(power=0.0, **kwargs)` {#torchmetrics.TweedieDevianceScore}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/tweedie_deviance.py#L32-L153)

Compute the [Tweedie Deviance Score](https://en.wikipedia.org/wiki/Tweedie_distribution#The_Tweedie_deviance).

```math
\begin{split}deviance\_score(\hat{y},y) = \begin{cases} (\hat{y} - y)^2, & \text{for }p=0\\ 2 * (y * log(\frac{y}{\hat{y}}) + \hat{y} - y), & \text{for }p=1\\ 2 * (log(\frac{\hat{y}}{y}) + \frac{y}{\hat{y}} - 1), & \text{for }p=2\\ 2 * (\frac{(max(y,0))^{2 - p}}{(1 - p)(2 - p)} - \frac{y(\hat{y})^{1 - p}}{1 - p} + \frac{( \hat{y})^{2 - p}}{2 - p}), & \text{otherwise} \end{cases}\end{split}
```

where $`y`$ is a tensor of targets values, $`\hat{y}`$ is a tensor of predictions, and $`p`$ is the power.

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

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

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

Parameters:

*   **power** ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")) –
    
    *   power < 0 : Extreme stable distribution. (Requires: preds > 0.)
        
    *   power = 0 : Normal distribution. (Requires: targets and preds can be any real numbers.)
        
    *   power = 1 : Poisson distribution. (Requires: targets >= 0 and y\_pred > 0.)
        
    *   1 < p < 2 : Compound Poisson distribution. (Requires: targets >= 0 and preds > 0.)
        
    *   power = 2 : Gamma distribution. (Requires: targets > 0 and preds > 0.)
        
    *   power = 3 : Inverse Gaussian distribution. (Requires: targets > 0 and preds > 0.)
        
    *   otherwise : Positive stable distribution. (Requires: targets > 0 and preds > 0.)
        
    
*   **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 TweedieDevianceScore
>>> targets = torch.tensor([1.0, 2.0, 3.0, 4.0])
>>> preds = torch.tensor([4.0, 3.0, 2.0, 1.0])
>>> deviance_score = TweedieDevianceScore(power=2)
>>> deviance_score(preds, targets)
tensor(1.2083)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/tweedie_deviance.py#L113-L153)

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 TweedieDevianceScore
>>> metric = TweedieDevianceScore()
>>> metric.update(randn(10,), randn(10,))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/tweedie\_deviance\_score-1.png](/docs/torchmetrics/1.9.0/static/_images/tweedie_deviance_score-1.png)](/docs/torchmetrics/1.9.0/static/_images/tweedie_deviance_score-1.png)

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

[![../../\_images/tweedie\_deviance\_score-2.png](/docs/torchmetrics/1.9.0/static/_images/tweedie_deviance_score-2.png)](/docs/torchmetrics/1.9.0/static/_images/tweedie_deviance_score-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.tweedie_deviance_score(preds, targets, power=0.0)` {#torchmetrics.functional.tweedie_deviance_score}

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

Compute the [Tweedie Deviance Score](https://en.wikipedia.org/wiki/Tweedie_distribution#The_Tweedie_deviance).

```math
\begin{split}deviance\_score(\hat{y},y) = \begin{cases} (\hat{y} - y)^2, & \text{for }p=0\\ 2 * (y * log(\frac{y}{\hat{y}}) + \hat{y} - y), & \text{for }p=1\\ 2 * (log(\frac{\hat{y}}{y}) + \frac{y}{\hat{y}} - 1), & \text{for }p=2\\ 2 * (\frac{(max(y,0))^{2 - p}}{(1 - p)(2 - p)} - \frac{y(\hat{y})^{1 - p}}{1 - p} + \frac{( \hat{y})^{2 - p}}{2 - p}), & \text{otherwise} \end{cases}\end{split}
```

where $`y`$ is a tensor of targets values, $`\hat{y}`$ is a tensor of predictions, and $`p`$ is the power.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – Predicted tensor with shape `(N,...)`
    
*   **targets** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – Ground truth tensor with shape `(N,...)`
    
*   **power** ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")) –
    
    *   power < 0 : Extreme stable distribution. (Requires: preds > 0.)
        
    *   power = 0 : Normal distribution. (Requires: targets and preds can be any real numbers.)
        
    *   power = 1 : Poisson distribution. (Requires: targets >= 0 and y\_pred > 0.)
        
    *   1 < p < 2 : Compound Poisson distribution. (Requires: targets >= 0 and preds > 0.)
        
    *   power = 2 : Gamma distribution. (Requires: targets > 0 and preds > 0.)
        
    *   power = 3 : Inverse Gaussian distribution. (Requires: targets > 0 and preds > 0.)
        
    *   otherwise : Positive stable distribution. (Requires: targets > 0 and preds > 0.)
        
    

Return type:

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

Example

```python
>>> from torchmetrics.functional.regression import tweedie_deviance_score
>>> targets = torch.tensor([1.0, 2.0, 3.0, 4.0])
>>> preds = torch.tensor([4.0, 3.0, 2.0, 1.0])
>>> tweedie_deviance_score(preds, targets, power=2)
tensor(1.2083)
```

---

**Navigation**

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