DeepMahalanobis#
- class scio.scores.DeepMahalanobis(*, act_norm=None, mode='raw', epsilon=0.0, fgm_norm=inf, weights=None)[source]#
Bases:
BaseScoreClassifDeepMahalanobis for classification.
- Parameters:
epsilon (
float) – Amplitude of the adversarial reinforcement. Defaults to0.fgm_norm (
float) – Parameter \(p\) for \(L^p\) adversarial reinforcement. Defaults toinf.weights (
Sequence[float], optional) – Weights for layer aggregation. If not provided, treated as[1] * n_layersat 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 (whenweightsis 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
RelativeMahalanobisfor more info.Note
Due to a design choice in favor of inference efficiency, this class is not numerically very stable for
torch.half. Seecompute_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. IfFalse, requiressamplesandmuto be broadcastable. Defaults toTrue.
- Returns:
D2 (
Tensor) – Squared Mahalanobis distance for combinations (defined byproduct) of samples and centroids. Ifproduct, the output shape is(*s_shape, *m_shape). Else, it is the result of broadcastings_shapeandm_shapetogether.
- static compute_precision(residues)[source]#
Compute the inverse of the covariance of the residues.
To avoid a potential
GPU > CPU > GPUbottleneck, we use torch’s experimentalinv_ex(), which does not supporttorch.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 ofnanif the covariance matrix is singular (i.e. forresiduesresiding on a strict submanifold) or generates numerical overflow during inversion.
Note
We compute and store the
precisionsince it may be used multiple times. Note however that for a single Mahalanobis distance computation, it is faster and numerically more stable to usetorch.lstsq()with the covariance directly.