Post · 11 min read· pp. 14–25

Generative Adversarial Nets in TensorFlow (Part I)

This post was first published on 12/29/15, and has since been migrated to Blogger.

This is a tutorial on implementing Ian Goodfellow's Generative Adversarial Nets paper in TensorFlow. Adversarial Nets are a fun little Deep Learning exercise that can be done in ~80 lines of Python code, and exposes you (the reader) to an active area of deep learning research (as of 2015): Generative Modeling!

Code on Github

Scenario: Counterfeit Money

To help explain the motivations behind the paper, here's a hypothetical scenario: Danielle is a teller at a bank, and one of her job responsibilities is to discriminate between real money and counterfeit money. George is a crook and is trying to make some counterfeit money, becase free money would be pretty radical.

Let's simplify things a bit and assume the only distinguishing feature of currency is one unique number, XX, printed on the each bill. These numbers are randomly sampled from a probability distribution, whose density function pdatap_{data} is only known to the Treasury (i.e. neither Danielle nor George know the function). For convenience, this tutorial uses pdatap_{data} to refer to both the distribution and the density function (though semantically a distribution and its density function are not the same).

George's goal is to generate samples x′x^\prime from pdatap_{data}, so his counterfeit currency is indistinguishable from "real" currency. You might ask: how can George generate samples from pdatap_{data} if he doesn't know pdatap_{data} in the first place?

We can create computationally indistinguishable samples without understanding the "true" underlying generative process [1]. The underlying generative process is the method that the Treasury itself is using to generate samples of XX - perhaps some efficient algorithm for sampling pdatap_{data} that relies on the analytical formula for the pdf.

We can think of this algorithm as the "natural (function) basis", the straightforward method the Treasury would actually use to print our hypothetical currency. However, a (continuous) function can be represented as a combination of a different set of basis functions; George can express the same sampler algorithm in a "neural network basis" or "Fourier basis" or other basis that can be used to build a universal approximator. From an outsider's perspective, the samplers are computationally indistinguishable, and yet George's model doesn't reveal to him the structure of the "natural" sampler basis or the analytical formula of pdatap_{data}.

Background: Discriminative vs. Generative Models

Let XX, YY be the "observed" and "target" random variables. The joint distribution for XX and YY is P(X,Y)P(X,Y), which we can think of as a probability density over 2 (possibly dependent) variables.

A Discriminative model allows us to evaluate the conditional probability P(Y∣X)P(Y|X). For instance, given a vector of pixel values xx, what is the probability that Y=6Y=6? (where "6" corresponds to the categorical class label for "tabby cat"). MNIST LeNet, AlexNet, and other classifiers are examples of a discriminative models.

 (image unavailable)

On the other hand, a Generative model can allows us to evaluate the joint probability P(X,Y)P(X,Y). This means that we can propose value pairs (X,Y)(X,Y) and do rejection-sampling to obtain samples xx,yy from P(X,Y)P(X,Y). Another way to put this is that with the right generative model, we can convert a random number from [0,1][0,1] into a picture of a rabbit. That's awesome.

Of course, generative models are much harder to construct than discriminative models, and both are active areas of research in statistics and machine learning.

Generative Adversarial Networks

Goodfellow's paper proposes a very elegant way to teach neural networks a generative model for any (continuous) probability density function. We build two neural networks DD (Danielle) and GG (George), and have them play an adversarial cat-and-mouse game: GG is a generator and attempts to counterfeit samples from pdatap_{data} and DD is a decider that tries to not get fooled. We train them simultaneously, so that they both improve by competing with each other. At convergence, we hope that GG has learned to sample exactly from pdatap_{data}, at which point D(x)=0.5D(x)=0.5 (random guessing).

Advesarial Nets have been used to great success to synthesize the following from thin air:

Cat Faces

Churches

Anime Characters

In this tutorial we won't be doing anything nearly as amazing, but hopefully you'll come away with a better fundamental understanding of adversarial nets.

Implementation

We'll be training a neural network to sample from the simple 1-D normal distribution N(−1,1)\mathcal{N}(-1,1)

