alibi_detect.utils.tensorflow.distance module

alibi_detect.utils.tensorflow.distance.mmd2(x, y, kernel)[source]

Compute MMD^2 between 2 samples.

Parameters
  • x (Tensor) – Batch of instances of shape [Nx, features].

  • y (Tensor) – Batch of instances of shape [Ny, features].

  • kernel (Callable) – Kernel function.

Return type

float

Returns

MMD^2 between the samples x and y.

alibi_detect.utils.tensorflow.distance.mmd2_from_kernel_matrix(kernel_mat, m, permute=False, zero_diag=True)[source]

Compute maximum mean discrepancy (MMD^2) between 2 samples x and y from the full kernel matrix between the samples.

Parameters
  • kernel_mat (Tensor) – Kernel matrix between samples x and y.

  • m (int) – Number of instances in y.

  • permute (bool) – Whether to permute the row indices. Used for permutation tests.

  • zero_diag (bool) – Whether to zero out the diagonal of the kernel matrix.

Return type

Tensor

Returns

MMD^2 between the samples from the kernel matrix.

alibi_detect.utils.tensorflow.distance.relative_euclidean_distance(x, y, eps=1e-12, axis=-1)[source]

Relative Euclidean distance.

Parameters
  • x (Tensor) – Tensor used in distance computation.

  • y (Tensor) – Tensor used in distance computation.

  • eps (float) – Epsilon added to denominator for numerical stability.

  • axis (int) – Axis used to compute distance.

Return type

Tensor

Returns

Tensor with relative Euclidean distance across specified axis.

alibi_detect.utils.tensorflow.distance.squared_pairwise_distance(x, y, a_min=1e-30, a_max=1e+30)[source]

TensorFlow pairwise squared Euclidean distance between samples x and y.

Parameters
  • x (Tensor) – Batch of instances of shape [Nx, features].

  • y (Tensor) – Batch of instances of shape [Ny, features].

  • a_min (float) – Lower bound to clip distance values.

  • a_max (float) – Upper bound to clip distance values.

Return type

Tensor

Returns

Pairwise squared Euclidean distance [Nx, Ny].