# Conditional Generative Adversarial Nets in TensorFlow

We have seen the Generative Adversarial Nets (GAN) model in the previous post. We have also seen the arch nemesis of GAN, the VAE and its conditional variation: Conditional VAE (CVAE). Hence, it is only proper for us to study conditional variation of GAN, called Conditional GAN or CGAN for short.

## CGAN: Formulation and Architecture

Recall, in GAN, we have two neural nets: the generator \( G(z) \) and the discriminator \( D(X) \). Now, as we want to condition those networks with some vector \( y \), the easiest way to do it is to feed \( y \) into both networks. Hence, our generator and discriminator are now \( G(z, y) \) and \( D(X, y) \) respectively.

We can see it with a probabilistic point of view. \( G(z, y) \) is modeling the distribution of our data, given \( z \) and \( y \), that is, our data is generated with this scheme \( X \sim G(X \, \vert \, z, y) \).

Likewise for the discriminator, now it tries to find discriminating label for \( X \) and \( X_G \), that are modeled with \( d \sim D(d \, \vert \, X, y) \).

Hence, we could see that both \( D \) and \( G \) is jointly conditioned to two variables \( z \) or \( X \) and \( y \).

Now, the objective function is given by:

\[\min_G \max_D V(D, G) = \mathop{\mathbb{E}}_{x \sim p_{data}(x)} [\log D(x, y)] + \mathop{\mathbb{E}}_{z \sim p_z(z)} [\log(1 - D(G(z, y), y))]\]If we compare the above loss to GAN loss, the difference only lies in the additional parameter \( y \) in both \( D \) and \( G \).

The architecture of CGAN is now as follows (taken from [1]):

In contrast with the architecture of GAN, we now has an additional input layer in both discriminator net and generator net.

## CGAN: Implementation in TensorFlow

I’d like to direct the reader to the previous post about GAN, particularly for the implementation in TensorFlow. Implementing CGAN is so simple that we just need to add a handful of lines to the original GAN implementation. So, here we will only look at those modifications.

The first additional code for CGAN is here:

We are adding new input to hold our variable we are conditioning our CGAN to.

Next, we add it to both our generator net and discriminator net:

The problem we have here is how to incorporate the new variable \( y \) into \( D(X) \) and \( G(z) \). As we are trying to model the joint conditional, the simplest way to do it is to just concatenate both variables. Hence, in \( G(z, y) \), we are concatenating \( z \) and \( y \) before we feed it into the networks. The same procedure is applied to \( D(X, y) \).

Of course, as our inputs for \( D(X, y) \) and \( G(z, y) \) is now different than the original GAN, we need to modify our weights:

That is, we just adjust the dimensionality of our weights.

Next, we just use our new networks:

And finally, when training, we also feed the value of \( y \) into the networks:

As an example above, we are training our GAN with MNIST data, and the conditional variable \( y \) is the labels.

## CGAN: Results

At test time, we want to generate new data samples with certain label. For example, we set the label to be 5, i.e. we want to generate digit “5”:

Above, we just sample \( z \), and then construct the conditional variables. In our example case, the conditional variables is a collection of one-hot vectors with value 1 in the 5th index. The last thing we need to is to run the network with those variables as inputs.

Here is the results:

Looks pretty much like digit 5, right?

If we set our one-hot vectors to have value of 1 in the 7th index:

Those results confirmed that have successfully trained our CGAN.

## Conclusion

In this post, we looked at the analogue of CVAE for GAN: the Conditional GAN (CGAN). We show that to make GAN into CGAN, we just need a little modifications to our GAN implementation.

The conditional variables for CGAN, just like CVAE, could be anything. Hence it makes CGAN an interesting model to work with for data modeling.

The full code is available at my GitHub repo: https://github.com/wiseodd/generative-models.

## References

- Mirza, Mehdi, and Simon Osindero. “Conditional generative adversarial nets.” arXiv preprint arXiv:1411.1784 (2014).