class_docstr: A unittest class for testing the ElasticNet attack.
class_docstr_tok: ["A","unittest","class","for","testing","the","ElasticNet","attack","."]
docstr: Second test with the KerasClassifier.
  :return:
docstr_tok: ["Second","test","with","the","KerasClassifier",".",":","return",":"]
code: def test_9a_keras_mnist(self):
      """
      Second test with the KerasClassifier.
      :return:
      """
      x_test_original = self.x_test_mnist.copy()
  
      # Build KerasClassifier
      krc = get_image_classifier_kr()
  
      # First attack
      ead = ElasticNet(
          classifier=krc, targeted=True, max_iter=2, verbose=False
      )
      y_target = to_categorical(
          np.asarray([6, 6, 7, 4, 9, 7, 9, 0, 1, 0]), nb_classes=10
      )
      x_test_adv = ead.generate(self.x_test_mnist, y=y_target)
      expected_x_test_adv = np.asarray(
          [
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              7.8193319e-04,
              5.5843666e-03,
              0.0000000e00,
              0.0000000e00,
              4.9869284e-01,
              1.0000000e00,
              6.4663666e-01,
              3.4855194e-03,
              3.5087438e-03,
              0.0000000e00,
              9.8862723e-03,
              3.8835173e-03,
              3.0151173e-02,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
              0.0000000e00,
          ]
      )
      np.testing.assert_array_almost_equal(
          x_test_adv[2, 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(y_target, axis=1)
      y_pred_adv = np.argmax(krc.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
      ead = ElasticNet(
          classifier=krc, targeted=False, max_iter=2, verbose=False
      )
      y_target = to_categorical(
          np.asarray([9, 5, 6, 7, 1, 6, 1, 5, 8, 5]), nb_classes=10
      )
      x_test_adv = ead.generate(self.x_test_mnist, y=y_target)
      self.assertLessEqual(np.amax(x_test_adv), 1.0)
      self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
      y_pred_adv = np.argmax(krc.predict(x_test_adv), axis=1)
      logger.debug("EAD target: %s", y_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())
      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,
      )
  
      k.clear_session()
  
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 5 for calling the TestElasticNet obj's underlying member method code with example usage: obj.test_9a_keras_mnist() without return types