Course contentsShow
← Machine Learning and Deep Learning
Lesson 1480 of 3,53833. Generative Adversarial NetworksFree lesson

Implementing a Basic GAN in PyTorch

Building a simple GAN with fully connected networks for generating basic data like MNIST digits.

Implementing a Basic GAN in PyTorch

You'll learn how to code a simple GAN with fully connected networks to generate synthetic MNIST digits from scratch.

The Core Implementation

Now that you understand GAN theory, let's build one. A basic GAN uses two fully connected networks—one generator creating fake digits from noise, and one discriminator trying to distinguish real MNIST digits from fakes.

The Generator Network

The generator transforms random noise (say, 100-dimensional) into 784-dimensional outputs (28×28 flattened MNIST images):

import torch
import torch.nn as nn

class Generator(nn.Module):
    def __init__(self, latent_dim=100):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(latent_dim, 256),
            nn.ReLU(),
            nn.Linear(256, 512),
            nn.ReLU(),
            nn.Linear(512, 784),
            nn.Tanh()  # Output range [-1, 1]
        )
    
    def forward(self, z):
        return self.model(z)

The Discriminator Network

The discriminator takes 784-dimensional images and outputs a single probability (real or fake):

class Discriminator(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(784, 512),
            nn.LeakyReLU(0.2),
            nn.Linear(512, 256),
            nn.LeakyReLU(0.2),
            nn.Linear(256, 1),
            nn.Sigmoid()  # Probability output
        )
    
    def forward(self, img):
        return self.model(img)

Notice the Tanh output for the generator (matching normalized image range) and LeakyReLU for the discriminator (helps with gradient flow during the minimax game).

Key Takeaway: A basic GAN implementation requires just two fully connected networks—a generator expanding noise into images and a discriminator classifying them—each optimized with their own loss during alternating training steps.