alibi_detect.od.pytorch.gmm module

class alibi_detect.od.pytorch.gmm.GMMTorch(n_components, device=None)[source]

Bases: TorchOutlierDetector

__init__(n_components, device=None)[source]

Pytorch backend for the Gaussian Mixture Model (GMM) outlier detector.

Parameters:
  • n_components (int) – Number of components in gaussian mixture model.

  • device (Union[Literal[‘cuda’, ‘gpu’, ‘cpu’], device, None]) – Device type used. The default tries to use the GPU and falls back on CPU if needed. Can be specified by passing either 'cuda', 'gpu', 'cpu' or an instance of torch.device.

Raises:

ValueError – If n_components is less than 1.

ensemble = False
fit(x_ref, optimizer=torch.optim.Adam, learning_rate=0.1, max_epochs=10, batch_size=32, tol=0.001, n_iter_no_change=25, verbose=0)[source]

Fit the GMM model.

Parameters:
  • x_ref (Tensor) – Training data.

  • optimizer (Type[Optimizer]) – Optimizer used to train the model.

  • learning_rate (float) – Learning rate used to train the model.

  • max_epochs (int) – Maximum number of training epochs.

  • batch_size (int) – Batch size used to train the model.

  • tol (float) – Convergence threshold. Training iterations will stop when the lower bound average gain is below this threshold.

  • n_iter_no_change (int) – The number of iterations over which the loss must decrease by tol in order for optimization to continue.

  • verbose (int) – Verbosity level during training. 0 is silent, 1 a progress bar.

Return type:

Dict

Returns:

Dictionary with fit results. The dictionary contains the following keys

  • converged: bool indicating whether training converged.

  • n_epochs: number of gradient descent iterations performed.

  • lower_bound: log-likelihood lower bound.

format_fit_kwargs(fit_kwargs)[source]

Format kwargs for fit method.

Parameters:

kwargs – dictionary of Kwargs to format. See fit method for details.

Return type:

Dict

Returns:

Formatted kwargs.

forward(x)[source]

Detect if x is an outlier.

Parameters:

x (Tensor) – torch.Tensor with leading batch dimension.

Return type:

Tensor

Returns:

torch.Tensor of bool values with leading batch dimension.

Raises:

ThresholdNotInferredException – If called before detector has had infer_threshold method called.

score(x)[source]

Computes the score of x

Parameters:

x (Tensor) – torch.Tensor with leading batch dimension.

Return type:

Tensor

Returns:

torch.Tensor of scores with leading batch dimension.

Raises:

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