Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

GANs and VAEs: Generating and Comparing Synthetic Images

Hello! Welcome to the final lesson in our module on the foundations of generative models.

In our previous lessons, we designed the architectures for a Generative Adversarial Network (GAN) and implemented the logic for the unique adversarial training loop. We have the art forger (the Generator), the art detective (the Discriminator), and the rules of their game.

Today, we'll bring this all to life. Our learning outcome is to train a GAN, generate synthetic images, and compare GANs vs. VAEs in terms of output quality and training stability. We'll execute the training process, witness our generator learn to create images from noise, and then take a step back to critically evaluate how GANs stack up against the Variational Autoencoders (VAEs) we studied earlier.

Let's begin!

1. Training a GAN and Generating Images

We have all the components; now it's time to put them together and run the training process. Seeing a GAN learn is one of the more satisfying moments in deep learning, as you can visually track the model's progress from generating noise to producing recognizable shapes.

The PyTorch DCGAN tutorial provides a complete, end-to-end example of this process. It uses a Deep Convolutional GAN (DCGAN) to generate images of celebrity faces.

DCGAN Tutorial — PyTorch Tutorials

This tutorial contains the full implementation of the training loop we discussed previously, along with code to visualize the results. It's an excellent reference for a complete GAN project.

Please review the sections titled 'Training' and 'Results'. In the 'Training' section, notice how the code is split into '(1) Update D network' and '(2) Update G network', exactly as we discussed. In the 'Results' section, pay attention to the two key outputs: the plot of Generator and Discriminator losses, and the grid showing the generator's output over time.

Visualizing the Learning Process

As you saw in the tutorial, one of the most powerful ways to understand if your GAN is learning is to watch the output of the generator over time. We do this by feeding a fixed batch of random noise (fixed_noise) to the generator at the end of each epoch and saving the resulting images.

The following video provides a dynamic look at this process. While it uses PyTorch Lightning (a high-level library that simplifies the training loop), the principle is identical. You can see the images evolve from random static into recognizable digits.

Building a GAN From Scratch With PyTorch | Theory + Implementation

Let's watch the training in action. This video shows a GAN being trained on the MNIST dataset. You'll see the generated images improve with each epoch.

Watch from 28:30 to 31:44. Observe how the images displayed after each epoch start as random noise and gradually begin to resemble handwritten digits.

By the end of training, you have a generator model that has learned a mapping from a simple distribution (e.g., a Gaussian) to the complex distribution of your training data. You can now generate infinite new samples by feeding new random vectors into your trained generator.

2. GANs vs. VAEs: A Comparative Analysis

Now that we've seen both VAEs and GANs in action, we can perform a critical comparison. Both are powerful generative models, but they have distinct characteristics, strengths, and weaknesses. We will focus on two key aspects: output quality and training stability.

This article from AICompetence.org provides an excellent high-level overview of the comparison.

VAEs Vs. GANs: Synergizing Two Powerful Generative Models

To start our comparison, let's read a short article that directly contrasts VAEs and GANs.

Read the first three sections: 'What Are VAEs and GANs? A Simple Breakdown', 'How Generative Adversarial Networks (GANs) Work: Pros and Cons', and 'VAE vs GAN: Key Differences in Training and Architecture'. This will set the stage for our deeper discussion.

Output Quality: Sharpness vs. Blur

One of the most noticeable differences between VAEs and GANs is the visual quality of the images they produce.

  • GANs are renowned for generating sharp, crisp, and highly realistic images. The adversarial nature of the training—where the discriminator acts as an adaptive loss function—pushes the generator to produce outputs that are perceptually convincing.
  • VAEs, on the other hand, often produce blurrier or smoother images. This is a direct result of their objective function. The reconstruction loss (like Mean Squared Error) averages over possible outputs, which tends to penalize extreme pixel values and results in less detailed textures.

VAEs Vs. GANs: Synergizing Two Powerful Generative Models

Let's revisit the comparison article to see its take on visual quality.

