alibi_detect.od.pytorch.ensemble module

class alibi_detect.od.pytorch.ensemble.AverageAggregator(weights=None)[source]

Bases: BaseTransformTorch

__init__(weights=None)[source]

Averages the scores of the detectors in an ensemble.

Parameters:

weights (Optional[Tensor]) – Optional parameter to weight the scores. If weights is left None then will be set to a vector of ones.

Raises:

ValueError – If weights does not sum to 1.

transform(scores)[source]

Averages the scores of the detectors in an ensemble. If weights were passed in the __init__ then these are used to weight the scores.

Parameters:

scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of mean of scores.

class alibi_detect.od.pytorch.ensemble.BaseTransformTorch(*args: Any, **kwargs: Any)[source]

Bases: Module

__init__()[source]

Base Transform class.

provides abstract methods for transform objects that map torch tensors.

forward(x)[source]
Return type:

Tensor

transform(x)[source]

Public transform method.

Parameters:

x (Tensor) – torch.Tensor array to be transformed

class alibi_detect.od.pytorch.ensemble.Ensembler(normalizer=None, aggregator=None)[source]

Bases: BaseTransformTorch, FitMixinTorch

__init__(normalizer=None, aggregator=None)[source]

An Ensembler applies normalization and aggregation operations to the scores of an ensemble of detectors.

Parameters:
  • normalizer (Optional[BaseTransformTorch]) – BaseFittedTransformTorch object to normalize the scores. If None then no normalization is applied.

  • aggregator (Optional[BaseTransformTorch]) – BaseTransformTorch object to aggregate the scores. If None defaults to AverageAggregator.

fit(x)[source]

Fit the normalizer to the scores.

Parameters:

x (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Self

transform(x)[source]

Apply the normalizer and aggregator to the scores.

Parameters:

x (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of aggregated and normalized scores.

class alibi_detect.od.pytorch.ensemble.FitMixinTorch[source]

Bases: ABC

check_fitted()[source]

Checks to make sure object has been fitted.

Raises:

NotFittedError – Raised if method called and object has not been fit.

abstract fit(x_ref)[source]

Abstract fit method.

Parameters:

xtorch.Tensor to fit object on.

Return type:

Self

fitted = False
class alibi_detect.od.pytorch.ensemble.MaxAggregator(*args: Any, **kwargs: Any)[source]

Bases: BaseTransformTorch

__init__()[source]

Takes the maximum of the scores of the detectors in an ensemble.

transform(scores)[source]

Takes the maximum score of a set of detectors in an ensemble.

Parameters:

scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of maximum scores.

class alibi_detect.od.pytorch.ensemble.MinAggregator(*args: Any, **kwargs: Any)[source]

Bases: BaseTransformTorch

__init__()[source]

Takes the minimum score of a set of detectors in an ensemble.

transform(scores)[source]

Takes the minimum score of a set of detectors in an ensemble.

Parameters:

scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of minimum scores.

class alibi_detect.od.pytorch.ensemble.PValNormalizer(*args: Any, **kwargs: Any)[source]

Bases: BaseTransformTorch, FitMixinTorch

__init__()[source]

Maps scores to there p-values.

Needs to be fit (see BaseFittedTransformTorch). Returns the proportion of scores in the reference dataset that are greater than the score of interest. Output is between 1 and 0. Small values are likely to be outliers.

fit(val_scores)[source]

Fit transform on scores.

Parameters:

val_scores (Tensor) – score outputs of ensemble of detectors applied to reference data.

Return type:

Self

transform(scores)[source]

Transform scores to 1 - p-values.

Parameters:

scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of 1 - p-values.

class alibi_detect.od.pytorch.ensemble.ShiftAndScaleNormalizer(*args: Any, **kwargs: Any)[source]

Bases: BaseTransformTorch, FitMixinTorch

__init__()[source]

Maps scores to their normalized values.

Needs to be fit (see BaseFittedTransformTorch). Subtracts the dataset mean and scales by the standard deviation.

fit(val_scores)[source]

Computes the mean and standard deviation of the scores and stores them.

Parameters:

val_scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Self

transform(scores)[source]

Transform scores to normalized values. Subtracts the mean and scales by the standard deviation.

Parameters:

scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of normalized scores.

class alibi_detect.od.pytorch.ensemble.TopKAggregator(k=None)[source]

Bases: BaseTransformTorch

__init__(k=None)[source]

Takes the mean of the top k scores.

Parameters:

k (Optional[int]) – number of scores to take the mean of. If k is left None then will be set to half the number of scores passed in the forward call.

transform(scores)[source]

Takes the mean of the top k scores.

Parameters:

scores (Tensor) – Torch.Tensor of scores from ensemble of detectors.

Return type:

Tensor

Returns:

Torch.Tensor of mean of top k scores.