<commit_before>from abc import ABCMeta
import numpy as np
from six.moves import xrange
import warnings
import collections


class Attack(object):

    """
    Abstract base class for all attack classes.
    """
    __metaclass__ = ABCMeta

    def __init__(self, model, back='tf', sess=None):
        """
        :param model: A function that takes a symbolic input and returns the
                      symbolic output for the model's predictions.
        :param back: The backend to use. Either 'tf' (default) or 'th'.
        :param sess: The tf session to run graphs in (use None for Theano)
        """
        if not(back == 'tf' or back == 'th'):
            raise ValueError("Backend argument must either be 'tf' or 'th'.")
        if back == 'th' and sess is not None:
            raise Exception("A session should not be provided when using th.")
        if not hasattr(model, '__call__'):
            raise ValueError("model argument must be a function that returns "
                             "the symbolic output when given an input tensor.")
        if back == 'th':
            warnings.warn("CleverHans support for Theano is deprecated and "
                          "will be dropped on 2017-11-08.")

        # Prepare attributes
        self.model = model
        self.back = back
        self.sess = sess

        # We are going to keep track of old graphs and cache them.
        self.graphs = {}

        # When calling generate_np, arguments in the following set should be
        # fed into the graph, as they are not structural changes that require
        # generating a new graph.
        # This dict should map names of arguments to the types they should have
        # (Usually, the target class will be a feedable keyword argument.)
        self.feedable_kwargs = {}

    def generate(self, x, **kwargs):
        """
        Generate the attack's symbolic graph for adversarial examples. This
        method should be overriden in any child class that implements an
        attack that is expressable symbolically. Otherwise, it will wrap the
        numerical implementation as a symbolic operator.
        :param x: The model's symbolic inputs.
        :param **kwargs: optional parameters used by child classes.
        :return: A symbolic representation of the adversarial examples.
        """
        if self.back == 'th':
            raise NotImplementedError('Theano version not implemented.')

        error = "Sub-classes must implement generate."
        raise NotImplementedError(error)

    def generate_np(self, x_val, **kwargs):
        """
        Generate adversarial examples and return them as a Numpy array.
        Sub-classes *should not* implement this method unless they must
        perform special handling of arguments.
        :param x_val: A Numpy array with the original inputs.
        :param **kwargs: optional parameters used by child classes.
        :return: A Numpy array holding the adversarial examples.
        """
        if self.back == 'th':
            raise NotImplementedError('Theano version not implemented.')

        import tensorflow as tf

        if self.sess is None:
            raise ValueError("Cannot use `generate_np` when no `sess` was"
                             " provided")

        # the set of arguments that are structural properties of the attack
        # if these arguments are different, we must construct a new graph
        fixed = dict((k, v) for k, v in kwargs.items()
                     if k not in self.feedable_kwargs)

        # the set of arguments that are passed as placeholders to the graph
        # on each call, and can change without constructing a new graph
        feedable = dict((k, v) for k, v in kwargs.items()
                        if k in self.feedable_kwargs)

        if not all(isinstance(value, collections.Hashable)
                   for value in fixed.values()):
            # we have received a fixed value that isn't hashable
            # this means we can't cache this graph for later use,
            # and it will have to be discarded later
            hash_key = None
        else:
            # create a unique key for this set of fixed paramaters
            hash_key = tuple(sorted(fixed.items()))

        if hash_key not in self.graphs:
            # try our very best to create a TF placeholder for each of the
            # feedable keyword arguments by inferring the type
            num_types = (int, float, np.float16, np.float32, np.float64,
                         np.int8, np.int16, np.int32, np.int32, np.int64,
                         np.uint8, np.uint16, np.uint32, np.uint64)

            new_kwargs = dict(x for x in fixed.items())
            for name, value in feedable.items():
                given_type = self.feedable_kwargs[name]
                if isinstance(value, np.ndarray):
                    new_shape = [None]+list(value.shape[1:])
                    new_kwargs[name] = tf.placeholder(value.dtype, new_shape)
                elif isinstance(value, num_types):
                    if isinstance(value, float):
                        new_kwargs[name] = tf.placeholder(given_type, shape=[])
                    elif isinstance(value, int):
                        new_kwargs[name] = tf.placeholder(given_type, shape=[])
                    else:
                        new_kwargs[name] = tf.placeholder(given_type, shape=[])
                else:
                    raise ValueError("Could not identify type of argument " +
                                     name + ": " + str(value))

            # x is a special placeholder we always want to have
            x = tf.placeholder(tf.float32, shape=[None]+list(x_val.shape)[1:])

            # now we generate the graph that we want
            x_adv = self.generate(x, **new_kwargs)

            if hash_key is not None:
                # only save the graph if every fixed element is hashable
                self.graphs[hash_key] = (x, new_kwargs, x_adv)

            if len(self.graphs) == 10:
                warnings.warn("Calling generate_np() with multiple different "
                              "structural paramaters is inefficient and should"
                              " be avoided. Calling generate() is preferred.")

        if hash_key is not None:
            # if it is None, we must have constructed it already
            x, new_kwargs, x_adv = self.graphs[hash_key]

        feed_dict = {x: x_val}

        for name in feedable:
            feed_dict[new_kwargs[name]] = feedable[name]

        return self.sess.run(x_adv, feed_dict)

    def parse_params(self, params=None):
        """
        Take in a dictionary of parameters and applies attack-specific checks
        before saving them as attributes.
        :param params: a dictionary of attack-specific parameters
        :return: True when parsing was successful
        """
        return True