Read the sections 'Which One Produces Better Outputs? Visual Quality Compared' and 'Exploring the Latent Space: VAEs vs GANs'. Note the discussion on sharpness versus blurriness and how it relates to the models' objectives.

This difference isn't just qualitative. The image below shows a quantitative comparison where image quality is measured by entropy (in this context, lower entropy can be associated with more structured, less noisy images). Notice how VAEs (red bars) consistently show higher entropy than GANs (blue/green bars).

Image Quality Comparison of Generative Models by Entropy
This bar chart compares the entropy of images generated by different models. In many instances, the VAE (red) has higher entropy than the GAN and WGAN models, which can be interpreted as lower image quality or less detail.

Training Stability: The Achilles' Heel of GANs

While GANs excel in output quality, they are notoriously difficult to train. A VAE's training process is stable: you are minimizing a single, well-defined loss function. A GAN's training is a delicate balancing act.

VAEs Vs. GANs: Synergizing Two Powerful Generative Models

The AICompetence.org article also touches upon the challenges in training these models.

Please read the section 'Challenges in Training VAEs and GANs' for a concise summary of the key issues.

The training of a GAN is a minimax game that should ideally converge to a Nash equilibrium. However, in practice, this convergence is fragile. The following video offers a brilliant theoretical explanation of the common failure modes.

Understanding GANs (Generative Adversarial Networks)

This video from DeepBean explains the practical issues that make GAN training so challenging.

Watch from 17:21 to 26:37. This section covers the three major training pitfalls: the optimal/lousy discriminator problem (vanishing gradients), non-convergence, and mode collapse.

Let's summarize the key training challenges for GANs:

  1. Vanishing Gradients: If the discriminator becomes too powerful too quickly, it can perfectly distinguish real from fake. Its loss for fake images becomes very low, and the gradient it passes back to the generator becomes zero (it "vanishes"). The generator stops learning because it receives no useful feedback.

  2. Mode Collapse: This is perhaps the most famous GAN failure. The generator discovers one or a few outputs that are very effective at fooling the discriminator. It then stops exploring and only produces these few "modes" of the data distribution, failing to capture the true diversity of the training set. For example, a GAN trained on MNIST might only ever generate the digit '1'.

  3. Non-Convergence/Oscillation: The generator and discriminator parameters may oscillate and fail to reach a stable equilibrium. One network undoes the progress of the other in a cycle, and the model never converges to a state that produces good samples consistently.

This image provides a great visual summary of these failure modes and lists several common techniques developed by researchers to stabilize GAN training.

GAN Training Problems and Solutions
A summary of common GAN training problems (Mode Collapse, Vanishing Gradients, Discriminator Dominance) and popular solutions to achieve more stable training.

Conclusion

Congratulations on completing this module on the foundations of generative models! We have journeyed through Autoencoders, VAEs, and now GANs.

In this lesson, you saw how to bring a GAN to life by running the adversarial training loop and generating synthetic images. More importantly, you've developed a critical understanding of the trade-offs between GANs and VAEs.

Key Takeaways:

  • GAN training is an iterative, two-part process where the discriminator and generator are updated separately in a competitive game.
  • Output Quality: GANs generally produce much sharper and more realistic images than VAEs due to their adversarial loss function.
  • Training Stability: GANs are notoriously unstable and difficult to train. They suffer from issues like mode collapse and vanishing gradients, which VAEs do not.
  • This trade-off between output quality and training stability is a central theme in generative modeling.

Preview of the Next Module

We've explored models that use reconstruction loss (VAEs) and adversarial loss (GANs). But a third family of generative models has recently emerged, often producing state-of-the-art results with more stable training than GANs. In our next module, we will dive into Diffusion Models, the technology behind powerful image generators like DALL-E 2 and Stable Diffusion. Our first lesson will explain the elegant core concepts of the forward (noising) and reverse (denoising) processes.

Can't find a good explanation? Sign up and we'll make it for you

Sign up