class_docstr: This class tests the deep partition ensemble classifier.
class_docstr_tok: ["This","class","tests","the","deep","partition","ensemble","classifier","."]
docstr: Test with a TensorFlow Classifier.
  :return:
docstr_tok: ["Test","with","a","TensorFlow","Classifier",".",":","return",":"]
code: def test_1_tf(self):
      """
      Test with a TensorFlow Classifier.
      :return:
      """
      tf_version = list(
          map(int, tf.__version__.lower().split("+")[0].split("."))
      )
      if tf_version[0] == 2:
          # Get MNIST
          (x_train, y_train), (x_test, y_test) = self.mnist
  
          # Create a model from scratch
          from tensorflow.keras import Model
          from tensorflow.keras.layers import (
              Dense,
              Flatten,
              Conv2D,
              MaxPool2D,
          )
  
          class TensorFlowModel(Model):
              """
              Standard TensorFlow model for unit testing.
              """
  
              def __init__(self):
                  super(TensorFlowModel, self).__init__()
                  self.conv1 = Conv2D(
                      filters=4, kernel_size=5, activation="relu"
                  )
                  self.conv2 = Conv2D(
                      filters=10, kernel_size=5, activation="relu"
                  )
                  self.maxpool = MaxPool2D(
                      pool_size=(2, 2),
                      strides=(2, 2),
                      padding="valid",
                      data_format=None,
                  )
                  self.flatten = Flatten()
                  self.dense1 = Dense(100, activation="relu")
                  self.logits = Dense(10, activation="linear")
  
              def call(self, x):
                  """
                  Call function to evaluate the model.
  
                  :param x: Input to the model
                  :return: Prediction of the model
                  """
                  x = self.conv1(x)
                  x = self.maxpool(x)
                  x = self.conv2(x)
                  x = self.maxpool(x)
                  x = self.flatten(x)
                  x = self.dense1(x)
                  x = self.logits(x)
                  return x
  
          model = TensorFlowModel()
          loss_object = tf.keras.losses.CategoricalCrossentropy(
              from_logits=True
          )
          optimizer = Adam(learning_rate=0.01)
          model.compile(loss=loss_object, optimizer=optimizer)
  
          classifier = TensorFlowV2Classifier(
              model=model,
              loss_object=loss_object,
              optimizer=optimizer,
              nb_classes=10,
              input_shape=(28, 28, 1),
              clip_values=(0, 1),
          )
  
          # Initialize DPA Classifier
          dpa = DeepPartitionEnsemble(
              classifiers=classifier,
              ensemble_size=ENSEMBLE_SIZE,
              channels_first=classifier.channels_first,
              clip_values=classifier.clip_values,
              preprocessing_defences=classifier.preprocessing_defences,
              postprocessing_defences=classifier.postprocessing_defences,
              preprocessing=classifier.preprocessing,
          )
  
          # Check basic functionality of DPA Classifier
          # check predict
          y_test_dpa = dpa.predict(x=x_test)
          self.assertEqual(y_test_dpa.shape, y_test.shape)
          self.assertTrue(
              (
                  np.sum(y_test_dpa, axis=1)
                  <= ENSEMBLE_SIZE * np.ones((NB_TEST,))
              ).all()
          )
  
          # loss gradient
          grad = dpa.loss_gradient(x=x_test, y=y_test, sampling=True)
          assert grad.shape == (10, 28, 28, 1)
  
          # fit
          dpa.fit(x=x_train, y=y_train)
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import os
  import unittest
  import numpy
  import tensorflow
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
  from tensorflow.keras.losses import categorical_crossentropy
  from tensorflow.keras.optimizers.legacy import Adam
  import torch.nn
  import torch.nn.functional
  import torch.optim
  from art.utils import load_dataset
  from art.estimators.classification.deep_partition_ensemble import DeepPartitionEnsemble
  from art.estimators.classification import TensorFlowV2Classifier, PyTorchClassifier, KerasClassifier
  from tests.utils import master_seed
label_desc: Use image node_id 3 for calling the TestDeepPartitionEnsemble obj's underlying member method code with example usage: obj.test_1_tf() and returns: x