alibi_detect.od.pytorch.base module

class alibi_detect.od.pytorch.base.TorchOutlierDetector(device=None)[source]

Bases: Module, FitMixinTorch, ABC

Base class for torch backend outlier detection algorithms.

check_threshold_inferred()[source]

Check if threshold is inferred.

Raises:

ThresholdNotInferredError – Raised if threshold is not inferred.

infer_threshold(x, fpr)[source]

Infer the threshold for the data. Prerequisite for outlier predictions.

Parameters:
  • x (Tensor) – Data to infer the threshold for.

  • fpr (float) – False positive rate to use for threshold inference.

Raises:
  • ValueError – Raised if fpr is not in (0, 1).

  • ValueError – Raised if fpr is less than 1/len(x).

predict(x)[source]

Predict outlier labels for the data.

Computes the outlier scores. If the detector is not fit on reference data we raise an error. If the threshold is inferred, the outlier labels and p-values are also computed and returned. Otherwise, the outlier labels and p-values are set to None.

Parameters:

x (Tensor) – Data to predict.

Return type:

TorchOutlierDetectorOutput

Returns:

Output of the outlier detector. Includes the p-values, outlier labels, instance scores and threshold.

Raises:

ValueError – Raised if the detector is not fit on reference data.

abstract score(x)[source]

Score the data.

Parameters:

x (Tensor) – Data to score.

Return type:

Tensor

threshold = None
threshold_inferred = False
class alibi_detect.od.pytorch.base.TorchOutlierDetectorOutput(threshold_inferred, instance_score, threshold, is_outlier, p_value)[source]

Bases: object

Output of the outlier detector.

instance_score: torch.Tensor
is_outlier: torch.Tensor | None
p_value: torch.Tensor | None
threshold: torch.Tensor | None
threshold_inferred: bool
to_numpy()[source]
alibi_detect.od.pytorch.base.to_numpy(x)[source]

Converts any torch tensors found in input to numpy arrays.

Takes a torch tensor or TorchOutlierDetectorOutput and converts any torch tensors found to numpy arrays

Parameters:

x (Union[Tensor, TorchOutlierDetectorOutput]) – Data to convert.

Return type:

Union[ndarray, Dict[str, ndarray]]

Returns:

np.ndarray or dictionary of containing numpy arrays