DeepMahalanobis#

class scio.scores.DeepMahalanobis(*, act_norm=None, mode='raw', epsilon=0.0, fgm_norm=inf, weights=None)[source]#

Bases: BaseScoreClassif

DeepMahalanobis for classification.

Parameters:
  • epsilon (float) – Amplitude of the adversarial reinforcement. Defaults to 0.

  • fgm_norm (float) – Parameter \(p\) for \(L^p\) adversarial reinforcement. Defaults to inf.

  • weights (Sequence[float], optional) – Weights for layer aggregation. If not provided, treated as [1] * n_layers at inference.

  • mode – See BaseScoreClassif.

  • act_norm – See BaseScore.

Notes

In [LLLS18], the authors benchmark in a supervised setup, which allows the learning of weights. The unsupervised equivalent uses predetermined weights or only one layer (when weights is not provided).

When epsilon > 0, this score applies an “adversarial perturbation” to move input sample closer to its best candidate class. The paper does not mention conformity for other classes. As such, when computing conformity for other classes, we chose to use still use the perturbation for the best candidate class (for computational reasons).

Functionality for Relative Mahalanobis Distance (RMD) is implemented but unused. See RelativeMahalanobis for more info.

Note

Due to a design choice in favor of inference efficiency, this class is not numerically very stable for torch.half. See compute_precision().

References

[LLLS18]

Kimin Lee, Kibok Lee, Honglak Lee, and Jinwoo Shin. A simple unified framework for detecting Out-of-Distribution samples and adversarial attacks. In Advances in Neural Information Processing Systems, volume 31. 2018. URL: https://proceedings.nips.cc/paper_files/paper/2018/file/abdeb6f575ac5c6676b747bca8d09cc2-Paper.pdf.

Hint

Below this point, the documentation is meant for development purposes only. Manual use of any listed member is highly discouraged. For usage, see Inferring with Confidence.

Useful methods defined here

compute_mahalanobis(samples, mu, precision, *)

Compute squared Mahalanobis distance.

compute_precision(residues)

Compute the inverse of the covariance of the residues.

static compute_mahalanobis(samples, mu, precision, *, product=True)[source]#

Compute squared Mahalanobis distance.

Parameters:
  • samples (Tensor) – Explicit. Shape (*s_shape, data_dim).

  • mu (Tensor) – Centroids. Shape (*m_shape, data_dim).

  • precision (Tensor) – Inverse of covariance matrix.

  • product (bool) – Whether to compute for the cartesian product of combinations of samples and centroids. If False, requires samples and mu to be broadcastable. Defaults to True.

Returns:

D2 (Tensor) – Squared Mahalanobis distance for combinations (defined by product) of samples and centroids. If product, the output shape is (*s_shape, *m_shape). Else, it is the result of broadcasting s_shape and m_shape together.

static compute_precision(residues)[source]#

Compute the inverse of the covariance of the residues.

To avoid a potential GPU > CPU > GPU bottleneck, we use torch’s experimental inv_ex(), which does not support torch.half. This may induce a temporary type conversion internally.

Parameters:

residues (Tensor) – Assumed centered feature-wise. Shape (n_samples, data_dim).

Returns:

precision (Tensor) – Inverse covariance matrix. Shape (data_dim, data_dim). Full of nan if the covariance matrix is singular (i.e. for residues residing on a strict submanifold) or generates numerical overflow during inversion.

Note

We compute and store the precision since it may be used multiple times. Note however that for a single Mahalanobis distance computation, it is faster and numerically more stable to use torch.lstsq() with the covariance directly.