alibi_detect.utils.pytorch.kernels module

class alibi_detect.utils.pytorch.kernels.DeepKernel(proj, kernel_a=torch.nn.Module, kernel_b=torch.nn.Module, eps='trainable')[source]

Bases: torch.nn.Module

Computes similarities as k(x,y) = (1-eps)*k_a(proj(x), proj(y)) + eps*k_b(x,y). A forward pass takes a batch of instances x [Nx, features] and y [Ny, features] and returns the kernel matrix [Nx, Ny].

Parameters
  • proj (Module) – The projection to be applied to the inputs before applying kernel_a

  • kernel_a (Module) – The kernel to apply to the projected inputs. Defaults to a Gaussian RBF with trainable bandwidth.

  • kernel_b (Optional[Module]) – The kernel to apply to the raw inputs. Defaults to a Gaussian RBF with trainable bandwidth. Set to None in order to use only the deep component (i.e. eps=0).

  • eps (Union[float, str]) – The proportion (in [0,1]) of weight to assign to the kernel applied to raw inputs. This can be either specified or set to ‘trainable’. Only relavent if kernel_b is not None.

property eps
Return type

Tensor

forward(x, y)[source]
Return type

Tensor

class alibi_detect.utils.pytorch.kernels.GaussianRBF(sigma=None, trainable=False)[source]

Bases: torch.nn.Module

__init__(sigma=None, trainable=False)[source]

Gaussian RBF kernel: k(x,y) = exp(-(1/(2*sigma^2)||x-y||^2). A forward pass takes a batch of instances x [Nx, features] and y [Ny, features] and returns the kernel matrix [Nx, Ny].

Parameters
  • sigma (Optional[Tensor]) – Bandwidth used for the kernel. Needn’t be specified if being inferred or trained. Can pass multiple values to eval kernel with and then average.

  • trainable (bool) – Whether or not to track gradients w.r.t. sigma to allow it to be trained.

Return type

None

forward(x, y, infer_sigma=False)[source]
Return type

Tensor

property sigma
Return type

Tensor