docstr: Run the attack on a batch of instance and labels.
docstr_tok: ["Run","the","attack","on","a","batch","of","instance","and","labels","."]
code: def attack_batch(self, imgs, labs):
      """
      Run the attack on a batch of instance and labels.
      """
  
      def compare(x, y):
          if not isinstance(x, (float, int, np.int64)):
              x = np.copy(x)
              if self.TARGETED:
                  x[y] -= self.CONFIDENCE
              else:
                  x[y] += self.CONFIDENCE
              x = np.argmax(x)
          if self.TARGETED:
              return x == y
          else:
              return x != y
  
      batch_size = self.batch_size
  
      imgs = np.clip(imgs, self.clip_min, self.clip_max)
  
      # set the lower and upper bounds accordingly
      lower_bound = np.zeros(batch_size)
      CONST = np.ones(batch_size) * self.initial_const
      upper_bound = np.ones(batch_size) * 1e10
  
      # placeholders for the best en, score, and instance attack found so far
      o_bestdst = [1e10] * batch_size
      o_bestscore = [-1] * batch_size
      o_bestattack = np.copy(imgs)
  
      for outer_step in range(self.BINARY_SEARCH_STEPS):
          # completely reset the optimizer's internal state.
          self.sess.run(self.init)
          batch = imgs[:batch_size]
          batchlab = labs[:batch_size]
  
          bestdst = [1e10] * batch_size
          bestscore = [-1] * batch_size
          _logger.debug(
              "  Binary search step %s of %s",
              outer_step,
              self.BINARY_SEARCH_STEPS,
          )
  
          # The last iteration (if we run many steps) repeat the search once.
          if self.repeat and outer_step == self.BINARY_SEARCH_STEPS - 1:
              CONST = upper_bound
  
          # set the variables so that we don't have to send them over again
          self.sess.run(
              self.setup,
              {
                  self.assign_timg: batch,
                  self.assign_tlab: batchlab,
                  self.assign_const: CONST,
              },
          )
          self.sess.run(self.setter, {self.assign_newimg: batch})
          self.sess.run(self.setter_y, {self.assign_slack: batch})
          prev = 1e6
          for iteration in range(self.MAX_ITERATIONS):
              # perform the attack
              self.sess.run([self.train])
              self.sess.run([self.setter, self.setter_y])
              l, l2s, l1s, crit, scores, nimg = self.sess.run(
                  [
                      self.loss,
                      self.l2dist,
                      self.l1dist,
                      self.crit,
                      self.output,
                      self.newimg,
                  ]
              )
              if iteration % ((self.MAX_ITERATIONS // 10) or 1) == 0:
                  _logger.debug(
                      (
                          "    Iteration {} of {}: loss={:.3g} "
                          + "l2={:.3g} l1={:.3g} f={:.3g}"
                      ).format(
                          iteration,
                          self.MAX_ITERATIONS,
                          l,
                          np.mean(l2s),
                          np.mean(l1s),
                          np.mean(scores),
                      )
                  )
  
              # check if we should abort search if we're getting nowhere.
              if (
                  self.ABORT_EARLY
                  and iteration % ((self.MAX_ITERATIONS // 10) or 1)
                  == 0
              ):
                  if l > prev * 0.9999:
                      msg = "    Failed to make progress; stop early"
                      _logger.debug(msg)
                      break
                  prev = l
  
              # adjust the best result found so far
              for e, (dst, sc, ii) in enumerate(
                  zip(crit, scores, nimg)
              ):
                  lab = np.argmax(batchlab[e])
                  if dst < bestdst[e] and compare(sc, lab):
                      bestdst[e] = dst
                      bestscore[e] = np.argmax(sc)
                  if dst < o_bestdst[e] and compare(sc, lab):
                      o_bestdst[e] = dst
                      o_bestscore[e] = np.argmax(sc)
                      o_bestattack[e] = ii
  
          # adjust the constant as needed
          for e in range(batch_size):
              if (
                  compare(bestscore[e], np.argmax(batchlab[e]))
                  and bestscore[e] != -1
              ):
                  # success, divide const by two
                  upper_bound[e] = min(upper_bound[e], CONST[e])
                  if upper_bound[e] < 1e9:
                      CONST[e] = (lower_bound[e] + upper_bound[e]) / 2
              else:
                  # failure, either multiply by 10 if no solution found yet
                  #          or do binary search with the known upper bound
                  lower_bound[e] = max(lower_bound[e], CONST[e])
                  if upper_bound[e] < 1e9:
                      CONST[e] = (lower_bound[e] + upper_bound[e]) / 2
                  else:
                      CONST[e] *= 10
          _logger.debug(
              "  Successfully generated adversarial examples "
              + "on {} of {} instances.".format(
                  sum(upper_bound < 1e9), batch_size
              )
          )
          o_bestdst = np.array(o_bestdst)
          mean = np.mean(np.sqrt(o_bestdst[o_bestdst < 1e9]))
          _logger.debug(
              self.crit_p
              + " Mean successful distortion: {:.4g}".format(mean)
          )
  
      # return the best solution found
      o_bestdst = np.array(o_bestdst)
      return o_bestattack
  
file_path: cleverhans/cleverhans_v3.1.0/cleverhans/attacks/elastic_net_method.py
imports: import logging
  import numpy
  import tensorflow
  from cleverhans.attacks.attack import Attack
  from cleverhans.compat import reduce_sum, reduce_max
  from cleverhans.model import Model, CallableModelWrapper, wrapper_warning_logits
  from cleverhans import utils
label_desc: Use image node_id 3 for calling the EAD obj's underlying member method code with example usage: obj.attack_batch(imgs, labs) and returns: o_bestattack, unknown, unknown