Return a tensor that constructs adversarial examples for the given
    input. Generate uses tf.py_func in order to operate over tensors.
    :param x: A tensor with the inputs.
    :param kwargs: See `parse_params`

def generate(self, x, **kwargs):
    """
    Return a tensor that constructs adversarial examples for the given
    input. Generate uses tf.py_func in order to operate over tensors.
    :param x: A tensor with the inputs.
    :param kwargs: See `parse_params`
    """
    self.parse_params(**kwargs)
    shape = [int(i) for i in x.get_shape().as_list()[1:]]

    assert self.sess is not None, \
        'Cannot use `generate` when no `sess` was provided'
    _check_first_dimension(x, 'input')
    if self.y_target is not None:
      _check_first_dimension(self.y_target, 'y_target')
      assert self.image_target is not None, \
          'Require a target image for targeted attack.'
      _check_first_dimension(self.image_target, 'image_target')

    # Set shape and d.
    self.shape = shape
    self.d = int(np.prod(shape))

    # Set binary search threshold.
    if self.constraint == 'l2':
      self.theta = self.gamma / np.sqrt(self.d)
    else:
      self.theta = self.gamma / self.d

    # Construct input placeholder and output for decision function.
    self.input_ph = tf.placeholder(
        tf_dtype, [None] + list(self.shape), name='input_image')
    self.logits = self.model.get_logits(self.input_ph)

    def bapp_wrap(x, target_label, target_image):
      """ Wrapper to use tensors as input and output. """
      return np.array(self._bapp(x, target_label, target_image),
                      dtype=self.np_dtype)

    if self.y_target is not None:
      # targeted attack that requires target label and image.
      wrap = tf.py_func(bapp_wrap,
                        [x[0], self.y_target[0], self.image_target[0]],
                        self.tf_dtype)
    else:
      if self.image_target is not None:
        # untargeted attack with an initialized image.
        wrap = tf.py_func(lambda x, target_image: bapp_wrap(x,
                                                            None, target_image),
                          [x[0], self.image_target[0]],
                          self.tf_dtype)
      else:
        # untargeted attack without an initialized image.
        wrap = tf.py_func(lambda x: bapp_wrap(x, None, None),
                          [x[0]],
                          self.tf_dtype)

    wrap.set_shape(x.get_shape())

    return wrap