---
title: Permutation Invariant Training (PIT)
description: Calculate Permutation invariant training (PIT).
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/audio/permutation_invariant_training
---
# Permutation Invariant Training (PIT) {#permutation-invariant-training-pit}

## Module Interface {#module-interface}

### `class torchmetrics.audio.PermutationInvariantTraining(metric_func, mode='speaker-wise', eval_func='max', **kwargs)` {#torchmetrics.audio.PermutationInvariantTraining}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/audio/pit.py#L31-L164)

Calculate [Permutation invariant training](https://arxiv.org/abs/1607.00325) (PIT).

This metric can evaluate models for speaker independent multi-talker speech separation in a permutation invariant way.

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

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

*   `pesq` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float scalar tensor with average PESQ value over samples
    

Parameters:

*   **metric\_func** ([`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")) –
    
    a metric function accept a batch of target and estimate.
    
    if mode\`==’speaker-wise’, then \`\`metric\_func(preds\[:, i, …\], target\[:, j, …\])\` is called and expected to return a batch of metric tensors `(batch,)`;
    
    if mode\`==’permutation-wise’, then \`\`metric\_func(preds\[:, p, …\], target\[:, :, …\])\` is called, where p is one possible permutation, e.g. \[0,1\] or \[1,0\] for 2-speaker case, and expected to return a batch of metric tensors `(batch,)`;
    
*   **mode** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'speaker-wise'`, `'permutation-wise'`\]) – can be ‘speaker-wise’ or ‘permutation-wise’.
    
*   **eval\_func** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'max'`, `'min'`\]) – the function to find the best permutation, can be ‘min’ or ‘max’, i.e. the smaller the better or the larger the better.
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments for either the `metric_func` or distributed communication, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Example

```python
>>> from torch import randn
>>> from torchmetrics.audio import PermutationInvariantTraining
>>> from torchmetrics.functional.audio import scale_invariant_signal_noise_ratio
>>> preds = randn(3, 2, 5) # [batch, spk, time]
>>> target = randn(3, 2, 5) # [batch, spk, time]
>>> pit = PermutationInvariantTraining(scale_invariant_signal_noise_ratio,
...     mode="speaker-wise", eval_func="max")
>>> pit(preds, target)
tensor(-2.1065)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/audio/pit.py#L118-L164)

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.audio import PermutationInvariantTraining
>>> from torchmetrics.functional.audio import scale_invariant_signal_noise_ratio
>>> preds = torch.randn(3, 2, 5) # [batch, spk, time]
>>> target = torch.randn(3, 2, 5) # [batch, spk, time]
>>> metric = PermutationInvariantTraining(scale_invariant_signal_noise_ratio,
...     mode="speaker-wise", eval_func="max")
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/permutation\_invariant\_training-1.png](/docs/torchmetrics/1.9.0/static/_images/permutation_invariant_training-1.png)](/docs/torchmetrics/1.9.0/static/_images/permutation_invariant_training-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.audio import PermutationInvariantTraining
>>> from torchmetrics.functional.audio import scale_invariant_signal_noise_ratio
>>> preds = torch.randn(3, 2, 5) # [batch, spk, time]
>>> target = torch.randn(3, 2, 5) # [batch, spk, time]
>>> metric = PermutationInvariantTraining(scale_invariant_signal_noise_ratio,
...     mode="speaker-wise", eval_func="max")
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/permutation\_invariant\_training-2.png](/docs/torchmetrics/1.9.0/static/_images/permutation_invariant_training-2.png)](/docs/torchmetrics/1.9.0/static/_images/permutation_invariant_training-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.audio.permutation_invariant_training(preds, target, metric_func, mode='speaker-wise', eval_func='max', **kwargs)` {#torchmetrics.functional.audio.permutation_invariant_training}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/audio/pit.py#L107-L213)

Calculate [Permutation invariant training](https://arxiv.org/abs/1607.00325) (PIT).

This metric can evaluate models for speaker independent multi-talker speech separation in a permutation invariant way.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – float tensor with shape `(batch_size,num_speakers,...)`
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – float tensor with shape `(batch_size,num_speakers,...)`
    
*   **metric\_func** ([`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")) –
    
    a metric function accept a batch of target and estimate. if mode\`==’speaker-wise’, then \`\`metric\_func(preds\[:, i, …\], target\[:, j, …\])\` is called and expected to return a batch of metric tensors `(batch,)`;
    
    if mode\`==’permutation-wise’, then \`\`metric\_func(preds\[:, p, …\], target\[:, :, …\])\` is called, where p is one possible permutation, e.g. \[0,1\] or \[1,0\] for 2-speaker case, and expected to return a batch of metric tensors `(batch,)`;
    
*   **mode** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'speaker-wise'`, `'permutation-wise'`\]) – can be ‘speaker-wise’ or ‘permutation-wise’.
    
*   **eval\_func** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'max'`, `'min'`\]) – the function to find the best permutation, can be `'min'` or `'max'`, i.e. the smaller the better or the larger the better.
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional args for metric\_func
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]

Returns:

Tuple of two float tensors. First tensor with shape `(batch,)` contains the best metric value for each sample and second tensor with shape `(batch,)` contains the best permutation.

Example

```python
>>> from torchmetrics.functional.audio import scale_invariant_signal_distortion_ratio
>>> # [batch, spk, time]
>>> preds = torch.tensor([[[-0.0579,  0.3560, -0.9604], [-0.1719,  0.3205,  0.2951]]])
>>> target = torch.tensor([[[ 1.0958, -0.1648,  0.5228], [-0.4100,  1.1942, -0.5103]]])
>>> best_metric, best_perm = permutation_invariant_training(
...     preds, target, scale_invariant_signal_distortion_ratio,
...     mode="speaker-wise", eval_func="max")
>>> best_metric
tensor([-5.1091])
>>> best_perm
tensor([[0, 1]])
>>> pit_permutate(preds, best_perm)
tensor([[[-0.0579,  0.3560, -0.9604],
         [-0.1719,  0.3205,  0.2951]]])
```

---

**Navigation**

- Parent: [Audio](/docs/torchmetrics/1.9.0/audio.md)
