class_docstr: A unittest class for testing the ElasticNet attack.
class_docstr_tok: ["A","unittest","class","for","testing","the","ElasticNet","attack","."]
docstr: First test with the TensorFlowClassifier.
  :return:
docstr_tok: ["First","test","with","the","TensorFlowClassifier",".",":","return",":"]
code: def test_4_tensorflow_mnist(self):
      """
      First test with the TensorFlowClassifier.
      :return:
      """
      x_test_original = self.x_test_mnist.copy()
  
      # Build TensorFlowClassifier
      tfc, sess = get_image_classifier_tf(from_logits=True)
  
      # First attack
      ead = ElasticNet(
          classifier=tfc, targeted=True, max_iter=2, verbose=False
      )
      params = {"y": random_targets(self.y_test_mnist, tfc.nb_classes)}
      x_test_adv = ead.generate(self.x_test_mnist, **params)
      expected_x_test_adv = np.asarray(
          [
              0.45284095,
              0.43225235,
              0.577448,
              1.0,
              0.16554962,
              0.3587564,
              0.19202651,
              0.04293771,
              0.0,
              0.25811037,
              0.0,
              0.0290696,
              0.0,
              0.136172,
              0.6153389,
              0.12244645,
              0.0,
              0.7586619,
              0.8366919,
              0.22206311,
              0.12455986,
              0.02862802,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
          ]
      )
      np.testing.assert_array_almost_equal(
          x_test_adv[0, 14, :, 0], expected_x_test_adv, decimal=6
      )
      self.assertLessEqual(np.amax(x_test_adv), 1.0)
      self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
      target = np.argmax(params["y"], axis=1)
      y_pred_adv = np.argmax(tfc.predict(x_test_adv), axis=1)
      logger.debug("EAD target: %s", target)
      logger.debug("EAD actual: %s", y_pred_adv)
      logger.info(
          "EAD success rate on MNIST: %.2f%%",
          (100 * sum(target == y_pred_adv) / len(target)),
      )
      self.assertTrue((target == y_pred_adv).any())
  
      # Second attack
      ead = ElasticNet(
          classifier=tfc, targeted=False, max_iter=2, verbose=False
      )
      params = {"y": random_targets(self.y_test_mnist, tfc.nb_classes)}
      x_test_adv = ead.generate(self.x_test_mnist, **params)
      self.assertLessEqual(np.amax(x_test_adv), 1.0)
      self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
      target = np.argmax(params["y"], axis=1)
      y_pred_adv = np.argmax(tfc.predict(x_test_adv), axis=1)
      logger.debug("EAD target: %s", target)
      logger.debug("EAD actual: %s", y_pred_adv)
      logger.info(
          "EAD success rate on MNIST: %.2f%%",
          (100 * sum(target != y_pred_adv) / float(len(target))),
      )
      np.testing.assert_array_equal(
          y_pred_adv, np.asarray([7, 1, 1, 4, 4, 1, 4, 4, 4, 4])
      )
  
      # Third attack
      ead = ElasticNet(
          classifier=tfc, targeted=False, max_iter=2, verbose=False
      )
      params = {}
      x_test_adv = ead.generate(self.x_test_mnist, **params)
      expected_x_test_adv = np.asarray(
          [
              0.22766514,
              0.21726893,
              0.22802338,
              0.06168516,
              0.0,
              0.0,
              0.04722975,
              0.0,
              0.0,
              0.0,
              0.05455382,
              0.0,
              0.0,
              0.0,
              0.38886347,
              0.10553087,
              0.32285708,
              0.9794307,
              0.7589039,
              0.16586718,
              0.15969527,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
          ]
      )
      np.testing.assert_array_almost_equal(
          x_test_adv[0, 14, :, 0], expected_x_test_adv, decimal=6
      )
      self.assertLessEqual(np.amax(x_test_adv), 1.0)
      self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
      y_pred = np.argmax(tfc.predict(self.x_test_mnist), axis=1)
      y_pred_adv = np.argmax(tfc.predict(x_test_adv), axis=1)
      logger.debug("EAD target: %s", y_pred)
      logger.debug("EAD actual: %s", y_pred_adv)
      logger.info(
          "EAD success rate: %.2f%%",
          (100 * sum(y_pred != y_pred_adv) / float(len(y_pred))),
      )
      np.testing.assert_array_equal(
          y_pred_adv, np.asarray([0, 4, 7, 9, 0, 7, 7, 3, 0, 7])
      )
  
      # First attack without batching
      ead_wob = ElasticNet(
          classifier=tfc,
          targeted=True,
          max_iter=2,
          batch_size=1,
          verbose=False,
      )
      params = {"y": random_targets(self.y_test_mnist, tfc.nb_classes)}
      x_test_adv = ead_wob.generate(self.x_test_mnist, **params)
      expected_x_test_adv = np.asarray(
          [
              0.32568744,
              0.31085464,
              0.43235543,
              1.0,
              0.2433605,
              0.3411813,
              0.49469775,
              0.2603619,
              0.0,
              0.35584357,
              0.0,
              0.2046029,
              0.0,
              0.08249304,
              1.0,
              0.35813788,
              0.62945133,
              1.0,
              0.32154015,
              0.3497113,
              0.7613426,
              0.36533928,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
              0.0,
          ]
      )
      np.testing.assert_array_almost_equal(
          x_test_adv[0, 14, :, 0], expected_x_test_adv, decimal=6
      )
      self.assertLessEqual(np.amax(x_test_adv), 1.0)
      self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
      target = np.argmax(params["y"], axis=1)
      y_pred_adv = np.argmax(tfc.predict(x_test_adv), axis=1)
      logger.debug("EAD target: %s", target)
      logger.debug("EAD actual: %s", y_pred_adv)
      logger.info(
          "EAD success rate: %.2f%%",
          (100 * sum(target == y_pred_adv) / float(len(target))),
      )
      self.assertTrue((target == y_pred_adv).any())
  
      # Second attack without batching
      ead_wob = ElasticNet(
          classifier=tfc,
          targeted=False,
          max_iter=2,
          batch_size=1,
          verbose=False,
      )
      params = {"y": random_targets(self.y_test_mnist, tfc.nb_classes)}
      x_test_adv = ead_wob.generate(self.x_test_mnist, **params)
      self.assertLessEqual(np.amax(x_test_adv), 1.0)
      self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
      target = np.argmax(params["y"], axis=1)
      y_pred_adv = np.argmax(tfc.predict(x_test_adv), axis=1)
      logger.debug("EAD target: %s", target)
      logger.debug("EAD actual: %s", y_pred_adv)
      logger.info(
          "EAD success rate: %.2f%%",
          (100 * sum(target != y_pred_adv) / float(len(target))),
      )
      np.testing.assert_array_equal(
          y_pred_adv, np.asarray([7, 1, 1, 4, 4, 1, 4, 4, 4, 4])
      )
  
      # Check that x_test has not been modified by attack and classifier
      self.assertAlmostEqual(
          float(np.max(np.abs(x_test_original - self.x_test_mnist))),
          0.0,
          delta=0.00001,
      )
  
      ead = ElasticNet(
          classifier=tfc,
          targeted=False,
          max_iter=2,
          verbose=False,
          decision_rule="L1",
      )
      _ = ead.generate(self.x_test_mnist, **params)
      ead = ElasticNet(
          classifier=tfc,
          targeted=False,
          max_iter=2,
          verbose=False,
          decision_rule="L2",
      )
      _ = ead.generate(self.x_test_mnist, **params)
      with self.assertRaises(ValueError):
          _ = ElasticNet(
              classifier=tfc,
              targeted=False,
              max_iter=2,
              verbose=False,
              decision_rule="L3",
          )
  
      # Close session
      if sess is not None:
          sess.close()
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import unittest
  import keras.backend
  import numpy
  from art.attacks.evasion.elastic_net import ElasticNet
  from art.estimators.estimator import BaseEstimator
  from art.estimators.classification.classifier import ClassGradientsMixin
  from art.estimators.classification.keras import KerasClassifier
  from art.utils import random_targets, to_categorical
  from tests.attacks.utils import backend_test_classifier_type_check_fail
  from tests.utils import TestBase, get_image_classifier_kr, get_image_classifier_pt, get_image_classifier_tf, get_tabular_classifier_kr, get_tabular_classifier_pt, get_tabular_classifier_tf, master_seed
label_desc: Use image node_id 4 for calling the TestElasticNet obj's underlying member method code with example usage: obj.test_4_tensorflow_mnist() without return types