docstr: One-Class Support vector machine (OCSVM) outlier detector.
  
  The one-class Support vector machine outlier detector fits a one-class SVM to the reference data.
  
  Rather than the typical approach of optimizing the exact kernel OCSVM objective through a dual formulation,
  here we instead map the data into the kernel's RKHS and then solve the linear optimization problem
  directly through its primal formulation. The Nystroem approximation is used to speed up training and inference
  by approximating the kernel's RKHS.
  
  We provide two options, specified by the `optimization` parameter, for optimizing the one-class svm. `''sgd''`
  wraps the `SGDOneClassSVM` class from the sklearn package and the other, `''bgd''` uses a custom implementation
  in PyTorch. The PyTorch approach is tailored for operation on GPUs. Instead of applying stochastic gradient
  descent (one data point at a time) with a fixed learning rate schedule it performs full gradient descent with
  step size chosen at each iteration via line search. Note that on a CPU this would not necessarily be preferable
  to SGD as we would have to iterate through both data points and candidate step sizes, however on GPU all of the
  operations are vectorized/parallelized. Moreover, the Nystroem approximation has complexity `O(n^2m)` where
  `n` is the number of reference instances and `m` defines the number of inducing points. This can therefore be
  expensive for large reference sets and benefits from implementation on the GPU.
  
  In general if using a small dataset then using the `''cpu''` with the optimization `''sgd''` is the best choice.
  Whereas if using a large dataset then using the `''gpu''` with the optimization `''bgd''` is the best choice.
  
  Parameters
  ----------
  nu
      The proportion of the training data that should be considered outliers. Note that this does not necessarily
      correspond to the false positive rate on test data, which is still defined when calling the
      `infer_threshold` method. `nu` should be thought of as a regularization parameter that affects how smooth
      the svm decision boundary is.
  n_components
      Number of components in the Nystroem approximation, By default uses all of them.
  kernel
      Kernel function to use for outlier detection. Should be an instance of a subclass of `torch.nn.Module`. If
      not specified then defaults to the `GaussianRBF`.
  optimization
      Optimization method to use. Choose from ``'sgd'`` or ``'bgd'``. Defaults to ``'sgd'``.
  backend
      Backend used for outlier detection. Defaults to ``'pytorch'``. Options are ``'pytorch'``.
  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``.
  
  Raises
  ------
  NotImplementedError
      If choice of `backend` is not implemented.
  ValueError
      If choice of `optimization` is not valid.
  ValueError
      If `n_components` is not a positive integer.
