From charlesreid1

 
(16 intermediate revisions by the same user not shown)
Line 3: Line 3:
Adversarial neural networks use an architecture consisting of two separate neural networks - one network attempts to learn how to accomplish a task, and another network attempts to differentiate between the output of the first network and the "real" output.
Adversarial neural networks use an architecture consisting of two separate neural networks - one network attempts to learn how to accomplish a task, and another network attempts to differentiate between the output of the first network and the "real" output.


=TensorFlow Examples of Adversarial Neural Networks=
=TensorFlow Adversarial Examples=


==Adversarial Crypto==
==Adversarial Crypto==
Line 9: Line 9:
This adversarial crypto neural network attempts to learn how to protect communications using the adversarial architecture.
This adversarial crypto neural network attempts to learn how to protect communications using the adversarial architecture.


Link to paper: "Learning to Protect Communications with Adversarial Neural Cryptography": https://arxiv.org/abs/1610.06918
Paper: "Learning to Protect Communications with Adversarial Neural Cryptography"
 
Link to paper: https://arxiv.org/abs/1610.06918


Link to code: https://github.com/tensorflow/models/tree/master/research/adversarial_crypto
Link to code: https://github.com/tensorflow/models/tree/master/research/adversarial_crypto
Line 25: Line 27:
The approach used by the training is to train the "defender" network (representing the Alice-Bob channel) until it is sufficiently well-trained, then reset the "attacker" network (representing the eavesdropper Eve) from scratch to give the eavesdropper multiple opportunities to find weaknesses in the cryptosystem.
The approach used by the training is to train the "defender" network (representing the Alice-Bob channel) until it is sufficiently well-trained, then reset the "attacker" network (representing the eavesdropper Eve) from scratch to give the eavesdropper multiple opportunities to find weaknesses in the cryptosystem.


===The Network===
===The Model===
 
We'll step through the code line-by-line. Here's the link to the code: https://github.com/tensorflow/models/blob/master/research/adversarial_crypto/train_eval.py
 
Full model walkthrough is on the [[TensorFlow/Adversarial Crypto]] page.
 
The rundown is:
* Create an AdversarialCrypto class that holds a training optimizer object for the Bob and Alice networks
* Define a method that evaluates the networks as-is and prints the percent losses
* Define a method that trains the network for a specified number of iterations, stopping early if the network reaches its target losses
* Define a method that calls the training function (above), then re-trains Eve several more times from scratch
 
==Adversarial Text==
 
This trains a neural network model to detect the sentiment in IMDB text. This illustrates semi-supervised learning.
 
Link to code: https://github.com/tensorflow/models/tree/master/research/adversarial_text
 
==Running==
 
Running this model is slightly more complicated than running the adversarial crypto network.


We'll step through the code line-by-line again. Here's the link to the code: https://github.com/tensorflow/models/blob/master/research/adversarial_crypto/train_eval.py
The adversarial text network steps are as follows:
* fetch data
* generate vocab
* generate training/validation/test data
* pretrain language model
* train classifier
* evaluate classifier on test data


====License====
===Get Vocabulary Data===


Obligatory license info:
Start by obtaining the data, which is an 80 MB tar file, and decompress it:


<pre>
<pre>
# Copyright 2016 The TensorFlow Authors All Rights Reserved.
$ wget http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz -O /tmp/imdb.tar.gz
#
 
# Licensed under the Apache License, Version 2.0 (the "License");
$ tar -xf /tmp/imdb.tar.gz -C /tmp
# you may not use this file except in compliance with the License.
 
# You may obtain a copy of the License at
$ du -hs /tmp/aclImdb
#
487M /tmp/aclImdb
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
</pre>
</pre>


Some info about the network:
===Build the Vocabulary===
* There are actually 3 neural networks involved: Alice, Bob, and Eve
* Alice takes inputs in_m (message), in_k (key) and outputs the ciphertext
* Bob takes inputs in_k (key), ciphertext and attempts to output the plaintext
* Even takes input ciphertext (no key) and also attempts to output the plaintext


The file starts with imports/declarations to be compatible with Python 3:
Use a Bazel job to build the vocabulary from the data:


<pre>
<pre>
# TensorFlow Python 3 compatibility
$ IMDB_DATA_DIR=/tmp/imdb
from __future__ import absolute_import
 
from __future__ import division
$ bazel run data:gen_vocab -- \
from __future__ import print_function
    --output_dir=$IMDB_DATA_DIR \
import signal
    --dataset=imdb \
