§ 12. Generative Adversarial Networks¶
Minimax Optimization¶
Definition 12.1 : Minimax Optimization Problem
In a minimax optimization problem we minimize with respect to one variable and maximize with respect to another:
We say \(\left(\theta^{\star}, \phi^{\star}\right)\) is a solution to the minimax problem if \(\theta^{\star} \in \Theta, \phi^{\star} \in \Phi\), and
In other words, unilaterally deviating from \(\theta^{\star} \in \Theta\) increases the value of \(\mathcal{L}(\theta, \phi)\) while unilaterally deviating from \(\phi^{\star} \in \Phi\) decreases the value of \(\mathcal{L}(\theta, \phi)\). In yet other words, the solution is defined as a Nash equilibrium in a 2-player zero-sum game.
(There are other broader definitions of a “solution” in minimax optimization problems. Our definition is, in a sense, the strictest definition.)
Concept 12.2 : Minimax vs. Maximin
When a solution (as we defined in Definition 12.1) does not exist, then min-max is not the same as max-min:
This is a technical distinction that we will not explore in this class.
Concept 12.3 : Minimax Optimization
So far, we trained NN by solving minimization problems.
However, GANs are trained by solving minimax problems. Since the advent of GANs, minimax training has become more widely used in all areas of deep learning.
Examples:
- Adversarial training to make NN robust against adversarial attacks.
- Domain adversarial networks to train NN to make fair decisions (e.g. not base its decision on a persons race or gender).
Definition 12.4 : Minimax Optimization Algorithm
First, consider deterministic gradient setup. Let \(\alpha\) and \(\beta\) be the stepsizes (learning rates) for the descent and ascent steps respectively.
-
Simultaneous gradient ascent-descent
\[ \begin{aligned} \phi^{k+1} & =\phi^{k}+\beta \nabla_{\phi} \mathcal{L}\left(\theta^{k}, \phi^{k}\right) \\ \theta^{k+1} & =\theta^{k}-\alpha \nabla_{\theta} \mathcal{L}\left(\theta^{k}, \phi^{k}\right) \end{aligned} \] -
Alternating gradient ascent-descent
\[ \begin{aligned} \phi^{k+1} & =\phi^{k}+\beta \nabla_{\phi} \mathcal{L}\left(\theta^{k}, \phi^{k}\right) \\ \theta^{k+1} & =\theta^{k}-\alpha \nabla_{\theta} \mathcal{L}\left(\theta^{k}, \phi^{k+1}\right) \end{aligned} \] -
Gradient multi-ascent-single-descent
\[ \begin{aligned} \phi_{0}^{k+1} & =\phi_{n_{\mathrm{dis}}}^{k} \\ \phi_{i+1}^{k+1} & =\phi_{i}^{k+1}+\beta \nabla_{\phi} \mathcal{L}\left(\theta^{k}, \phi_{i}^{k+1}\right), \quad \text { for } i=0, \ldots, n_{\mathrm{dis}}-1 \\ \theta^{k+1} & =\theta^{k}-\alpha \nabla_{\theta} \mathcal{L}\left(\theta^{k}, \phi_{n_{\mathrm{dis}}}^{k+1}\right) \end{aligned} \](\(n_{\text {dis }}\) stands for number of discriminator updates.) When \(n_{\text {dis }}=1\), this algorithm reduces to alternating ascent-descent.
Definition 12.5 : Stochastic Minimax Optimization
In deep learning, however, we have access to stochastic gradients.
- Stochastic gradient simultaneous ascent-descent
- Stochastic gradient alternating ascent-descent
- Stochastic gradient multi-ascent-single-descent
Concept 12.6 : Minimax Optimization in Pytorch
To perform minimax optimization in PyTorch, we maintain two separate optimizers, one for the ascent, one for the descent.
The OPTIMIZER can be anything like SGD or Adam.
G = Generator(...).to(device)
D = Discriminator(...).to(device)
D_optimizer = optim.OPTIMIZER(D.parameters(), lr = beta)
G_optimizer = optim.OPTIMIZER(G.parameters(), lr = alpha)
-
Simultaneous ascent-descent:
-
Alternating ascent-descent
-
Multi-ascent-single-descent
Definition of GAN¶

These are synthetic (fake) images made with GAN.
(A. Brock, J. Donahue, and K. Simonyan, Large scale GAN training for high fidelity natural image synthesis, ICLR, 2019.)
Definition 12.7 : Generative Adversarial Networks (GAN)
In generative adversarial networks (GAN) a generator network and a discriminator network compete adversarially.

