class_docstr: Implementation of Sleeper Agent Attack
  
  | Paper link: https://arxiv.org/pdf/2106.08970.pdf
class_docstr_tok: ["Implementation","of","Sleeper","Agent","Attack","|","Paper","link",":","https",":","\/\/arxiv.org\/pdf\/2106.08970.pdf"]
docstr: Creates a new model.
  
  :param x_train: Samples of train data.
  :param y_train: Labels of train data.
  :param x_test: Samples of test data.
  :param y_test: Labels of test data.
  :param num_classes: Number of classes of labels in train data.
  :param batch_size: The size of batch used for training.
  :param epochs: The number of epochs for which training need to be applied.
  :return model, loss_fn, optimizer - trained model, loss function used to train the model and optimizer used.
docstr_tok: ["Creates","a","new","model",".",":","param","x_train",":","Samples","of","train","data",".",":","param","y_train",":","Labels","of","train","data",".",":","param","x_test",":","Samples","of","test","data",".",":","param","y_test",":","Labels","of","test","data",".",":","param","num_classes",":","Number","of","classes","of","labels","in","train","data",".",":","param","batch_size",":","The","size","of","batch","used","for","training",".",":","param","epochs",":","The","number","of","epochs","for","which","training","need","to","be","applied",".",":","return","model",",","loss_fn",",","optimizer","-","trained","model",",","loss","function","used","to","train","the","model","and","optimizer","used","."]
code: def _create_model(
      self,
      x_train: np.ndarray,
      y_train: np.ndarray,
      x_test: np.ndarray,
      y_test: np.ndarray,
      batch_size: int = 128,
      epochs: int = 80,
  ) -> Union["TensorFlowV2Classifier", "PyTorchClassifier"]:
      """
      Creates a new model.
  
      :param x_train: Samples of train data.
      :param y_train: Labels of train data.
      :param x_test: Samples of test data.
      :param y_test: Labels of test data.
      :param num_classes: Number of classes of labels in train data.
      :param batch_size: The size of batch used for training.
      :param epochs: The number of epochs for which training need to be applied.
      :return model, loss_fn, optimizer - trained model, loss function used to train the model and optimizer used.
      """
      if isinstance(self.substitute_classifier, PyTorchClassifier):
          # Reset Weights of the newly initialized model
          model_pt = self.substitute_classifier.clone_for_refitting()
          for layer in model_pt.model.children():
              if hasattr(layer, "reset_parameters"):
                  layer.reset_parameters()  # type: ignore
          model_pt.fit(
              x_train,
              y_train,
              batch_size=batch_size,
              nb_epochs=epochs,
              verbose=True,
          )
          predictions = model_pt.predict(x_test)
          accuracy = np.sum(
              np.argmax(predictions, axis=1)
              == np.argmax(y_test, axis=1)
          ) / len(y_test)
          logger.info(
              "Accuracy of retrained model : %s", accuracy * 100.0
          )
          return model_pt
  
      if isinstance(self.substitute_classifier, TensorFlowV2Classifier):
          self.substitute_classifier.model.trainable = True
          model_tf = self.substitute_classifier.clone_for_refitting()
          model_tf.fit(
              x_train,
              y_train,
              batch_size=batch_size,
              nb_epochs=epochs,
              verbose=False,
          )
          predictions = model_tf.predict(x_test)
          accuracy = np.sum(
              np.argmax(predictions, axis=1)
              == np.argmax(y_test, axis=1)
          ) / len(y_test)
          logger.info(
              "Accuracy of retrained model : %s", accuracy * 100.0
          )
          return model_tf
  
      raise ValueError(
          "SleeperAgentAttack is currently implemented only for PyTorch and TensorFlowV2."
      )
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import Tuple, TYPE_CHECKING, List, Union
  import random
  import numpy
  from tqdm.auto import trange
  from art.attacks.poisoning.gradient_matching_attack import GradientMatchingAttack
  from art.estimators.classification.pytorch import PyTorchClassifier
  from art.estimators.classification import TensorFlowV2Classifier
  from art.preprocessing.standardisation_mean_std.pytorch import StandardisationMeanStdPyTorch
  from art.preprocessing.standardisation_mean_std.tensorflow import StandardisationMeanStdTensorFlow
label_desc: Use image node_id 6 for calling the SleeperAgentAttack obj's underlying member method code with example usage: obj._create_model(x_train, y_train, x_test, y_test, batch_size, epochs) and returns: model_pt, model_tf