import sys
    --imdb_input_dir=/tmp/aclImdb \
from six.moves import xrange  # pylint: disable=redefined-builtin
    --lowercase=False
import tensorflow as tf
</pre>
</pre>


====Input Argument Flags====
This uses a build rule called <code>gen_vocab</code> located in <code>data/BUILD</code>:
 
Hyperparameter flags can be set on the command line:


<pre>
<pre>
flags = tf.app.flags
py_binary(
flags.DEFINE_float('learning_rate', 0.0008, 'Constant learning rate')
    name = "gen_vocab",
flags.DEFINE_integer('batch_size', 4096, 'Batch size')
    srcs = ["gen_vocab.py"],
FLAGS = flags.FLAGS
    deps = [
        ":data_utils",
        ":document_generators",
        # tensorflow dep,
    ],
)
</pre>
</pre>


The FLAGS stuff does not seem to be defined anywhere in the documentation, so the usage is not clear here. But, as an author on TF project states [https://stackoverflow.com/questions/33932901/whats-the-purpose-of-tf-app-flags-in-tensorflow#33938519 here], it is intended to make demos more convenient, and essentially wraps argparse.
This build vocabulary step is, unfortunately, failing. See this Github issue (1917): https://github.com/tensorflow/models/issues/1917
 
Also see [[TensorFlow/Command Line Args]].


==Adversarial Text==
==Adversarial Image Network==


=Flags=
=Flags=

Latest revision as of 00:13, 27 October 2017

Adversarial Neural Networks

Adversarial neural networks use an architecture consisting of two separate neural networks - one network attempts to learn how to accomplish a task, and another network attempts to differentiate between the output of the first network and the "real" output.

TensorFlow Adversarial Examples

Adversarial Crypto

This adversarial crypto neural network attempts to learn how to protect communications using the adversarial architecture.

Paper: "Learning to Protect Communications with Adversarial Neural Cryptography"

Link to paper: https://arxiv.org/abs/1610.06918

Link to code: https://github.com/tensorflow/models/tree/master/research/adversarial_crypto

Part of the tensorflow models repository (https://github.com/tensorflow/models/tree/master/research).

Running

To train the network:

$ python train_eval.py

The approach used by the training is to train the "defender" network (representing the Alice-Bob channel) until it is sufficiently well-trained, then reset the "attacker" network (representing the eavesdropper Eve) from scratch to give the eavesdropper multiple opportunities to find weaknesses in the cryptosystem.

The Model

We'll step through the code line-by-line. Here's the link to the code: https://github.com/tensorflow/models/blob/master/research/adversarial_crypto/train_eval.py

Full model walkthrough is on the TensorFlow/Adversarial Crypto page.

The rundown is:

  • Create an AdversarialCrypto class that holds a training optimizer object for the Bob and Alice networks
  • Define a method that evaluates the networks as-is and prints the percent losses
  • Define a method that trains the network for a specified number of iterations, stopping early if the network reaches its target losses
  • Define a method that calls the training function (above), then re-trains Eve several more times from scratch

Adversarial Text

This trains a neural network model to detect the sentiment in IMDB text. This illustrates semi-supervised learning.

Link to code: https://github.com/tensorflow/models/tree/master/research/adversarial_text

Running

Running this model is slightly more complicated than running the adversarial crypto network.

The adversarial text network steps are as follows:

  • fetch data
  • generate vocab
  • generate training/validation/test data
  • pretrain language model
  • train classifier
  • evaluate classifier on test data

Get Vocabulary Data

Start by obtaining the data, which is an 80 MB tar file, and decompress it:

$ wget http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz -O /tmp/imdb.tar.gz

$ tar -xf /tmp/imdb.tar.gz -C /tmp

$ du -hs /tmp/aclImdb
487M	/tmp/aclImdb

Build the Vocabulary

Use a Bazel job to build the vocabulary from the data:

$ IMDB_DATA_DIR=/tmp/imdb

$ bazel run data:gen_vocab -- \
    --output_dir=$IMDB_DATA_DIR \
    --dataset=imdb \
    --imdb_input_dir=/tmp/aclImdb \
    --lowercase=False

This uses a build rule called gen_vocab located in data/BUILD:

py_binary(
    name = "gen_vocab",
    srcs = ["gen_vocab.py"],
    deps = [
        ":data_utils",
        ":document_generators",
        # tensorflow dep,
    ],
)

This build vocabulary step is, unfortunately, failing. See this Github issue (1917): https://github.com/tensorflow/models/issues/1917

Adversarial Image Network

Flags