class FastGradientMethod(Attack):

    """
    This attack was originally implemented by Goodfellow et al. (2015) with the<commit_msg>Minor change to make attack fail gracefully<commit_after>from abc import ABCMeta
import numpy as np
from six.moves import xrange
import warnings
import collections


class Attack(object):

    """
    Abstract base class for all attack classes.
    """
    __metaclass__ = ABCMeta

    def __init__(self, model, back='tf', sess=None):
        """
        :param model: A function that takes a symbolic input and returns the
                      symbolic output for the model's predictions.
        :param back: The backend to use. Either 'tf' (default) or 'th'.
        :param sess: The tf session to run graphs in (use None for Theano)
        """
        if not(back == 'tf' or back == 'th'):
            raise ValueError("Backend argument must either be 'tf' or 'th'.")
        if back == 'th' and sess is not None:
            raise Exception("A session should not be provided when using th.")
        if not hasattr(model, '__call__'):
            raise ValueError("model argument must be a function that returns "
                             "the symbolic output when given an input tensor.")
        if back == 'th':
            warnings.warn("CleverHans support for Theano is deprecated and "
                          "will be dropped on 2017-11-08.")

        # Prepare attributes
        self.model = model
        self.back = back
        self.sess = sess

        # We are going to keep track of old graphs and cache them.
        self.graphs = {}

        # When calling generate_np, arguments in the following set should be
        # fed into the graph, as they are not structural changes that require
        # generating a new graph.
        # This dict should map names of arguments to the types they should have
        # (Usually, the target class will be a feedable keyword argument.)
        self.feedable_kwargs = {}

    def generate(self, x, **kwargs):
        """
        Generate the attack's symbolic graph for adversarial examples. This
        method should be overriden in any child class that implements an
        attack that is expressable symbolically. Otherwise, it will wrap the
        numerical implementation as a symbolic operator.
        :param x: The model's symbolic inputs.
        :param **kwargs: optional parameters used by child classes.
        :return: A symbolic representation of the adversarial examples.
        """
        if self.back == 'th':
            raise NotImplementedError('Theano version not implemented.')

        error = "Sub-classes must implement generate."
        raise NotImplementedError(error)

    def generate_np(self, x_val, **kwargs):
        """
        Generate adversarial examples and return them as a Numpy array.
        Sub-classes *should not* implement this method unless they must
        perform special handling of arguments.
        :param x_val: A Numpy array with the original inputs.
        :param **kwargs: optional parameters used by child classes.
        :return: A Numpy array holding the adversarial examples.
        """
        if self.back == 'th':
            raise NotImplementedError('Theano version not implemented.')

        import tensorflow as tf

        if self.sess is None:
            raise ValueError("Cannot use `generate_np` when no `sess` was"
                             " provided")

        # the set of arguments that are structural properties of the attack
        # if these arguments are different, we must construct a new graph
        fixed = dict((k, v) for k, v in kwargs.items()
                     if k not in self.feedable_kwargs)

        # the set of arguments that are passed as placeholders to the graph
        # on each call, and can change without constructing a new graph
        feedable = dict((k, v) for k, v in kwargs.items()
                        if k in self.feedable_kwargs)

        if not all(isinstance(value, collections.Hashable)
                   for value in fixed.values()):
            # we have received a fixed value that isn't hashable
            # this means we can't cache this graph for later use,
            # and it will have to be discarded later
            hash_key = None
        else:
            # create a unique key for this set of fixed paramaters
            hash_key = tuple(sorted(fixed.items()))

        if hash_key not in self.graphs:
            # try our very best to create a TF placeholder for each of the
            # feedable keyword arguments by inferring the type
            num_types = (int, float, np.float16, np.float32, np.float64,
                         np.int8, np.int16, np.int32, np.int32, np.int64,
                         np.uint8, np.uint16, np.uint32, np.uint64)

            new_kwargs = dict(x for x in fixed.items())
            for name, value in feedable.items():
                given_type = self.feedable_kwargs[name]
                if isinstance(value, np.ndarray):
                    new_shape = [None]+list(value.shape[1:])
                    new_kwargs[name] = tf.placeholder(value.dtype, new_shape)
                elif isinstance(value, num_types):
                    if isinstance(value, float):
                        new_kwargs[name] = tf.placeholder(given_type, shape=[])
                    elif isinstance(value, int):
                        new_kwargs[name] = tf.placeholder(given_type, shape=[])
                    else:
                        new_kwargs[name] = tf.placeholder(given_type, shape=[])
                else:
                    raise ValueError("Could not identify type of argument " +
                                     name + ": " + str(value))

            # x is a special placeholder we always want to have
            x = tf.placeholder(tf.float32, shape=[None]+list(x_val.shape)[1:])

            # now we generate the graph that we want
            x_adv = self.generate(x, **new_kwargs)

            if hash_key is not None:
                # only save the graph if every fixed element is hashable
                self.graphs[hash_key] = (x, new_kwargs, x_adv)

            if len(self.graphs) >= 10:
                warnings.warn("Calling generate_np() with multiple different "
                              "structural paramaters is inefficient and should"
                              " be avoided. Calling generate() is preferred.")

        if hash_key is not None:
            # if it is None, we must have constructed it already
            x, new_kwargs, x_adv = self.graphs[hash_key]

        feed_dict = {x: x_val}

        for name in feedable:
            feed_dict[new_kwargs[name]] = feedable[name]

        return self.sess.run(x_adv, feed_dict)

    def parse_params(self, params=None):
        """
        Take in a dictionary of parameters and applies attack-specific checks
        before saving them as attributes.
        :param params: a dictionary of attack-specific parameters
        :return: True when parsing was successful
        """
        return True


class FastGradientMethod(Attack):

    """
    This attack was originally implemented by Goodfellow et al. (2015) with the
{'fraction_non_alphanumeric': 0.0744199207696661, 'fraction_numerical': 0.005659309564233163, 'mean_word_length': 1.932379174445136, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6471981', 'n_tokens_mistral': 3629, 'n_tokens_neox': 3426, 'n_words': 1445}