Let DD,GG be small 3-layer perceptrons, each with a meager 11 hidden units in total. GG takes as input a single sample of a noise distribution: z∼uniform(0,1)z \sim \text{uniform}(0,1). We want GG to map points z1,z2,...zMz_1,z_2,...z_M to x1,x2,...xMx_1,x_2,...x_M, in such a way that mapped points xi=G(zi)x_i=G(z_i) cluster densely where pdata(X)p_{data}(X) is dense. Thus, G takes in zz and generates fake data x′x^\prime.

Meanwhile, the discriminator DD, takes in input xx and outputs a likelihood of the input belonging to pdatap_{data}. Let D1D_1 and D2D_2 be copies of DD (they share the same parameters so D1(x)=D2(x)D_1(x)=D_2(x)). The input to D1D_1 is a single sample of the legitimate data distribution: x∼pdatax \sim p_{data}, so when optimizing the decider we want the quantity D1(x)D_1(x) to be maximized. D2D_2 takes as input x′x^\prime (the fake data generated by GG), so when optimizing DD we want to D2(x′)D_2(x^\prime) to be minimized. The value function for DD is:

log⁡(D1(x))+log⁡(1−D2(G(z)))\log(D_1(x))+\log(1-D_2(G(z)))

Here's the Python code:

text
batch=tf.Variable(0)
obj_d=tf.reduce_mean(tf.log(D1)+tf.log(1-D2))
opt_d=tf.train.GradientDescentOptimizer(0.01)
              .minimize(1-obj_d,global_step=batch,var_list=theta_d)

The reason we go through the trouble of specifying copies D1D_1 and D2D_2 is that in TensorFlow, we need one copy of DD to process the input xx and another copy to process the input G(z)G(z); the same section of the computational graph can't be re-used for different inputs. When optimizing GG, we want the quantity D2(X′)D_2(X^\prime) to be maximized (successfully fooling DD). The value function for GG is:

log⁡(D2(G(z)))\log(D_2(G(z)))

text
batch=tf.Variable(0)
obj_g=tf.reduce_mean(tf.log(D2))
opt_g=tf.train.GradientDescentOptimizer(0.01)
              .minimize(1-obj_g,global_step=batch,var_list=theta_g)

Instead of optimizing with one pair (x,z)(x,z) at a time, we update the gradient based on the average of MM loss gradients computed for MM different (x,z)(x,z) pairs. The stochastic gradient estimated from a minibatch is closer to the true gradient across the training data. The training loop is straightforward:

text
# Algorithm 1, GoodFellow et al. 2014
for i in range(TRAIN_ITERS):
    x= np.random.normal(mu,sigma,M) # sample minibatch from p_data
    z= np.random.random(M)  # sample minibatch from noise prior
    sess.run(opt_d, {x_node: x, z_node: z}) # update discriminator D
    z= np.random.random(M) # sample noise prior
    sess.run(opt_g, {z_node: z}) # update generator G

Manifold Alignment

Following the above recipe naively will not lead to good results, because we are sampling pdatap_{data} and uniform(0,1)\text{uniform}(0,1) independently each iteration. Nothing is enforcing that adjacent points in the ZZ domain are being mapped to adjacent points in the XX domain; in one minibatch, we might train GG to map $0.501 \to -1.1$, $0.502 \to 0.01$, and $0.503 \to -1.11$. The mapping arrows cross each other too much, making the transformation very bumpy. What's worse, the next minibatch might map $0.5015 \to 1.1$, $0.5025 \to -1.1$, and $0.504 \to 1.01$. This implies a completely different mapping GG from the previous minibatch, so the optimizer will fail to converge.

To remedy this, we want to minimize the total length of the arrows taking points from ZZ to XX, because this will make the transformation as smooth as possible and easier to learn. Another way of saying this is that the "vector bundles" carrying ZZ to XX should be correlated between minibatches.

