# Generative Adversarial Networks

> A generator and a discriminator trained against each other; powerful for sharp images and a lesson in unstable optimization. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/gans
Last updated: 2026-11-28

A generator and a discriminator trained against each other; powerful for sharp images and a lesson in unstable optimization.

Advanced · about 8 hours · difficulty 4/5

Learn first: [Autoencoders and Variational Autoencoders](https://www.internhack.in/roadmaps/tracks/machine-learning/autoencoders-vaes)

A **GAN** sets up a game. The **generator** turns random noise into fake samples; the **discriminator** tries to tell real from fake. Each improves by exploiting the other's weaknesses, and at equilibrium the generator produces samples the discriminator cannot distinguish from real data.

### Training loop

1. Sample real data and generate fakes from noise.
2. Update the discriminator to label real as 1 and fake as 0.
3. Update the generator so the discriminator labels its fakes as real.

### Why GANs are hard

- **Mode collapse**: the generator finds a few outputs that fool the discriminator and stops producing variety.
- **Non-convergence**: the two networks oscillate instead of settling.
- **Balance**: if the discriminator gets too good, the generator's gradients vanish.

Fixes that became standard include the **DCGAN** architecture guidelines, **Wasserstein loss with gradient penalty**, spectral normalization and careful learning rates for each network.

### Notable variants

Conditional GANs generate a requested class; Pix2Pix and CycleGAN translate images between domains; StyleGAN produces high-resolution faces with controllable style.

### Evaluating generators

Visual inspection is essential but biased. **FID** (Frechet Inception Distance) compares statistics of real and generated images and is the common benchmark; lower is better.

Diffusion models have largely overtaken GANs for image quality, but GANs remain fast at sampling and appear in super-resolution and real-time applications.

**Good enough to move on** when you can train a DCGAN on a small dataset, recognize mode collapse in its samples, and describe one fix.

## Key concepts

- **Generator and discriminator**
- **Adversarial training loop**
- **Mode collapse**
- **DCGAN architecture**
- **Wasserstein loss and gradient penalty**
- **Conditional GANs and CycleGAN**
- **FID evaluation**

## Mini project

Train a DCGAN on a small image dataset, save a sample grid every epoch into an animation, and compare results with and without label smoothing or spectral normalization.

## Check yourself

What is mode collapse, how would you notice it, and why can an overly strong discriminator stop the generator from learning?

## Free resources

- [Google: Generative Adversarial Networks](https://developers.google.com/machine-learning/gan) (Google)
- [PyTorch tutorials](https://pytorch.org/tutorials/) (PyTorch)

Previous: [Autoencoders and Variational Autoencoders](https://www.internhack.in/roadmaps/tracks/machine-learning/autoencoders-vaes) · Next: [Diffusion Models](https://www.internhack.in/roadmaps/tracks/machine-learning/diffusion-models)

## More in Generative Models

- [Autoencoders and Variational Autoencoders](https://www.internhack.in/roadmaps/tracks/machine-learning/autoencoders-vaes)
- [Generative Adversarial Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/gans)
- [Diffusion Models](https://www.internhack.in/roadmaps/tracks/machine-learning/diffusion-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
