Generative Adversarial Network (GAN)
This is a free course. Log in to run the code exercises and track your progress.
Generative Adversarial Network (GAN)
GANs are generative models that learn from existing data and then synthesize new, realistic samples. Ian Goodfellow introduced them in 2014, and they let machines produce content such as images, video, and music.
They matter because they:
- Produce new data that resembles real-world data
- Move past classification into content generation
- Appear in art, gaming, healthcare, and data science
Architecture of GAN
A GAN pairs two neural networks—the generator and the discriminator—trained in opposition. The generator tries to fool the discriminator, while the discriminator tries to tell real data from fake.
1. Generator Model
The generator is a deep network that consumes random noise and emits realistic samples such as images or text. During training it updates its parameters through backpropagation so it can capture underlying data patterns. Its goal is to create samples that the discriminator labels as real.
Generator Loss Function: The generator aims to minimize this loss:
\[ J_{G} = -\frac{1}{m} \Sigma^m _{i=1} log D(G(z_{i})) \]
where:
- J_G measures how successfully the generator fools the discriminator.
- G(z_i) is the sample produced from random noise z_i
- D(G(z_i)) is the discriminator’s estimated probability that the generated sample is real.
The generator wants to maximize D(G(z_i)), so the discriminator scores its fakes as real (probability near 1).
2. Discriminator Model
The discriminator is a binary classifier that separates real data from generated samples. Training updates its parameters so it gets better at spotting fakes; for images, convolutional layers extract features that improve classification accuracy.
Discriminator Loss Function: The discriminator aims to minimize this loss:
\[ J_{D} = -\frac{1}{m} \Sigma_{i=1}^m log\; D(x_{i}) - \frac{1}{m}\Sigma_{i=1}^m log(1 - D(G(z_{i})) \]
- J_D measures how well the discriminator separates real and fake samples.
- x_{i} is a real data sample.
- G(z_{i}) is a fake sample from the generator.
- D(x_{i}) is the discriminator’s probability that x_{i} is real.
- D(G(z_{i})) is the discriminator’s probability that the fake sample is real.
The discriminator seeks to classify real data correctly (maximize log D(x_{i})) and mark fakes as fake (maximize log(1 - D(G(z_{i})))).
MinMax Loss
GANs are trained with a MinMax Loss shared by the generator and discriminator:
\[ min_{G}\;max_{D}(G,D) = [\mathbb{E}_{x∼p_{data}}[log\;D(x)] + \mathbb{E}_{z∼p_{z}(z)}[log(1 - D(g(z)))] \]
where:
- G is the generator network and D is the discriminator network
- p_{data}(x) = true data distribution
- p_z(z) = distribution of random noise (usually normal or uniform)
- D(x) = discriminator’s estimate for real data
- D(G(z)) = discriminator’s estimate for generated data
The generator minimizes this objective (to fool the discriminator), while the discriminator maximizes it (to detect fakes accurately).
Working of GAN
Training proceeds as the Generator (G) and Discriminator (D) compete and improve together. The process unfolds as follows.
1. Generator's First Move
The generator begins with a random noise vector—essentially random numbers. That noise is transformed through its internal layers into a fake data sample, such as a synthetic image that resembles real data.
2. Discriminator's Turn
The discriminator sees two kinds of inputs:
- Real samples drawn from the training dataset.
- Fake samples produced by the generator.
D evaluates each input and decides whether it is genuine or created by G. It returns a probability between 0 and 1: values near 1 suggest real data, and values near 0 suggest a fake.
3. Adversarial Learning
- When the discriminator correctly separates real and fake samples, it strengthens at its task.
- When the generator fools the discriminator with realistic fakes, the generator receives a favorable update and the discriminator is penalized for the mistake.
4. Generator's Improvement
- Every time the discriminator mistakes fake data for real, the generator learns from that success.
- Across many iterations, the generator improves and produces more convincing samples.
5. Discriminator's Adaptation
- The discriminator keeps updating so it can detect fakes more reliably.
- This ongoing contest strengthens both networks over time.
6. Training Progression
- With continued training, the generator becomes skilled at producing realistic data.
- Ideally, generated samples become so convincing that the discriminator struggles to tell them apart from real data—though in practice GAN training can be unstable and may never fully reach that equilibrium.
- At that stage, the generator can emit high-quality synthetic data useful across many applications.
Types of GAN
Several GAN variants exist for different goals. Important types include:
1. Vanilla GAN
Vanilla GAN is the most basic form. It consists of:
- A generator and a discriminator, both built from multi-layer perceptrons (MLPs).
- Optimization of its mathematical objective with stochastic gradient descent (SGD).
- Potential for unstable training and limited variety in outputs
- Example: Generating random handwritten digits similar to the MNIST dataset
2. Conditional GAN (CGAN)
Conditional GAN (CGAN) adds a conditioning signal that steers generation. Instead of sampling freely, the model can produce specific kinds of outputs. How CGANs work:
- A conditional variable (y) is supplied to both the generator and the discriminator.
- That condition pushes the generator to create data matching the request (for example, images of particular objects).
- The discriminator also receives labels so it can better separate real from fake under that condition.
Example: Rather than any random image, a CGAN can produce a dog or a cat when given the matching label.
3. Deep Convolutional GAN (DCGAN)
Deep Convolutional GAN (DCGAN) is among the most widely used GAN families for image generation. Key traits:
- Relies on Convolutional Neural Networks (CNNs) instead of simple multi-layer perceptrons (MLPs).
- Replaces max pooling with strided convolutions for greater efficiency.
- Removes fully connected layers so the model retains stronger spatial understanding of images.
- Example: Generating realistic human faces or objects from random noise
4. Laplacian Pyramid GAN (LAPGAN)
Laplacian Pyramid GAN (LAPGAN) targets ultra-high-quality images with a multi-resolution strategy. How LAPGAN works:
- Deploys multiple generator–discriminator pairs at successive levels of the Laplacian pyramid.
- Images are downsampled at each pyramid level and then upscaled again with Conditional GAN (CGAN).
- Gradual refinement reduces noise and improves clarity.
- Example: Building high-resolution landscapes step by step from low-resolution images
5. Super Resolution GAN (SRGAN)
Super-Resolution GAN (SRGAN) raises the resolution of low-quality images while preserving detail. How SRGAN works:
- Combines a deep neural network with an adversarial loss.
- Adds finer detail to low-resolution inputs so they look sharper and more realistic.
- Helps reduce common upscaling artifacts such as blur and pixelation.
- Example: Turning a blurry image into a clear HD image
Implementation
Generative Adversarial Networks can synthesize realistic images by learning from existing image datasets. Below is a GAN setup trained on CIFAR-10 with PyTorch.
Step 1: Importing Required Libraries
This example uses Pytorch, Torchvision, Matplotlib, and Numpy. Prefer a GPU when one is available; otherwise fall back to the CPU.
Python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')