alibi_detect.od.pytorch.pca module

class alibi_detect.od.pytorch.pca.KernelPCATorch(n_components, kernel, device=None)[source]

Bases: PCATorch

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

Kernel variant of the PyTorch backend for PCA detector.

Parameters:
  • n_components (int) – The number of dimensions in the principal subspace.

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

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

compute_kernel_mat(x)[source]

Computes the centered kernel matrix.

Parameters:

x (Tensor) – The reference data.

Return type:

Tensor

Returns:

The centered kernel matrix.

class alibi_detect.od.pytorch.pca.LinearPCATorch(n_components, device=None)[source]

Bases: PCATorch

__init__(n_components, device=None)[source]

Linear variant of the PyTorch backend for PCA detector.

Parameters:
  • n_components (int) – The number of dimensions in the principal subspace.

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

class alibi_detect.od.pytorch.pca.PCATorch(n_components, device=None)[source]

Bases: TorchOutlierDetector

__init__(n_components, device=None)[source]

PyTorch backend for PCA detector.

Parameters:
  • n_components (int) – The number of dimensions in the principal subspace. For linear PCA should have 1 <= n_components < dim(data). For kernel pca should have 1 <= n_components < len(data).

  • 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)[source]

Fits the PCA detector.

Parameters:

x_ref (Tensor) – The Dataset tensor.

Return type:

None

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) – The tensor of instances. First dimension corresponds to batch.

Return type:

Tensor

Returns:

Tensor of scores for each element in x.

Raises:

NotFitException – If called before detector has been fit.