Given data \(X_{1}, \ldots, X_{N} \sim p_{\text {true }}\), GAN aims to learn \(p_{\theta} \approx p_{\text {true }}\).
Generator aims to generate fake data similar to training data.
Discriminator aims to distinguish the training data from fake data.
Analogy: Criminal creating fake money vs. police distinguishing fake money from real.
(I. J. Goodfellow, J. Pouget-Abadie, M. Mirza, B. Xu, D. Warde-Farley, S. Ozair, A. Courville, and Y. Bengio, Generative adversarial networks, NeurIPS, 2014.)
Definition 12.8 : Generator Network
The generator \(G_{\theta}: \mathbb{R}^{k} \rightarrow \mathbb{R}^{n}\) is a neural network parameterized by \(\theta \in \Theta\). The generator takes a random latent vector \(Z \sim p_{Z}\) as input and outputs generated (fake) data \(\tilde{X}=G_{\theta}(Z)\). The latent distribution is usually \(p_{Z}=\mathcal{N}(0, I)\).
Write \(p_{\theta}\) for the probability distribution of \(\tilde{X}=G_{\theta}(Z)\). Although we can't evaluate the density \(p_{\theta}(x)\), neither exactly nor approximately, we can sample from \(\tilde{X} \sim p_{\theta}\).
Definition 12.9 : Discriminator Network
The discriminator \(D_{\phi}: \mathbb{R}^{n} \rightarrow(0,1)\) is a neural network parameterized by \(\phi \in \Phi\). The discriminator takes an image \(X\) as input and outputs whether \(X\) is a real or fake. (Real : \(X\) comes from a data set, i.e., \(X \sim p_{\text{true}}\). Fake : generated by \(G_{\theta}\), i.e., \(X \sim p_{\theta}\).)
- \(D_{\phi}(X) \approx 1\) : discriminator confidently predicts \(X\) is real.
- \(D_{\phi}(X) \approx 0\) : discriminator confidently predicts \(X\) is fake.
- \(D_{\phi}(X) \approx 0.5\) : discriminator is unsure whether \(X\) is real or fake.
Concept 12.10 : Discriminator Loss
Cost of incorrectly classifying real as fake (type I error):
Cost of incorrectly classifying fake as real (type II error):
Discriminator solves
which is equivalent to
We can view
as an instance of the reparameterization technique.
The loss
puts equal weight on type I and type II errors. Alternatively, one can use the loss
where \(\lambda>0\) represents the relative significance of a type II error over a type I error.
Concept 12.11 : Generator Loss
Since the goal of the generator is to deceive the discriminator, the generator minimizes the same loss as Concept 12.10.
(The generator and discriminator operate under a zero-sum game.)
Note, only the second term depend on \(\theta\), while the both terms depend on \(\phi\).
Concept 12.12 : Empirical Risk Minimization for Discriminator / Generator
In practice, we have finite samples \(X_{1}, \ldots, X_{N}\), so we instead use the loss
Since \(\tilde{X}=G_{\theta}(Z)\) is generated with \(Z \sim p_{Z}\), we have unlimited \(\tilde{X}\) samples. So we replace \(\mathbb{E}_{X} \approx \frac{1}{N} \sum\) while leaving \(\mathbb{E}_{Z}\) as is.
Definition 12.13 : Minimax Training (Zero-Sum Game) for GAN
Train generator and discriminator simultaneously by solving
where
It remains to specify the architectures for \(G_{\theta}\) and \(D_{\phi}\).
The original GAN was also deep and convolutional. However, Radford et al.'s Deep Convolutional Generative Adversarial Networks (DCGAN) paper proposed the following architectures, which crucially utilize batchnorm.
Use batchnorm in both the generator and the discriminator after transposed conv and conv layers.

