docstr: Maximum Mean Discrepancy (MMD) data drift detector using a permutation test.
  
  Parameters
  ----------
  x_ref
      Data used as reference distribution.
  p_val
      p-value used for the significance of the permutation test.
  x_ref_preprocessed
      Whether the given reference data `x_ref` has been preprocessed yet. If `x_ref_preprocessed=True`, only
      the test data `x` will be preprocessed at prediction time. If `x_ref_preprocessed=False`, the reference
      data will also be preprocessed.
  preprocess_at_init
      Whether to preprocess the reference data when the detector is instantiated. Otherwise, the reference
      data will be preprocessed at prediction time. Only applies if `x_ref_preprocessed=False`.
  update_x_ref
      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
      Function to preprocess the data before computing the data drift metrics.
  kernel
      Kernel used for the MMD computation, defaults to Gaussian RBF kernel.
  sigma
      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
      Whether to already configure the kernel bandwidth from the reference data.
  n_permutations
      Number of permutations used in the permutation test.
  input_shape
      Shape of input data.
  data_type
      Optionally specify the data type (tabular, image or time-series). Added to metadata.
docstr_tok: ["Maximum","Mean","Discrepancy","(","MMD",")","data","drift","detector","using","a","permutation","test",".","Parameters","--","--","--","--","--","x_ref","Data","used","as","reference","distribution",".","p_val","p-value","used","for","the","significance","of","the","permutation","test",".","x_ref_preprocessed","Whether","the","given","reference","data","`","x_ref","`","has","been","preprocessed","yet",".","If","`","x_ref_preprocessed=True","`",",","only","the","test","data","`","x","`","will","be","preprocessed","at","prediction","time",".","If","`","x_ref_preprocessed=False","`",",","the","reference","data","will","also","be","preprocessed",".","preprocess_at_init","Whether","to","preprocess","the","reference","data","when","the","detector","is","instantiated",".","Otherwise",",","the","reference","data","will","be","preprocessed","at","prediction","time",".","Only","applies","if","`","x_ref_preprocessed=False","`",".","update_x_ref","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","Function","to","preprocess","the","data","before","computing","the","data","drift","metrics",".","kernel","Kernel","used","for","the","MMD","computation",",","defaults","to","Gaussian","RBF","kernel",".","sigma","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","Whether","to","already","configure","the","kernel","bandwidth","from","the","reference","data",".","n_permutations","Number","of","permutations","used","in","the","permutation","test",".","input_shape","Shape","of","input","data",".","data_type","Optionally","specify","the","data","type","(","tabular",",","image","or","time-series",")",".","Added","to","metadata","."]
code: def __init__(
      self,
      x_ref: Union[np.ndarray, list],
      p_val: float = 0.05,
      x_ref_preprocessed: bool = False,
      preprocess_at_init: bool = True,
      update_x_ref: Optional[Dict[str, int]] = None,
      preprocess_fn: Optional[Callable] = None,
      kernel: Callable = GaussianRBF,
      sigma: Optional[np.ndarray] = None,
      configure_kernel_from_x_ref: bool = True,
      n_permutations: int = 100,
      input_shape: Optional[tuple] = None,
      data_type: Optional[str] = None,
  ) -> None:
      """
      Maximum Mean Discrepancy (MMD) data drift detector using a permutation test.
  
      Parameters
      ----------
      x_ref
          Data used as reference distribution.
      p_val
          p-value used for the significance of the permutation test.
      x_ref_preprocessed
          Whether the given reference data `x_ref` has been preprocessed yet. If `x_ref_preprocessed=True`, only
          the test data `x` will be preprocessed at prediction time. If `x_ref_preprocessed=False`, the reference
          data will also be preprocessed.
      preprocess_at_init
          Whether to preprocess the reference data when the detector is instantiated. Otherwise, the reference
          data will be preprocessed at prediction time. Only applies if `x_ref_preprocessed=False`.
      update_x_ref
          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
          Function to preprocess the data before computing the data drift metrics.
      kernel
          Kernel used for the MMD computation, defaults to Gaussian RBF kernel.
      sigma
          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
          Whether to already configure the kernel bandwidth from the reference data.
      n_permutations
          Number of permutations used in the permutation test.
      input_shape
          Shape of input data.
      data_type
          Optionally specify the data type (tabular, image or time-series). Added to metadata.
      """
      super().__init__(
          x_ref=x_ref,
          p_val=p_val,
          x_ref_preprocessed=x_ref_preprocessed,
          preprocess_at_init=preprocess_at_init,
          update_x_ref=update_x_ref,
          preprocess_fn=preprocess_fn,
          sigma=sigma,
          configure_kernel_from_x_ref=configure_kernel_from_x_ref,
          n_permutations=n_permutations,
          input_shape=input_shape,
          data_type=data_type,
      )
      self.meta.update({"backend": Framework.TENSORFLOW.value})
  
      # initialize kernel
      if isinstance(sigma, np.ndarray):
          sigma = tf.convert_to_tensor(sigma)
      self.kernel = kernel(sigma) if kernel == GaussianRBF else kernel
  
      # compute kernel matrix for the reference data
      if self.infer_sigma or isinstance(sigma, tf.Tensor):
          self.k_xx = self.kernel(
              self.x_ref, self.x_ref, infer_sigma=self.infer_sigma
          )
          self.infer_sigma = False
      else:
          self.k_xx, self.infer_sigma = None, True
  
file_path: alibi-detect/alibi_detect/cd/tensorflow/mmd.py
imports: import logging
  import numpy
  import tensorflow
  from typing import Callable, Dict, Optional, Tuple, Union
  from alibi_detect.cd.base import BaseMMDDrift
  from alibi_detect.utils.tensorflow.distance import mmd2_from_kernel_matrix
  from alibi_detect.utils.tensorflow.kernels import GaussianRBF
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.utils.frameworks import Framework
label_desc: Use image node_id 1 to create a new MMDDriftTF object from inherited base classes: BaseMMDDrift with example: obj = MMDDriftTF(x_ref, p_val, x_ref_preprocessed, preprocess_at_init, update_x_ref, preprocess_fn, kernel, sigma, configure_kernel_from_x_ref, n_permutations, input_shape, data_type)