docstr: Maximum Mean Discrepancy (MMD) data drift detector where the kernel is trained to maximise an
  estimate of the test power. The kernel is trained on a split of the reference and test instances
  and then the MMD is evaluated on held out instances and a permutation test is performed.
  
  For details see Liu et al (2020): Learning Deep Kernels for Non-Parametric Two-Sample Tests
  (https://arxiv.org/abs/2002.09116)
  
  
  Parameters
  ----------
  x_ref
      Data used as reference distribution.
  kernel
      Trainable PyTorch module that returns a similarity between two instances.
  p_val
      p-value used for the significance of the 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 applying the kernel.
  n_permutations
      The number of permutations to use in the permutation test once the MMD has been computed.
  var_reg
      Constant added to the estimated variance of the MMD for stability.
  reg_loss_fn
      The regularisation term reg_loss_fn(kernel) is added to the loss function being optimized.
  train_size
      Optional fraction (float between 0 and 1) of the dataset used to train the kernel.
      The drift is detected on `1 - train_size`.
  retrain_from_scratch
      Whether the kernel should be retrained from scratch for each set of test data or whether
      it should instead continue training from where it left off on the previous set.
  optimizer
      Optimizer used during training of the kernel.
  learning_rate
      Learning rate used by optimizer.
  batch_size
      Batch size used during training of the kernel.
  batch_size_predict
      Batch size used for the trained drift detector predictions.
  preprocess_batch_fn
      Optional batch preprocessing function. For example to convert a list of objects to a batch which can be
      processed by the kernel.
  epochs
      Number of training epochs for the kernel. Corresponds to the smaller of the reference and test sets.
  num_workers
      Number of workers for the dataloader. The default (`num_workers=0`) means multi-process data loading
      is disabled. Setting `num_workers>0` may be unreliable on Windows.
  verbose
      Verbosity level during the training of the kernel. 0 is silent, 1 a progress bar.
  train_kwargs
      Optional additional kwargs when training the kernel.
  device
      Device type used. The default tries to use the GPU and falls back on CPU if needed.
      Can be specified by passing either ``'cuda'``, ``'gpu'``, ``'cpu'`` or an instance of
      ``torch.device``. Only relevant for 'pytorch' backend.
  dataset
      Dataset object used during training.
  dataloader
      Dataloader object used during training. Only relevant for 'pytorch' backend.
  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","where","the","kernel","is","trained","to","maximise","an","estimate","of","the","test","power",".","The","kernel","is","trained","on","a","split","of","the","reference","and","test","instances","and","then","the","MMD","is","evaluated","on","held","out","instances","and","a","permutation","test","is","performed",".","For","details","see","Liu","et","al","(","2020",")",":","Learning","Deep","Kernels","for","Non-Parametric","Two-Sample","Tests","(","https",":","\/\/arxiv.org\/abs\/2002.09116",")","Parameters","--","--","--","--","--","x_ref","Data","used","as","reference","distribution",".","kernel","Trainable","PyTorch","module","that","returns","a","similarity","between","two","instances",".","p_val","p-value","used","for","the","significance","of","the","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","applying","the","kernel",".","n_permutations","The","number","of","permutations","to","use","in","the","permutation","test","once","the","MMD","has","been","computed",".","var_reg","Constant","added","to","the","estimated","variance","of","the","MMD","for","stability",".","reg_loss_fn","The","regularisation","term","reg_loss_fn","(","kernel",")","is","added","to","the","loss","function","being","optimized",".","train_size","Optional","fraction","(","float","between","0","and","1",")","of","the","dataset","used","to","train","the","kernel",".","The","drift","is","detected","on","`","1","-","train_size","`",".","retrain_from_scratch","Whether","the","kernel","should","be","retrained","from","scratch","for","each","set","of","test","data","or","whether","it","should","instead","continue","training","from","where","it","left","off","on","the","previous","set",".","optimizer","Optimizer","used","during","training","of","the","kernel",".","learning_rate","Learning","rate","used","by","optimizer",".","batch_size","Batch","size","used","during","training","of","the","kernel",".","batch_size_predict","Batch","size","used","for","the","trained","drift","detector","predictions",".","preprocess_batch_fn","Optional","batch","preprocessing","function",".","For","example","to","convert","a","list","of","objects","to","a","batch","which","can","be","processed","by","the","kernel",".","epochs","Number","of","training","epochs","for","the","kernel",".","Corresponds","to","the","smaller","of","the","reference","and","test","sets",".","num_workers","Number","of","workers","for","the","dataloader",".","The","default","(","`","num_workers=0","`",")","means","multi-process","data","loading","is","disabled",".","Setting","`","num_workers",">","0","`","may","be","unreliable","on","Windows",".","verbose","Verbosity","level","during","the","training","of","the","kernel",".","0","is","silent",",","1","a","progress","bar",".","train_kwargs","Optional","additional","kwargs","when","training","the","kernel",".","device","Device","type","used",".","The","default","tries","to","use","the","GPU","and","falls","back","on","CPU","if","needed",".","Can","be","specified","by","passing","either","``","'cuda","'","``",",","``","'gpu","'","``",",","``","'cpu","'","``","or","an","instance","of","``","torch.device","``",".","Only","relevant","for","'pytorch","'","backend",".","dataset","Dataset","object","used","during","training",".","dataloader","Dataloader","object","used","during","training",".","Only","relevant","for","'pytorch","'","backend",".","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],
      kernel: Union[nn.Module, nn.Sequential],
      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,
      n_permutations: int = 100,
      var_reg: float = 1e-5,
      reg_loss_fn: Callable = (lambda kernel: 0),
      train_size: Optional[float] = 0.75,
      retrain_from_scratch: bool = True,
      optimizer: torch.optim.Optimizer = torch.optim.Adam,  # type: ignore
      learning_rate: float = 1e-3,
      batch_size: int = 32,
      batch_size_predict: int = 32,
      preprocess_batch_fn: Optional[Callable] = None,
      epochs: int = 3,
      num_workers: int = 0,
      verbose: int = 0,
      train_kwargs: Optional[dict] = None,
      device: TorchDeviceType = None,
      dataset: Callable = TorchDataset,
      dataloader: Callable = DataLoader,
      input_shape: Optional[tuple] = None,
      data_type: Optional[str] = None,
  ) -> None:
      """
      Maximum Mean Discrepancy (MMD) data drift detector where the kernel is trained to maximise an
      estimate of the test power. The kernel is trained on a split of the reference and test instances
      and then the MMD is evaluated on held out instances and a permutation test is performed.
  
      For details see Liu et al (2020): Learning Deep Kernels for Non-Parametric Two-Sample Tests
      (https://arxiv.org/abs/2002.09116)
  
  
      Parameters
      ----------
      x_ref
          Data used as reference distribution.
      kernel
          Trainable PyTorch module that returns a similarity between two instances.
      p_val
          p-value used for the significance of the 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 applying the kernel.
      n_permutations
          The number of permutations to use in the permutation test once the MMD has been computed.
      var_reg
          Constant added to the estimated variance of the MMD for stability.
      reg_loss_fn
          The regularisation term reg_loss_fn(kernel) is added to the loss function being optimized.
      train_size
          Optional fraction (float between 0 and 1) of the dataset used to train the kernel.
          The drift is detected on `1 - train_size`.
      retrain_from_scratch
          Whether the kernel should be retrained from scratch for each set of test data or whether
          it should instead continue training from where it left off on the previous set.
      optimizer
          Optimizer used during training of the kernel.
      learning_rate
          Learning rate used by optimizer.
      batch_size
          Batch size used during training of the kernel.
      batch_size_predict
          Batch size used for the trained drift detector predictions.
      preprocess_batch_fn
          Optional batch preprocessing function. For example to convert a list of objects to a batch which can be
          processed by the kernel.
      epochs
          Number of training epochs for the kernel. Corresponds to the smaller of the reference and test sets.
      num_workers
          Number of workers for the dataloader. The default (`num_workers=0`) means multi-process data loading
          is disabled. Setting `num_workers>0` may be unreliable on Windows.
      verbose
          Verbosity level during the training of the kernel. 0 is silent, 1 a progress bar.
      train_kwargs
          Optional additional kwargs when training the kernel.
      device
          Device type used. The default tries to use the GPU and falls back on CPU if needed.
          Can be specified by passing either ``'cuda'``, ``'gpu'``, ``'cpu'`` or an instance of
          ``torch.device``. Only relevant for 'pytorch' backend.
      dataset
          Dataset object used during training.
      dataloader
          Dataloader object used during training. Only relevant for 'pytorch' backend.
      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,
          n_permutations=n_permutations,
          train_size=train_size,
          retrain_from_scratch=retrain_from_scratch,
          input_shape=input_shape,
          data_type=data_type,
      )
      self.meta.update({"backend": Framework.PYTORCH.value})
  
      # set device, define model and training kwargs
      self.device = get_device(device)
      self.original_kernel = kernel
      self.kernel = deepcopy(kernel)
  
      # define kwargs for dataloader and trainer
      self.dataset = dataset
      self.dataloader = partial(
          dataloader,
          batch_size=batch_size,
          shuffle=True,
          drop_last=True,
          num_workers=num_workers,
      )
  
      self.kernel_mat_fn = partial(
          batch_compute_kernel_matrix,
          device=self.device,
          preprocess_fn=preprocess_batch_fn,
          batch_size=batch_size_predict,
      )
      self.train_kwargs = {
          "optimizer": optimizer,
          "epochs": epochs,
          "preprocess_fn": preprocess_batch_fn,
          "reg_loss_fn": reg_loss_fn,
          "learning_rate": learning_rate,
          "verbose": verbose,
      }
      if isinstance(train_kwargs, dict):
          self.train_kwargs.update(train_kwargs)
  
      self.j_hat = LearnedKernelDriftTorch.JHat(
          self.kernel, var_reg
      ).to(self.device)
  
file_path: alibi-detect/alibi_detect/cd/pytorch/learned_kernel.py
imports: from copy import deepcopy
  from functools import partial
  from tqdm import tqdm
  import numpy
  import torch
  import torch.nn
  from torch.utils.data import DataLoader
  from typing import Callable, Dict, Optional, Union, Tuple
  from alibi_detect.cd.base import BaseLearnedKernelDrift
  from alibi_detect.utils.pytorch import get_device
  from alibi_detect.utils.pytorch.distance import mmd2_from_kernel_matrix, batch_compute_kernel_matrix
  from alibi_detect.utils.pytorch.data import TorchDataset
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.utils.frameworks import Framework
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 1 to create a new LearnedKernelDriftTorch object from inherited base classes: BaseLearnedKernelDrift with example: obj = LearnedKernelDriftTorch(x_ref, kernel, p_val, x_ref_preprocessed, preprocess_at_init, update_x_ref, preprocess_fn, n_permutations, var_reg, reg_loss_fn, train_size, retrain_from_scratch, optimizer, learning_rate, batch_size, batch_size_predict, preprocess_batch_fn, epochs, num_workers, verbose, train_kwargs, device, dataset, dataloader, input_shape, data_type)