05 / Generative models · Unsupervised learning
Generative adversarial networks
A PyTorch GAN that turns random noise into 28 × 28 digit images, with a custom training loop and loss tracking.
The question
How can a model learn to generate samples by competing with another model?
The approach
I implemented a fully connected GAN in PyTorch using MNIST. A generator maps a 100-value random noise vector to a 28 × 28 image. A discriminator receives real or generated images and predicts whether they came from the dataset. The training loop alternates between updating the two networks.
Project scope
- Generator & discriminator in PyTorch
- MNIST training with Adam and BCE loss
- Sample grids & training-loss tracking
How it works
Start with noise
The generator expands a 100-dimensional noise vector through layers of 256, 512 and 1,024 units. LeakyReLU activations sit between the hidden layers, and a Tanh output produces the 784 values of a grayscale digit image.
Train both sides
The discriminator reduces a flattened image through layers of 1,024, 512 and 256 units to a real-or-generated probability. Both networks use Adam optimisers and binary cross-entropy loss. The notebook configures 100 epochs with batches of 100 and a learning rate of 0.0002.
Watch the training process
MNIST images are normalised before training. The loop records each network's loss and the time per epoch, while the sample-grid helper makes it possible to inspect the generated images as training progresses.
The build
Inside the notebook
The public notebook contains the network definitions, data loading, training loop and recorded training logs. It also includes code to save a 5 × 5 grid of generated samples after each epoch and plot the generator and discriminator losses. The experiment focuses on how adversarial training works.
Explore the implementationNext project / 01