- class alibi_detect.od.pytorch.knn.KNNTorch(k, kernel=None, ensembler=None, device=None)[source]
- __init__(k, kernel=None, ensembler=None, device=None)[source]
PyTorch backend for KNN detector.
int]) – Number of nearest neighbors to compute distance to. k can be a single value or an array of integers. If k is a single value the outlier score is the distance/kernel similarity to the k-th nearest neighbor. If k is a list then it returns the distance/kernel similarity to each of the specified k neighbors.
Module]) – If a kernel is specified then instead of using torch.cdist the kernel defines the k nearest neighbor distance.
Ensembler]) – If k is an array of integers then the ensembler must not be
None. Should be an instance of
alibi_detect.od.pytorch.ensemble.ensembler. Responsible for combining multiple scores into a single score.
Literal[‘cuda’, ‘gpu’, ‘cpu’],
None]) – Device type used. The default tries to use the GPU and falls back on CPU if needed. Can be specified by passing either
'cpu'or an instance of
Detect if x is an outlier.
Tensor) – torch.Tensor with leading batch dimension.
- Return type:
boolvalues with leading batch dimension.
ThresholdNotInferredError – If called before detector has had infer_threshold method called.
Computes the score of x
Tensor) – The tensor of instances. First dimension corresponds to batch.
- Return type:
Tensor of scores for each element in x.
NotFittedError – If called before detector has been fit.