<commit_before>import tensorflow as tf


def fgsm(model, x, eps=0.01, epochs=1, clip_min=0., clip_max=1.):

    x_adv = tf.identity(x)

    ybar = model(x_adv)
    yshape = tf.shape(ybar)
    ydim = yshape[1]
    indices = tf.argmax(ybar, axis=1)
    target = tf.one_hot(indices, ydim, on_value=clip_max,
                        off_value=clip_min)

    eps = tf.abs(eps)

    def _cond(x_adv, i):
        return tf.less(i, epochs)

    def _body(x_adv, i):
        ybar, logits = model(x_adv, logits=True)
        loss = tf.nn.softmax_cross_entropy_with_logits(
            labels=target, logits=logits)
        dy_dx, = tf.gradients(loss, x_adv)
        x_adv = tf.stop_gradient(x_adv + eps*tf.sign(dy_dx))
        x_adv = tf.clip_by_value(x_adv, clip_min, clip_max)
        return x_adv, i+1

    x_adv, i = tf.while_loop(_cond, _body, (x_adv, 0),
                             back_prop=False, name='fgsm')
    return x_adv<commit_msg>add doc to fgsm<commit_after>import tensorflow as tf


def fgsm(model, x, eps=0.01, epochs=1, clip_min=0., clip_max=1.):
    """
    Fast gradient sign method.

    See https://arxiv.org/abs/1412.6572 and https://arxiv.org/abs/1607.02533 for
    details.  This implements the revised version, since the original FGSM has
    label leaking problem.

    :param model: A wrapper that returns the output as well as logits.
    :param x: The input placeholder.
    :param eps: The scale factor for noise.
    :param epochs: The maximum epoch to run.
    :param clip_min: The minimum value in output.
    :param clip_max: The maximum value in output.

    :return: A tensor, contains adversarial samples for each input.
    """
    x_adv = tf.identity(x)

    ybar = model(x_adv)
    yshape = tf.shape(ybar)
    ydim = yshape[1]
    indices = tf.argmax(ybar, axis=1)
    target = tf.one_hot(indices, ydim, on_value=clip_max,
                        off_value=clip_min)

    eps = tf.abs(eps)

    def _cond(x_adv, i):
        return tf.less(i, epochs)

    def _body(x_adv, i):
        ybar, logits = model(x_adv, logits=True)
        loss = tf.nn.softmax_cross_entropy_with_logits(
            labels=target, logits=logits)
        dy_dx, = tf.gradients(loss, x_adv)
        x_adv = tf.stop_gradient(x_adv + eps*tf.sign(dy_dx))
        x_adv = tf.clip_by_value(x_adv, clip_min, clip_max)
        return x_adv, i+1

    x_adv, _ = tf.while_loop(_cond, _body, (x_adv, 0), back_prop=False,
                             name='fgsm')
    return x_adv
{'fraction_non_alphanumeric': 0.11214199273900767, 'fraction_numerical': 0.014925373134328358, 'mean_word_length': 2.532763532763533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2077236', 'n_tokens_mistral': 952, 'n_tokens_neox': 903, 'n_words': 216}