audtorch.metrics

EnergyConservingLoss

class audtorch.metrics.EnergyConservingLoss(*args: Any, **kwargs: Any)

Energy conserving loss.

A two term loss that enforces energy conservation after [RPS18].

The loss can be described as:

\[\ell(x, y, m) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = |x_n - y_n| + |b_n - \hat{b_n}|, \]

where \(N\) is the batch size. If reduction is not 'none', then:

\[\ell(x, y, m) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases} \]

\(x\) is the input signal (estimated target), \(y\) the target signal, \(m\) the mixture signal, \(b\) the background signal given by \(b = m - y\), and \(\hat{b}\) the estimated background signal given by \(\hat{b} = m - x\).

Parameters

reduction (string, optional) – specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the sum of the output will be divided by the number of elements in the output, ‘sum’: the output will be summed.

Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Target: \((N, *)\), same shape as the input

  • Mixture: \((N, *)\), same shape as the input

  • Output: scalar. If reduction is 'none', then \((N, *)\), same shape as the input

Examples

>>> import torch
>>> _ = torch.manual_seed(0)
>>> loss = EnergyConservingLoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> mixture = torch.randn(3, 5)
>>> loss(input, target, mixture)
tensor(2.1352, grad_fn=<AddBackward0>)

PearsonR

class audtorch.metrics.PearsonR(*, reduction='mean', batch_first=True)

Computes Pearson Correlation Coefficient.

Pearson Correlation Coefficient (also known as Linear Correlation Coefficient or Pearson’s \(\rho\)) is computed as:

\[\rho = \frac {E[(X-\mu_X)(Y-\mu_Y)]} {\sigma_X\sigma_Y}\]

If inputs are vectors, computes Pearson’s \(\rho\) between the two of them. If inputs are multi-dimensional arrays, computes Pearson’s \(\rho\) along the first or last input dimension according to the batch_first argument, returns a torch.Tensor as output, and optionally reduces it according to the reduction argument.

Parameters
  • reduction (string, optional) – specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the sum of the output will be divided by the number of elements in the output, ‘sum’: the output will be summed. Default: ‘mean’

  • batch_first (bool, optional) – controls if batch dimension is first. Default: True

Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Target: \((N, *)\), same shape as the input

  • Output: scalar. If reduction is 'none', then \((N, 1)\)

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> metric = PearsonR()
>>> input = torch.rand(3, 5)
>>> target = torch.rand(3, 5)
>>> metric(input, target)
tensor(0.1220)

ConcordanceCC

class audtorch.metrics.ConcordanceCC(*, reduction='mean', batch_first=True)

Computes Concordance Correlation Coefficient (CCC).

CCC is computed as:

\[\rho_c = \frac {2\rho\sigma_X\sigma_Y} {\sigma_X\sigma_X + \sigma_Y\sigma_Y + (\mu_X - \mu_Y)^2}\]

where \(\rho\) is Pearson Correlation Coefficient, \(\sigma_X\), \(\sigma_Y\) are the standard deviation, and \(\mu_X\), \(\mu_Y\) the mean values of \(X\) and \(Y\) accordingly.

If inputs are vectors, computes CCC between the two of them. If inputs are multi-dimensional arrays, computes CCC along the first or last input dimension according to the batch_first argument, returns a torch.Tensor as output, and optionally reduces it according to the reduction argument.

Parameters
  • reduction (string, optional) – specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the sum of the output will be divided by the number of elements in the output, ‘sum’: the output will be summed. Default: ‘mean’

  • batch_first (bool, optional) – controls if batch dimension is first. Default: True

Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Target: \((N, *)\), same shape as the input

  • Output: scalar. If reduction is 'none', then \((N, 1)\)

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> metric = ConcordanceCC()
>>> input = torch.rand(3, 5)
>>> target = torch.rand(3, 5)
>>> metric(input, target)
tensor(0.0014)