alibi_detect.od.pytorch.svm module

class alibi_detect.od.pytorch.svm.BgdSVMTorch(nu, kernel=None, n_components=None, device=None)[source]

Bases: SVMTorch

__init__(nu, kernel=None, n_components=None, device=None)[source]

Pytorch backend for the Support Vector Machine (SVM) outlier detector.

Parameters:
  • nu (float) – The proportion of the training data that should be considered outliers. Note that this does not necessarily correspond to the false positive rate on test data, which is still defined when calling the infer_threshold method.

  • kernel (Optional[Module]) – Kernel function to use for outlier detection.

  • n_components (Optional[int]) – Number of components in the Nystroem approximation, by default uses all of them.

  • 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.

ensemble = False
fit(x_ref, step_size_range=(1e-08, 1.0), n_step_sizes=16, tol=1e-06, n_iter_no_change=25, max_iter=1000, verbose=0)[source]

Fit the Nystroem approximation and python SVM model.

Parameters:
  • x_ref (Tensor) – Training data.

  • step_size_range (Tuple[float, float]) – The range of values to be considered for the gradient descent step size at each iteration. This is specified as a tuple of the form (min_eta, max_eta).

  • n_step_sizes (int) – The number of step sizes in the defined range to be tested for loss reduction. This many points are spaced equidistantly along the range in log space.

  • tol (float) – The decrease in loss required over the previous n_iter_no_change iterations in order to continue optimizing.

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

  • max_iter (int) – The maximum number of optimization steps.

  • 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_iter: number of iterations performed.

  • lower_bound: loss lower bound.

format_fit_kwargs(fit_kwargs)[source]

Format kwargs for fit method.

Parameters:

fit_kwargs (Dict) – dictionary of Kwargs to format. See fit method for details.

Return type:

Dict

Returns:

Formatted kwargs.

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.

class alibi_detect.od.pytorch.svm.SVMTorch(nu, kernel=None, n_components=None, device=None)[source]

Bases: TorchOutlierDetector

__init__(nu, kernel=None, n_components=None, device=None)[source]

Pytorch backend for the Support Vector Machine (SVM) outlier detector.

Parameters:
  • nu (float) – The proportion of the training data that should be considered outliers. Note that this does not necessarily correspond to the false positive rate on test data, which is still defined when calling the infer_threshold method.

  • kernel (Optional[Module]) – Kernel function to use for outlier detection.

  • n_components (Optional[int]) – Number of components in the Nystroem approximation, by default uses all of them.

  • 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.

ensemble = False
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.

class alibi_detect.od.pytorch.svm.SgdSVMTorch(nu, kernel=None, n_components=None, device=None)[source]

Bases: SVMTorch

__init__(nu, kernel=None, n_components=None, device=None)[source]

SGD Optimization backend for the One class support vector machine (SVM) outlier detector.

Parameters:
  • nu (float) – The proportion of the training data that should be considered outliers. Note that this does not necessarily correspond to the false positive rate on test data, which is still defined when calling the infer_threshold method.

  • kernel (Optional[Module]) – Kernel function to use for outlier detection.

  • n_components (Optional[int]) – Number of components in the Nystroem approximation, by default uses all of them.

  • 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.

ensemble = False
fit(x_ref, tol=1e-06, max_iter=1000, verbose=0)[source]

Fit the Nystroem approximation and Sklearn SGDOneClassSVM SVM model.

Parameters:
  • x_ref (Tensor) – Training data.

  • tol (float) – The decrease in loss required over the previous n_iter_no_change iterations in order to continue optimizing.

  • max_iter (int) – The maximum number of optimization steps.

  • 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_iter: number of iterations performed.

format_fit_kwargs(fit_kwargs)[source]

Format kwargs for fit method.

Parameters:

fit_kwargs (Dict) – dictionary of Kwargs to format. See fit method for details.

Return type:

Dict

Returns:

Formatted kwargs.

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.