First, we'll stretch the domain of ZZ to the same size of XX. The normal distribution centered at -1 has most of its probability mass lying between [−5,5][-5,5], so we should sample ZZ from uniform(−5,5)\text{uniform}(-5,5). Doing this means GG no longer needs to learn how to "stretch" the domain [0,1][0,1] by a factor of 10. The less GG has to learn, the better.

Next, we'll align the samples of ZZ and XX within a minibatch by sorting them both from lowest to highest.

Instead of sampling ZZ via np.random.random(M).sort(), we'll use via stratified sampling - we generate MM equally spaced points along the domain and then jitter the points randomly. This preserves sorted order and also increases the representativeness the entire training space. We then match our stratified, sorted ZZ samples to our sorted XX samples.

Of course, for higher dimensional problems it's not so straightforward to align the input space ZZ with the target space XX, since sorting points doesn't really make sense in 2D and higher. However, the notion of minimizing the transformation distance between the ZZ and XX manifolds still holds [2].

The modified algorithm is as follows:

text
for i in range(TRAIN_ITERS):
    x= np.random.normal(mu,sigma,M).sort()
    z= np.linspace(-5.,5.,M)+np.random.random(M)*.01 # stratified
    sess.run(opt_d, {x_node: x, z_node: z})
    z= np.linspace(-5.,5.,M)+np.random.random(M)*.01
    sess.run(opt_g, {z_node: z})

This step was crucial for me to get this example working: when dealing with random noise as input, failing to align the transformation map properly will give rise to a host of other problems, like massive gradients that kill ReLU units early on, plateaus in the objective function, or performance not scaling with minibatch size.

Pretraining D

The original algorithm runs kk steps of gradient descent on DD for every step of gradient descent on GG. I found it more helpful to pre-train DD a larger number of steps prior to running the adversarial net, using a mean-square error (MSE) loss function to fit DD to pdatap_{data}. This loss function is nicer to optimize than the log-likelihood objective function for DD (since the latter has to deal with stuff from GG). It's easy to see that pdatap_{data} is the optimal likelihood decision surface for its own distribution.

Here is the decision boundary at initialization.

After pretraining:

Close enough, lol.

Other Troubleshooting Comments

  • Using too many parameters in the model often leads to overfitting, but in my case making the network too big failed to even converge under the minimax objective - the network units saturated too quickly from large gradients. Start with a shallow, tiny network and only add extra units/layers if you feel that the size increase is absolutely necessary.
  • I started out using ReLU units but the units kept saturating (possibly due to manifold alignment issues). The Tanh activation function seemed to work better.
  • I also had to tweak the learning rate a bit to get good results.

Results

Before training, here is pdatap_{data}, the pre-trained decision surface of DD, and the generative distribution pgp_g. .  (image unavailable)

Here's the loss function as a function of training iterations.  (image unavailable)

After training, pgp_g approximates pdatap_{data}, and the discriminator is about uniformly confused (D=0.5D=0.5) for all values of XX.

And there you have it! GG has learned how to sample (approximately) from pdatap_{data} to the extent that DD can no longer separate the "reals" from the forgeries.

Footnotes

  1. Here's a more vivid example of computational indistinguishability: suppose we train a super-powerful neural network to sample from the distribution of cat faces. The underlying (generative) data distribution for "true" cat faces involves 1) a cat being born and 2) somebody eventually taking a photograph of the cat. Clearly, our neural network isn't learning this particular generative process, as no real cats are involved. However, if our neural network can generate pictures that we cannot distinguish from "authentic" cat photos (armed with polynomial-time computational resources), then in some ways these photos are "just as legitimate" as regular cat photos. This is worth pondering over, in the context of the Turing Test, Cryptography, and fake Rolexes.
  2. Excessive mapping crossover can be viewed from the perspective of "overfitting", where the learned regression/classification function has been distorted in "contradictory" ways by data samples (for instance, a picture of a cat classified as a dog). Regularization techniques implicitly discourage excessive "mapping crossover", but do not explicitly implement a sorting algorithm to ensure the learned transformation is continuous / aligned in XX-space with respect to ZZ-space. Such a sorting mechanism might be very useful for improving training speeds...