---
title: Jensen-Shannon Divergence
description: Compute the Jensen-Shannon divergence.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/regression/js_divergence'
---
# Jensen-Shannon Divergence {#jensen-shannon-divergence}

## Module Interface {#module-interface}

### `class torchmetrics.regression.JensenShannonDivergence(log_prob=False, reduction='mean', **kwargs)` {#torchmetrics.regression.JensenShannonDivergence}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/js_divergence.py#L31-L172)

Compute the [Jensen-Shannon divergence](#jensen-shannon-divergence).

```math
D_{JS}(P||Q) = \frac{1}{2} D_{KL}(P||M) + \frac{1}{2} D_{KL}(Q||M)
```

Where $`P`$ and $`Q`$ are probability distributions where $`P`$ usually represents a distribution over data and $`Q`$ is often a prior or approximation of $`P`$. $`D_{KL}`$ is the [KL divergence](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence) and $`M`$ is the average of the two distributions. It should be noted that the Jensen-Shannon divergence is a symmetrical metric i.e. $`D_{JS}(P||Q) = D_{JS}(Q||P)`$.

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

*   `p` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): a data distribution with shape `(N, d)`
    
*   `q` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): prior or approximate distribution with shape `(N, d)`
    

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

*   `js_divergence` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): A tensor with the Jensen-Shannon divergence
    

Parameters:

*   **log\_prob** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – bool indicating if input is log-probabilities or probabilities. If given as probabilities, will normalize to make sure the distributes sum to 1.
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'mean'`, `'sum'`, `'none'`, `None`\]) –
    
    Determines how to reduce over the `N`/batch dimension:
    
    *   `'mean'` \[default\]: Averages score across samples
        
    *   `'sum'`: Sum score across samples
        
    *   `'none'` or `None`: Returns score per sample
        
    
*   **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.
    

Raises:

*   [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – If `log_prob` is not an `bool`.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `reduction` is not one of `'mean'`, `'sum'`, `'none'` or `None`.
    

Attention

Half precision is only support on GPU for this metric.

Example

```python
>>> from torch import tensor
>>> from torchmetrics.regression import JensenShannonDivergence
>>> p = tensor([[0.1, 0.9], [0.2, 0.8], [0.3, 0.7]])
>>> q = tensor([[0.3, 0.7], [0.4, 0.6], [0.5, 0.5]])
>>> js_div = JensenShannonDivergence()
>>> js_div(p, q)
tensor(0.0259)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/js_divergence.py#L132-L172)

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/typing.html#typing.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 JensenShannonDivergence
>>> metric = JensenShannonDivergence()
>>> metric.update(randn(10,3).softmax(dim=-1), randn(10,3).softmax(dim=-1))
>>> fig_, ax_ = metric.plot()
```

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

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

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

## Functional Interface {#functional-interface}

### `torchmetrics.functional.regression.jensen_shannon_divergence(p, q, log_prob=False, reduction='mean')` {#torchmetrics.functional.regression.jensen_shannon_divergence}

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

Compute [Jensen-Shannon divergence](#jensen-shannon-divergence).

```math
D_{JS}(P||Q) = \frac{1}{2} D_{KL}(P||M) + \frac{1}{2} D_{KL}(Q||M)
```

Where $`P`$ and $`Q`$ are probability distributions where $`P`$ usually represents a distribution over data and $`Q`$ is often a prior or approximation of $`P`$. $`D_{KL}`$ is the [KL divergence](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence) and $`M`$ is the average of the two distributions. It should be noted that the Jensen-Shannon divergence is a symmetrical metric i.e. $`D_{JS}(P||Q) = D_{JS}(Q||P)`$.

Parameters:

*   **p** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – data distribution with shape `[N, d]`
    
*   **q** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – prior or approximate distribution with shape `[N, d]`
    
*   **log\_prob** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – bool indicating if input is log-probabilities or probabilities. If given as probabilities, will normalize to make sure the distributes sum to 1
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'mean'`, `'sum'`, `'none'`, `None`\]) –
    
    Determines how to reduce over the `N`/batch dimension:
    
    *   `'mean'` \[default\]: Averages score across samples
        
    *   `'sum'`: Sum score across samples
        
    *   `'none'` or `None`: Returns score per sample
        
    

Return type:

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

Example

```python
>>> from torch import tensor
>>> p = tensor([[0.36, 0.48, 0.16]])
>>> q = tensor([[1/3, 1/3, 1/3]])
>>> jensen_shannon_divergence(p, q)
tensor(0.0225)
```

---

**Navigation**

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