alibi_detect.utils.tensorflow.kernels module

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

Bases: Model

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 (Model) – The projection to be applied to the inputs before applying kernel_a

  • kernel_a (Union[Model, str]) – The kernel to apply to the projected inputs. Defaults to a Gaussian RBF with trainable bandwidth.

  • kernel_b (Union[Model, str, 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, 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 is kernel_b is not None.

call(x, y)[source]
Return type

Tensor

property eps: tensorflow.Tensor
Return type

Tensor

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

dict

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

Bases: Model

__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 [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.

  • 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_median(), meaning that it should take in the tensors x, y and dist and return sigma. If None, it is set to sigma_median().

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

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

Tensor

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: tensorflow.Tensor
Return type

Tensor

alibi_detect.utils.tensorflow.kernels.sigma_median(x, y, dist)[source]

Bandwidth estimation using the median heuristic Gretton et al. [GBR+12].

Parameters
  • x (Tensor) – Tensor of instances with dimension [Nx, features].

  • y (Tensor) – Tensor of instances with dimension [Ny, features].

  • dist (Tensor) – Tensor with dimensions [Nx, Ny], containing the pairwise distances between x and y.

Return type

Tensor

Returns

The computed bandwidth, sigma.