alibi_detect.cd.pytorch.mmd module

class alibi_detect.cd.pytorch.mmd.MMDDriftTorch(x_ref, p_val=0.05, preprocess_x_ref=True, update_x_ref=None, preprocess_fn=None, kernel=<class 'alibi_detect.utils.pytorch.kernels.GaussianRBF'>, sigma=None, configure_kernel_from_x_ref=True, n_permutations=100, device=None, input_shape=None, data_type=None)[source]

Bases: alibi_detect.cd.base.BaseMMDDrift

__init__(x_ref, p_val=0.05, preprocess_x_ref=True, update_x_ref=None, preprocess_fn=None, kernel=<class 'alibi_detect.utils.pytorch.kernels.GaussianRBF'>, sigma=None, configure_kernel_from_x_ref=True, n_permutations=100, device=None, input_shape=None, data_type=None)[source]

Maximum Mean Discrepancy (MMD) data drift detector using a permutation test.

Parameters
  • x_ref (Union[ndarray, list]) – Data used as reference distribution.

  • p_val (float) – p-value used for the significance of the permutation test.

  • preprocess_x_ref (bool) – Whether to already preprocess and store the reference data.

  • update_x_ref (Optional[Dict[str, int]]) – Reference data can optionally be updated to the last n instances seen by the detector or via reservoir sampling with size n. For the former, the parameter equals {‘last’: n} while for reservoir sampling {‘reservoir_sampling’: n} is passed.

  • preprocess_fn (Optional[Callable]) – Function to preprocess the data before computing the data drift metrics.

  • kernel (Callable) – Kernel used for the MMD computation, defaults to Gaussian RBF kernel.

  • sigma (Optional[ndarray]) – Optionally set the GaussianRBF kernel bandwidth. Can also pass multiple bandwidth values as an array. The kernel evaluation is then averaged over those bandwidths.

  • configure_kernel_from_x_ref (bool) – Whether to already configure the kernel bandwidth from the reference data.

  • n_permutations (int) – Number of permutations used in the permutation test.

  • device (Optional[str]) – Device type used. The default None tries to use the GPU and falls back on CPU if needed. Can be specified by passing either ‘cuda’, ‘gpu’ or ‘cpu’.

  • input_shape (Optional[tuple]) – Shape of input data.

  • data_type (Optional[str]) – Optionally specify the data type (tabular, image or time-series). Added to metadata.

Return type

None

kernel_matrix(x, y)[source]

Compute and return full kernel matrix between arrays x and y.

Return type

Tensor

score(x)[source]

Compute the p-value resulting from a permutation test using the maximum mean discrepancy as a distance measure between the reference data and the data to be tested.

Parameters

x (Union[ndarray, list]) – Batch of instances.

Return type

Tuple[float, float, ndarray]

Returns

  • p-value obtained from the permutation test, the MMD^2 between the reference and test set

  • and the MMD^2 values from the permutation test.