← Back to home
Jun 21, 2026MR. ERROR 3133 min read

GAN Tutorial

Generative Adversarial Networks (GANs) are a type of deep learning model. They consist of two neural networks that compete with each other to generate new data.

machine-learningpytorchgan
GAN Tutorial

Introduction to Generative Adversarial Networks

Generative Adversarial Networks (GANs) are a type of deep learning model that have gained significant attention in recent years due to their ability to generate new, synthetic data that is similar to existing data. They consist of two neural networks: a generator network and a discriminator network. The generator network takes a random noise vector as input and produces a synthetic data sample that aims to be similar to the real data. The discriminator network takes a data sample (either real or synthetic) as input and outputs a probability that the sample is real.

Context and Importance

GANs have numerous applications in various fields such as computer vision, natural language processing, and robotics. They can be used for data augmentation, style transfer, image-to-image translation, and many other tasks. The ability of GANs to generate new data makes them particularly useful when there is a lack of existing data.

Core Concept

The core concept of GANs is based on a two-player game framework. The generator network tries to produce synthetic data that is indistinguishable from real data, while the discriminator network tries to correctly distinguish between real and synthetic data. The two networks are trained simultaneously, with the generator trying to maximize the probability of the discriminator making a mistake, and the discriminator trying to minimize the probability of making a mistake.

Math Behind GANs

The math behind GANs is based on the concept of minimax optimization. The generator network tries to maximize the probability of the discriminator making a mistake, while the discriminator network tries to minimize the probability of making a mistake. This can be represented mathematically as:

import torch
import torch.nn as nn

# Define the generator and discriminator networks
class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        self.fc1 = nn.Linear(100, 128)  # input layer (100) -> hidden layer (128)
        self.fc2 = nn.Linear(128, 784)  # hidden layer (128) -> output layer (784)

    def forward(self, x):
        x = torch.relu(self.fc1(x))  # activation function for hidden layer
        x = torch.sigmoid(self.fc2(x))  # activation function for output layer
        return x

class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()
        self.fc1 = nn.Linear(784, 128)  # input layer (784) -> hidden layer (128)
        self.fc2 = nn.Linear(128, 1)  # hidden layer (128) -> output layer (1)

    def forward(self, x):
        x = torch.relu(self.fc1(x))  # activation function for hidden layer
        x = torch.sigmoid(self.fc2(x))  # activation function for output layer
        return x

# Define the loss functions and optimizers
criterion = nn.BCELoss()
optimizer_g = torch.optim.Adam(generator.parameters(), lr=0.001)
optimizer_d = torch.optim.Adam(discriminator.parameters(), lr=0.001)

# Train the GAN
for epoch in range(100):
    for x in dataset:
        # Train the discriminator
        optimizer_d.zero_grad()
        output = discriminator(x)
        loss_d = criterion(output, torch.ones_like(output))
        loss_d.backward()
        optimizer_d.step()

        # Train the generator
        optimizer_g.zero_grad()
        z = torch.randn(1, 100)
        output = generator(z)
        loss_g = criterion(discriminator(output), torch.ones_like(discriminator(output)))
        loss_g.backward()
        optimizer_g.step()

Worked Example

In this example, we will use the PyTorch library to implement a simple GAN that generates handwritten digits. We will use the MNIST dataset, which consists of 60,000 training images and 10,000 testing images.

Pitfalls

One of the main pitfalls of training GANs is mode collapse, where the generator produces limited variations of the same output. This can be addressed by using techniques such as batch normalization, dropout, and regularization.

What to Read Next

For further reading, we recommend the original GAN paper by Goodfellow et al. [1], as well as the Deep Learning book by Goodfellow et al. [2]. Additionally, the PyTorch documentation provides a comprehensive guide to implementing GANs [3].

References: [1] Goodfellow, I., Pouget-Abadie, J., Mirza, M., Xu, B., Warde-Farley, D., Ozair, S., ... & Bengio, Y. (2014). Generative adversarial nets. In Advances in neural information processing systems (pp. 2672-2680). [2] Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep learning. MIT Press. [3] PyTorch. (2022). Generative Adversarial Networks (GANs). Retrieved from https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html