alibi_detect.utils.keops package

class alibi_detect.utils.keops.DeepKernel(proj, kernel_a='rbf', kernel_b='rbf', eps='trainable')[source]

Bases: Module

__init__(proj, kernel_a='rbf', kernel_b='rbf', eps='trainable')[source]

Computes similarities as k(x,y) = (1-eps)*k_a(proj(x), proj(y)) + eps*k_b(x,y). A forward pass takes an already projected batch of instances x_proj and y_proj and optionally (if k_b is present) a batch of instances x and y and returns the kernel matrix. x_proj can be of shape [Nx, 1, features_proj] or [batch_size, Nx, 1, features_proj]. y_proj can be of shape [1, Ny, features_proj] or [batch_size, 1, Ny, features_proj]. x can be of shape [Nx, 1, features] or [batch_size, Nx, 1, features]. y can be of shape [1, Ny, features] or [batch_size, 1, Ny, features]. The returned kernel matrix can be of shape [Nx, Ny] or [batch_size, Nx, Ny]. x, y and the returned kernel matrix are all lazy tensors.

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

  • kernel_a (Union[Module, Literal[‘rbf’]]) – The kernel to apply to the projected inputs. Defaults to a Gaussian RBF with trainable bandwidth.

  • kernel_b (Union[Module, Literal[‘rbf’], None]) – 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, Literal[‘trainable’]]) – 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: torch.Tensor
Return type:

Tensor

forward(x_proj, y_proj, x=None, y=None)[source]
Return type:

LazyTensor

classmethod from_config(config)[source]
get_config()[source]
Return type:

dict

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

Bases: Module

__init__(sigma=None, init_sigma_fn=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 and y and returns the kernel matrix. x can be of shape [Nx, 1, features] or [batch_size, Nx, 1, features]. y can be of shape [1, Ny, features] or [batch_size, 1, Ny, features]. The returned kernel matrix can be of shape [Nx, Ny] or [batch_size, Nx, Ny]. x, y and the returned kernel matrix are all lazy tensors.

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.

  • init_sigma_fn (Optional[Callable]) – Function used to compute the bandwidth sigma. Used when sigma is to be inferred. The function’s signature should match sigma_mean(), meaning that it should take in the lazy tensors x, y and dist and return a tensor sigma.

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

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

LazyTensor

classmethod from_config(config)[source]

Instantiates a kernel from a config dictionary.

Parameters:

config – A kernel config dictionary.

get_config()[source]

Returns a serializable config dict (excluding the input_sigma_fn, which is serialized in alibi_detect.saving).

Return type:

dict

property sigma: torch.Tensor
Return type:

Tensor

Submodules