class_docstr: Implementation of Label-Only Inference Attack based on Decision Boundary.
  
  You only need to call ONE of the calibrate methods, depending on which attack you want to launch.
  
  | Paper link: https://arxiv.org/abs/2007.14321 (Choquette-Choo et al.)
  | Paper link: https://arxiv.org/abs/2007.15528 (Li and Zhang)
docstr: Calibrate distance threshold maximising the membership inference accuracy on `x_train` and `x_test`.
  
  | Paper link: https://arxiv.org/abs/2007.14321
  
  :param x_train: Training data.
  :param y_train: Labels of training data `x_train`.
  :param x_test: Test data.
  :param y_test: Labels of test data `x_test`.
  
  :Keyword Arguments for HopSkipJump:
      * *norm*: Order of the norm. Possible values: "inf", np.inf or 2.
      * *max_iter*: Maximum number of iterations.
      * *max_eval*: Maximum number of evaluations for estimating gradient.
      * *init_eval*: Initial number of evaluations for estimating gradient.
      * *init_size*: Maximum number of trials for initial generation of adversarial examples.
      * *verbose*: Show progress bars.
docstr_tok: ["Calibrate","distance","threshold","maximising","the","membership","inference","accuracy","on","`","x_train","`","and","`","x_test","`",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/2007.14321",":","param","x_train",":","Training","data",".",":","param","y_train",":","Labels","of","training","data","`","x_train","`",".",":","param","x_test",":","Test","data",".",":","param","y_test",":","Labels","of","test","data","`","x_test","`",".",":","Keyword","Arguments","for","HopSkipJump",":","*","*","norm","*",":","Order","of","the","norm",".","Possible","values",":","``","inf","''",",","np.inf","or","2",".","*","*","max_iter","*",":","Maximum","number","of","iterations",".","*","*","max_eval","*",":","Maximum","number","of","evaluations","for","estimating","gradient",".","*","*","init_eval","*",":","Initial","number","of","evaluations","for","estimating","gradient",".","*","*","init_size","*",":","Maximum","number","of","trials","for","initial","generation","of","adversarial","examples",".","*","*","verbose","*",":","Show","progress","bars","."]
code: def calibrate_distance_threshold(
      self,
      x_train: np.ndarray,
      y_train: np.ndarray,
      x_test: np.ndarray,
      y_test: np.ndarray,
      **kwargs,
  ):
      """
      Calibrate distance threshold maximising the membership inference accuracy on `x_train` and `x_test`.
  
      | Paper link: https://arxiv.org/abs/2007.14321
  
      :param x_train: Training data.
      :param y_train: Labels of training data `x_train`.
      :param x_test: Test data.
      :param y_test: Labels of test data `x_test`.
  
      :Keyword Arguments for HopSkipJump:
          * *norm*: Order of the norm. Possible values: "inf", np.inf or 2.
          * *max_iter*: Maximum number of iterations.
          * *max_eval*: Maximum number of evaluations for estimating gradient.
          * *init_eval*: Initial number of evaluations for estimating gradient.
          * *init_size*: Maximum number of trials for initial generation of adversarial examples.
          * *verbose*: Show progress bars.
      """
      from art.attacks.evasion.hop_skip_jump import HopSkipJump
  
      if "classifier" in kwargs:  # pragma: no cover
          raise ValueError(
              "Keyword `classifier` in kwargs is not supported."
          )
  
      if "targeted" in kwargs:  # pragma: no cover
          raise ValueError(
              "Keyword `targeted` in kwargs is not supported."
          )
  
      y_train_onehot = check_and_transform_label_format(
          y_train, nb_classes=self.estimator.nb_classes
      )
      if y_train_onehot is None:
          raise ValueError("None value detected.")
      y_test_onehot = check_and_transform_label_format(
          y_test, nb_classes=self.estimator.nb_classes
      )
      if y_test_onehot is None:
          raise ValueError("None value detected.")
  
      hsj = HopSkipJump(
          classifier=self.estimator, targeted=False, **kwargs
      )
  
      x_train_adv = hsj.generate(x=x_train, y=y_train_onehot)
      x_test_adv = hsj.generate(x=x_test, y=y_test_onehot)
  
      distance_train = np.linalg.norm(
          (x_train_adv - x_train).reshape((x_train.shape[0], -1)),
          ord=2,
          axis=1,
      )
      distance_test = np.linalg.norm(
          (x_test_adv - x_test).reshape((x_test.shape[0], -1)),
          ord=2,
          axis=1,
      )
  
      y_train_pred = self.estimator.predict(x=x_train)
      y_test_pred = self.estimator.predict(x=x_test)
  
      distance_train[
          np.argmax(y_train_pred, axis=1)
          != np.argmax(y_train_onehot, axis=1)
      ] = 0
      distance_test[
          np.argmax(y_test_pred, axis=1)
          != np.argmax(y_test_onehot, axis=1)
      ] = 0
  
      num_increments = 100
      tau_increment = (
          np.amax([np.amax(distance_train), np.amax(distance_test)])
          / num_increments
      )
  
      acc_max = 0.0
      distance_threshold_tau = 0.0
      self.threshold_bins = []
      for i_tau in range(1, num_increments):
          # searching for threshold that yields the best accuracy in separating between members and non-members
          is_member_train = np.where(
              distance_train > i_tau * tau_increment, 1, 0
          )
          is_member_test = np.where(
              distance_test > i_tau * tau_increment, 1, 0
          )
  
          acc = (
              np.sum(is_member_train)
              + (is_member_test.shape[0] - np.sum(is_member_test))
          ) / (is_member_train.shape[0] + is_member_test.shape[0])
          new_threshold_tau = i_tau * tau_increment
          self.threshold_bins.append((new_threshold_tau, acc))
          if acc > acc_max:
              distance_threshold_tau = new_threshold_tau
              acc_max = acc
  
      self.distance_threshold_tau = distance_threshold_tau
  
file_path: adversarial-robustness-toolbox/art/attacks/inference/membership_inference/label_only_boundary_distance.py
imports: import logging
  from typing import Optional, TYPE_CHECKING
  import numpy
  from art.attacks.attack import MembershipInferenceAttack
  from art.estimators.estimator import BaseEstimator
  from art.estimators.classification.classifier import ClassifierMixin
  from art.utils import check_and_transform_label_format
label_desc: Use image node_id 3 for calling the LabelOnlyDecisionBoundary obj's underlying member method code with example usage: obj.calibrate_distance_threshold(x_train, y_train, x_test, y_test) without return types