(A. Radford, L. Metz, and S. Chintala, Unsupervised representation learning with deep convolutional generative adversarial networks, ICLR, 2016.)
f-GAN¶
Definition 12.15 : f-Divergence
The f-divergence of \(p\) from \(q\), where \(f\) is a convex function such that \(f(1)=0\), is
This includes the KL divergence:
- If \(f(u)=u \log u\), then \(D_{f}(p \| q)=D_{\mathrm{KL}}(p \| q)\).
- If \(f(u)=-\log u\), then \(D_{f}(p \| q)=D_{\text {KL }}(q \| p)\).
Definition 12.16 : JS-Divergence
Jensen-Shannon-divergence (JS-divergence) is
With, \(f(u)=\left\{\begin{array}{ll}\frac{1}{2}\left(u \log u-(u+1) \log \frac{u+1}{2}\right) & \text { for } u \geq 0 \\ \infty & \text { otherwise }\end{array}\right.\) we have \(D_{f}=D_{\mathrm{JS}}\).
With, \(f(u)=\left\{\begin{array}{ll}u \log u-(u+1) \log (u+1)+\log 4 & \text { for } u \geq 0 \\ \infty & \text { otherwise }\end{array}\right.\) we have \(D_{f}=2 D_{\mathrm{JS}}\).
Concept 12.17 : GAN \(\approx\) JSD minimization
Let us understand the minimax problem
via the minimization problem
where
For simplicity, assume the discriminator is infinitely powerful, i.e., \(D_{\phi}(x)\) can represent any arbitrary function.
Note
Since
The integral is maximized by
If we plug in the optimal discriminator,
we get
Therefore,
Concept 12.18 : Motivation for f-GAN
With GANs, we started from a minimax formulation and later reinterpreted it as minimizing the JS-divergence. (Concept 12.17)
Let us instead the start from an f-divergence minimization
and then variationally approximate \(D_{f}\) to obtain a minimax formulation.
Variational approach: Evaluating \(D_{f}\) directly is difficult, so we pose it as a maximization problem and parameterize the maximizing function as a "discriminator" neural network.
For simplicity, however, we only consider the order
However, one can also consider
to obtain similar results.
Definition 12.19 : Convex Conjugate
Let \(f: \mathbb{R} \rightarrow \mathbb{R} \cup\{\infty\}\). Define the convex conjugate of \(f\) as
where \(f^{*}: \mathbb{R} \rightarrow \mathbb{R} \cup\{\infty\}\). This is also referred to as the Legendre transform.
If \(f\) is a nice (closed and proper) convex function, then \(f^{*}\) is convex and \(f^{* *}=f\), i.e., the conjugate of the conjugate is the original function. (So conjugacy is an involution in the space of convex functions.) So
Example 12.20 : Examples of Convex Conjugate
-
KL
\[ f_{\mathrm{KL}}(u)= \begin{cases} u \log u & \text { for } u \geq 0 \\ \infty & \text { otherwise } \\ \end{cases} \]\[ f_{\mathrm{KL}}^{*}(t)=\exp(t-1) \] -
LK (Reversed KL)
\[ f_{\mathrm{LK}}(u)= \begin{cases} - \log u & \text { for } u > 0 \\ \infty & \text { otherwise } \\ \end{cases} \]\[ f_{\mathrm{LK}}^{*}(u)= \begin{cases} -1-\log(-t) & \text { for } t < 0 \\ \infty & \text { otherwise } \\ \end{cases} \] -
SH (Squared Hellinger Distance)
\[ f_{\mathrm{SH}}(u)=(\sqrt{u}-1)^2 \]\[ f_{\mathrm{SH}}^{*}(t)= \begin{cases} \frac{1}{1/t-1} & \text { for } t < 1 \\ \infty & \text { otherwise } \\ \end{cases} \] -
JS
\[ f_{\mathrm{JS}}(u)= \begin{cases} u\log u-(u+1) \log (u+1)+\log 4 & \text { for } u \geq 0 \\ \infty & \text { otherwise } \\ \end{cases} \]\[ f_{\mathrm{JS}}^{*}(u)= \begin{cases} -\log (1-\exp (t))-\log 4 & \text { for } t < 0 \\ \infty & \text { otherwise } \\ \end{cases} \]
We get the following f-divergences:
We don't use the following property, but it's interesting so we mention it. If \(f\) and \(f^{*}\) are differentiable, then
Concept 12.21 : Variational Formulation of f-Divergence
Variational formulation of f-divergence:
where \(\mathcal{T}\) is the set of all measurable functions. In particular, \(\mathcal{T}\) contains \(T^{\star}(x)=\underset{t}{\operatorname{argmax}}\left\{t \frac{p(x)}{q(x)}-f^*(t)\right\}\) \(D_{\phi}\) is a neural network parameterized by \(\phi\).
Definition 12.22 : f-GAN Minimax Formulation
Minimax formulation of f-GANs.
Concept 21.23 : f-GAN with KL Divergence
Instantiate f-GAN with KL-divergence:
Step (*) uses the substitution \(D_{\phi} \mapsto D_{\phi}+1\), which is valid if the final layer of \(D_{\phi}\) has a trainable bias term. ( \(D_{\phi}: \mathbb{R}^{n} \rightarrow \mathbb{R}\).)
Note that most of the time, the convex conjugate \(f^{*}(t)\) has a constraint on it. When the constraint is violated, the \(f^{*}(t)=\infty\) case makes the maximization objective \(-\infty\). However, directly enforcing the neural networks to satisfy \(f^{*}(D_{\phi}\left(G_{\theta}(z)\right))<\infty\) is awkward.
Concept 21.24 : Resolving Infinity with Output Activation
When \(D_{\phi}: \mathbb{R}^{n} \rightarrow \mathbb{R}\) and \(\left\{t \mid f^{*}(t)<\infty\right\} \neq \mathbb{R}\), then \(f^{*}\left(D_{\phi}(\tilde{X})\right)=\infty\) is possible. To prevent this, substitute \(T(x) \mapsto \rho(\tilde{T}(x))\), where \(\rho: \mathbb{R} \rightarrow\left\{t \mid f^{*}(t)<\infty\right\}\) is a one-to-one function:
(\(*\)) We can restrict the search over \(T\) since if \(f^{*}(T(x))=\infty\), then the objective becomes \(-\infty\).
(\(**\)) With \(T=\rho \circ \tilde{T}\), have \(\left[T \in \mathcal{T}\right.\) and \(\left.f^{*}(T(x))<\infty\right] \Leftrightarrow[\tilde{T} \in \mathcal{T}]\) since \(\rho\) is one-to-one.
Definition 21.25 : f-GAN with Output Activation
Formulate f-GAN with output activation function \(\rho\) (\(\rho: \mathbb{R} \rightarrow\left\{t \mid f^{*}(t)<\infty\right\}\) is a one-to-one function) :
Concept 21.26 : f-GAN with Squared Hellinger
Instantiate f-GAN with squared Hellinger distance using \(\rho(r)=1-e^{-r}\) and
Note that \(f^{*}(\rho(r))=-1+e^{r}\).
Concept 21.27 : f-GAN with Reversed KL
Instantiate f-GAN with reverse KL using \(\rho(r)=-e^{r}\) and
Note that \(f^{*}(\rho(r))=-1-r\).
Concept 21.28 : f-GAN with JS (Recovering Standard GAN)
We recover standard GAN with
Note that \(\sigma\) is the familiar sigmoid and
where \(D_{\phi}: \mathbb{R}^{n} \rightarrow \mathbb{R}\).
(Standard GAN has \(D_{\phi}: \mathbb{R}^{n} \rightarrow(0,1)\). Here, \(\left(\sigma \circ D_{\phi}\right): \mathbb{R}^{n} \rightarrow(0,1)\) serves the same purpose.)
The Wasserstein GAN (WGAN) minimizes the Wasserstein distance:
The \(W(p, q)\) is a distance (metric) on probability distributions defined as
where the infimum is taken over joint probability distributions \(f\) with marginals \(p\) and \(q\), i.e.,
(M. Arjovsky, S. Chintala, and L. Bottou, Wasserstein GAN, ICML, 2017.)
Another equivalent formulation of the Wasserstein distance is by the theory of optimal transport. Given distributions \(p\) and \(q\) (initial and target)
where \(T\) is a transport plan that transports \(p\) to \(q\). Figuratively speaking, we are transporting grains of sand from one pile to another, and we wan to minimize the aggregate transport distance.
(Image from W. Li, E. K. Ryu, S. Osher, W. Yin, and W. Gangbo, A parallel method for earth mover’s distance, J. Sci. Comput., 2018.)
Kantorovich-Rubinstein duality \(^{\#}\) establishes:
Minimax formulation of WGAN:
(\(^{\#}\) L.V. Kantorovich and G. Rubinstein, On a space of completely additive functions, Vestnik Leningradskogo Universiteta, 1958.
The Kantorovich–Rubinstein dual as the convex (Lagrange) dual of a “flux” formulation of the optimal transport.)
Concept 21.30 : Spectral Normalization
How do we enforce the constraint that \(D_{\phi}\) is 1-Lipschitz? Consider an MLP:
where \(\sigma\) is a 1-Lipschitz continuous activation function, such as ReLU and tanh. If
for \(i=1, \ldots, L\), where \(\sigma_{\text {max }}\) denotes the largest singular value, then each layer is 1-Lipschitz continuous and the entire mapping \(x \mapsto y_{L}\) is 1 -Lipschitz. (A sufficient, but not a necessary, condition.)
Replace Lipschitz constraint with a singular-value constraint
Constraint is handled with a projected gradient method. (Note that \(A_{1}, \ldots, A_{L}\) are part of the discriminator parameters \(\phi\).)
(Specifically, one performs an (approximate) projection after the ascent step in the stochastic gradient ascent-descent methods. The approximate projection involves computing the largest singular with the power iteration.)
(T. Miyato, T. Kataoka, M. Koyama, and Y. Yoshida, Spectral normalization for generative adversarial networks, ICLR, 2018.)