alibi_detect.utils.keops.kernels module
- class alibi_detect.utils.keops.kernels.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_akernel_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
- class alibi_detect.utils.keops.kernels.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 matchsigma_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.
- 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:
- property sigma: torch.Tensor
- Return type:
Tensor
- alibi_detect.utils.keops.kernels.sigma_mean(x, y, dist, n_min=100)[source]
Set bandwidth to the mean distance between instances x and y.
- Parameters:
x (
LazyTensor
) – LazyTensor of instances with dimension [Nx, 1, features] or [batch_size, Nx, 1, features]. The singleton dimension is necessary for broadcasting.y (
LazyTensor
) – LazyTensor of instances with dimension [1, Ny, features] or [batch_size, 1, Ny, features]. The singleton dimension is necessary for broadcasting.dist (
LazyTensor
) – LazyTensor with dimensions [Nx, Ny] or [batch_size, Nx, Ny] containing the pairwise distances between x and y.n_min (
int
) – In order to check whether x equals y after squeezing the singleton dimensions, we check if the diagonal of the distance matrix (which is a lazy tensor from which the diagonal cannot be directly extracted) consists of all zeros. We do this by computing the k-min distances and k-argmin indices over the columns of the distance matrix. We then check if the distances on the diagonal of the distance matrix are all zero or not. If they are all zero, then we do not use these distances (zeros) when computing the mean pairwise distance as bandwidth. If Nx becomes very large, it is advised to set n_min to a low enough value to avoid OOM issues. By default we set it to 100 instances.
- Return type:
Tensor
- Returns:
The computed bandwidth, sigma.