<commit_before>"""
This tutorial shows how to generate some simple adversarial examples
and train a model using adversarial training using nothing but pure
TensorFlow.
It is very similar to mnist_tutorial_tf.py, which does the same thing
but with a dependence on keras.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import numpy as np

import tensorflow as tf
from tensorflow.python.platform import app
from tensorflow.python.platform import flags

from cleverhans.utils_mnist import data_mnist
from cleverhans.utils_tf import model_train, model_eval
from cleverhans.attacks import FastGradientMethod
from cleverhans.utils import AccuracyReport
from cleverhans.model import Model

FLAGS = flags.FLAGS

"""
CleverHans is intended to supply attacks and defense, not models.
Users may apply CleverHans to many different kinds of models.
In this tutorial, we show you an example of the kind of model
you might build.
"""


class MLP(Model):
  """
  An example of a bare bones multilayer perceptron (MLP) class.
  """

  def __init__(self, layers, input_shape):
    super(MLP, self).__init__()

    self.layer_names = []
    self.layers = layers
    self.input_shape = input_shape
    for i,layer in enumerate(self.layers):
      name = layer.__class__.__name__
      self.layer_names.append(name+"_"+str(i))

      layer.set_input_shape(input_shape)
      input_shape = layer.get_output_shape()

  def get_layer_names(self):
    return self.layer_names

  def fprop(self, x, set_ref=False):
    if (x, self.state) in self.fprop_cache.keys():
      return self.fprop_cache[x, self.state]
    states = []
    for layer in self.layers:
      if set_ref:
        layer.ref = x
      x = layer.fprop(x)
      assert x is not None
      states.append(x)
    self.fprop_cache[x, self.state] = states
    return states

  def get_layer(self, x, layer):
    layers = self.fprop(x)
    return layers[self.get_layer_names().index(layer)]

  def get_logits(self, x):
    if isinstance(self.layers[-1], Softmax):
      idx = -2
    else:
      idx = -1
    return self.get_layer(x, self.get_layer_names()[idx])

  def get_probs(self, x):
    out = self.get_layer(x, self.get_layer_names()[-1])
    if not isinstance(self.layers[-1], Softmax):
      out = tf.nn.softmax(out)
    return out


class Layer(object):
  def get_output_shape(self):
    return self.output_shape


class Linear(Layer):

  def __init__(self, num_hid):
    self.num_hid = num_hid

  def set_input_shape(self, input_shape):
    batch_size, dim = input_shape
    self.input_shape = [batch_size, dim]<commit_msg>Make pure TF tutorial have fprop return dict<commit_after>"""
This tutorial shows how to generate some simple adversarial examples
and train a model using adversarial training using nothing but pure
TensorFlow.
It is very similar to mnist_tutorial_tf.py, which does the same thing
but with a dependence on keras.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import numpy as np

import tensorflow as tf
from tensorflow.python.platform import app
from tensorflow.python.platform import flags

from cleverhans.utils_mnist import data_mnist
from cleverhans.utils_tf import model_train, model_eval
from cleverhans.attacks import FastGradientMethod
from cleverhans.utils import AccuracyReport
from cleverhans.model import Model

FLAGS = flags.FLAGS

"""
CleverHans is intended to supply attacks and defense, not models.
Users may apply CleverHans to many different kinds of models.
In this tutorial, we show you an example of the kind of model
you might build.
"""


class MLP(Model):
  """
  An example of a bare bones multilayer perceptron (MLP) class.
  """

  def __init__(self, layers, input_shape):
    super(MLP, self).__init__()

    self.layer_names = []
    self.layers = layers
    self.input_shape = input_shape
    for i, layer in enumerate(self.layers):
      name = layer.__class__.__name__
      self.layer_names.append(name + "_" + str(i))

      layer.set_input_shape(input_shape)
      input_shape = layer.get_output_shape()

  def get_layer_names(self):
    return self.layer_names

  def fprop(self, x, set_ref=False):
    if (x, self.state) in self.fprop_cache.keys():
      return self.fprop_cache[x, self.state]
    states = []
    for layer in self.layers:
      if set_ref:
        layer.ref = x
      x = layer.fprop(x)
      assert x is not None
      states.append(x)
    states = dict(zip(self.get_layer_names(), states))
    self.fprop_cache[x, self.state] = states
    return states

  def get_layer(self, x, layer):
    layers = self.fprop(x)
    return layers[layer]

  def get_logits(self, x):
    if isinstance(self.layers[-1], Softmax):
      idx = -2
    else:
      idx = -1
    return self.get_layer(x, self.get_layer_names()[idx])

  def get_probs(self, x):
    out = self.get_layer(x, self.get_layer_names()[-1])
    if not isinstance(self.layers[-1], Softmax):
      out = tf.nn.softmax(out)
    return out


class Layer(object):
  def get_output_shape(self):
    return self.output_shape


class Linear(Layer):

  def __init__(self, num_hid):
    self.num_hid = num_hid

  def set_input_shape(self, input_shape):
    batch_size, dim = input_shape
    self.input_shape = [batch_size, dim]
{'fraction_non_alphanumeric': 0.08747919363787683, 'fraction_numerical': 0.0018494544109487702, 'mean_word_length': 3.743859649122807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3501091', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1694, 'n_words': 531}