docstr_tok: ["One-Class","Support","vector","machine","(","OCSVM",")","outlier","detector",".","The","one-class","Support","vector","machine","outlier","detector","fits","a","one-class","SVM","to","the","reference","data",".","Rather","than","the","typical","approach","of","optimizing","the","exact","kernel","OCSVM","objective","through","a","dual","formulation",",","here","we","instead","map","the","data","into","the","kernel","'s","RKHS","and","then","solve","the","linear","optimization","problem","directly","through","its","primal","formulation",".","The","Nystroem","approximation","is","used","to","speed","up","training","and","inference","by","approximating","the","kernel","'s","RKHS",".","We","provide","two","options",",","specified","by","the","`","optimization","`","parameter",",","for","optimizing","the","one-class","svm",".","`","``","sgd","''","`","wraps","the","`","SGDOneClassSVM","`","class","from","the","sklearn","package","and","the","other",",","`","``","bgd","''","`","uses","a","custom","implementation","in","PyTorch",".","The","PyTorch","approach","is","tailored","for","operation","on","GPUs",".","Instead","of","applying","stochastic","gradient","descent","(","one","data","point","at","a","time",")","with","a","fixed","learning","rate","schedule","it","performs","full","gradient","descent","with","step","size","chosen","at","each","iteration","via","line","search",".","Note","that","on","a","CPU","this","would","not","necessarily","be","preferable","to","SGD","as","we","would","have","to","iterate","through","both","data","points","and","candidate","step","sizes",",","however","on","GPU","all","of","the","operations","are","vectorized\/parallelized",".","Moreover",",","the","Nystroem","approximation","has","complexity","`","O","(","n^2m",")","`","where","`","n","`","is","the","number","of","reference","instances","and","`","m","`","defines","the","number","of","inducing","points",".","This","can","therefore","be","expensive","for","large","reference","sets","and","benefits","from","implementation","on","the","GPU",".","In","general","if","using","a","small","dataset","then","using","the","`","``","cpu","''","`","with","the","optimization","`","``","sgd","''","`","is","the","best","choice",".","Whereas","if","using","a","large","dataset","then","using","the","`","``","gpu","''","`","with","the","optimization","`","``","bgd","''","`","is","the","best","choice",".","Parameters","--","--","--","--","--","nu","The","proportion","of","the","training","data","that","should","be","considered","outliers",".","Note","that","this","does","not","necessarily","correspond","to","the","false","positive","rate","on","test","data",",","which","is","still","defined","when","calling","the","`","infer_threshold","`","method",".","`","nu","`","should","be","thought","of","as","a","regularization","parameter","that","affects","how","smooth","the","svm","decision","boundary","is",".","n_components","Number","of","components","in","the","Nystroem","approximation",",","By","default","uses","all","of","them",".","kernel","Kernel","function","to","use","for","outlier","detection",".","Should","be","an","instance","of","a","subclass","of","`","torch.nn.Module","`",".","If","not","specified","then","defaults","to","the","`","GaussianRBF","`",".","optimization","Optimization","method","to","use",".","Choose","from","``","'sgd","'","``","or","``","'bgd","'","``",".","Defaults","to","``","'sgd","'","``",".","backend","Backend","used","for","outlier","detection",".","Defaults","to","``","'pytorch","'","``",".","Options","are","``","'pytorch","'","``",".","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","``",".","Raises","--","--","--","NotImplementedError","If","choice","of","`","backend","`","is","not","implemented",".","ValueError","If","choice","of","`","optimization","`","is","not","valid",".","ValueError","If","`","n_components","`","is","not","a","positive","integer","."]
code: def __init__(
      self,
      nu: float,
      n_components: Optional[int] = None,
      kernel: "torch.nn.Module" = None,
      optimization: Literal["sgd", "bgd"] = "sgd",
      backend: Literal["pytorch"] = "pytorch",
      device: TorchDeviceType = None,
  ) -> None:
      """One-Class Support vector machine (OCSVM) outlier detector.
  
      The one-class Support vector machine outlier detector fits a one-class SVM to the reference data.
  
      Rather than the typical approach of optimizing the exact kernel OCSVM objective through a dual formulation,
      here we instead map the data into the kernel's RKHS and then solve the linear optimization problem
      directly through its primal formulation. The Nystroem approximation is used to speed up training and inference
      by approximating the kernel's RKHS.
  
      We provide two options, specified by the `optimization` parameter, for optimizing the one-class svm. `''sgd''`
      wraps the `SGDOneClassSVM` class from the sklearn package and the other, `''bgd''` uses a custom implementation
      in PyTorch. The PyTorch approach is tailored for operation on GPUs. Instead of applying stochastic gradient
      descent (one data point at a time) with a fixed learning rate schedule it performs full gradient descent with
      step size chosen at each iteration via line search. Note that on a CPU this would not necessarily be preferable
      to SGD as we would have to iterate through both data points and candidate step sizes, however on GPU all of the
      operations are vectorized/parallelized. Moreover, the Nystroem approximation has complexity `O(n^2m)` where
      `n` is the number of reference instances and `m` defines the number of inducing points. This can therefore be
      expensive for large reference sets and benefits from implementation on the GPU.
  
      In general if using a small dataset then using the `''cpu''` with the optimization `''sgd''` is the best choice.
      Whereas if using a large dataset then using the `''gpu''` with the optimization `''bgd''` is the best choice.
  
      Parameters
      ----------
      nu
          The proportion of the training data that should be considered outliers. Note that this does not necessarily
          correspond to the false positive rate on test data, which is still defined when calling the
          `infer_threshold` method. `nu` should be thought of as a regularization parameter that affects how smooth
          the svm decision boundary is.
      n_components
          Number of components in the Nystroem approximation, By default uses all of them.
      kernel
          Kernel function to use for outlier detection. Should be an instance of a subclass of `torch.nn.Module`. If
          not specified then defaults to the `GaussianRBF`.
      optimization
          Optimization method to use. Choose from ``'sgd'`` or ``'bgd'``. Defaults to ``'sgd'``.
      backend
          Backend used for outlier detection. Defaults to ``'pytorch'``. Options are ``'pytorch'``.
      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``.
  
      Raises
      ------
      NotImplementedError
          If choice of `backend` is not implemented.
      ValueError
          If choice of `optimization` is not valid.
      ValueError
          If `n_components` is not a positive integer.
      """
      super().__init__()
  
      if optimization not in ("sgd", "bgd"):
          raise ValueError(
              f"Optimization {optimization} not recognized. Choose from `sgd` or `bgd`."
          )
  
      if n_components is not None and n_components <= 0:
          raise ValueError(
              f"n_components must be a positive integer, got {n_components}."
          )
  
      backend_str: str = backend.lower()
      BackendValidator(
          backend_options={"pytorch": ["pytorch"]},
          construct_name=self.__class__.__name__,
      ).verify_backend(backend_str)
  
      backend_cls = backends[backend][optimization]
      args: Dict[str, Any] = {
          "n_components": n_components,
          "kernel": kernel,
          "nu": nu,
      }
      args["device"] = device
      self.backend = backend_cls(**args)
  
      # set metadata
      self.meta["detector_type"] = "outlier"
      self.meta["data_type"] = "numeric"
      self.meta["online"] = False
  
file_path: alibi-detect/alibi_detect/od/_svm.py
imports: from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
  import numpy
  from alibi_detect.base import BaseDetector, FitMixin, ThresholdMixin, outlier_prediction_dict
  from alibi_detect.exceptions import _catch_error
  from alibi_detect.od.pytorch import SgdSVMTorch, BgdSVMTorch
  from alibi_detect.utils._types import Literal
  from alibi_detect.utils.frameworks import BackendValidator
  from alibi_detect.version import __version__
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 1 to create a new SVM object from inherited base classes: BaseDetector, ThresholdMixin, FitMixin with example: obj = SVM(nu, n_components, kernel, optimization, backend, device)