'Return a tensor that constructs adversarial examples for the given
input. Generate uses tf.py_func in order to operate over tensors.
:param x: (required) A tensor with the inputs.
:param y: (optional) A tensor with the true labels for an untargeted
attack. If None (and y_target is None) then use the
original labels the classifier assigns.
:param y_target: (optional) A tensor with the target labels for a
targeted attack.
:param nb_classes: The number of classes the model has.
:param confidence: Confidence of adversarial examples: higher produces
examples with larger l2 distortion, but more
strongly classified as adversarial.
:param batch_size: Number of attacks to run simultaneously.
:param learning_rate: The learning rate for the attack algorithm.
Smaller values produce better results but are
slower to converge.
:param binary_search_steps: The number of times we perform binary
search to find the optimal tradeoff-
constant between norm of the purturbation
and confidence of the classification.
:param max_iterations: The maximum number of iterations. Setting this
to a larger value will produce lower distortion
results. Using only a few iterations requires
a larger learning rate, and will produce larger
distortion results.
:param abort_early: If true, allows early aborts if gradient descent
is unable to make progress (i.e., gets stuck in
a local minimum).
:param initial_const: The initial tradeoff-constant to use to tune the
relative importance of size of the pururbation
and confidence of classification.
If binary_search_steps is large, the initial
constant is not important. A smaller value of
this constant gives lower distortion results.
:param clip_min: (optional float) Minimum input component value
:param clip_max: (optional float) Maximum input component value'

def generate(self, x, **kwargs):

    import tensorflow as tf
    from .attacks_tf import CarliniWagnerL2 as CWL2
    self.parse_params(**kwargs)
    attack = CWL2(self.sess, self.model, self.batch_size, self.confidence, ('y_target' in kwargs), self.learning_rate, self.binary_search_steps, self.max_iterations, self.abort_early, self.initial_const, self.clip_min, self.clip_max, self.nb_classes, x.get_shape().as_list()[1:])
    if (('y' in kwargs) and ('y_target' in kwargs)):
        raise ValueError("Can    not    set    both    'y'    and    'y_target'.")
    elif ('y' in kwargs):
        labels = kwargs['y']
    elif ('y_target' in kwargs):
        labels = kwargs['y_target']
    else:
        preds = self.model.get_probs(x)
        preds_max = tf.reduce_max(preds, 1, keep_dims=True)
        original_predictions = tf.to_float(tf.equal(preds, preds_max))
        labels = original_predictions
    def cw_wrap(x_val, y_val):
        return np.array(attack.attack(x_val, y_val), dtype=np.float32)
    wrap = tf.py_func(cw_wrap, [x, labels], tf.float32)
    return wrap
