Implementing Vanilla GAN using Keras

Generative Adversarial Networks (GANs) have become an increasingly famous topic in AI due to their ability to generate high-quality data across various domains, from images to music and beyond. Among the different GAN variants, vanilla GAN stands out as the fundamental architecture on which many other GANs are built. Here, we will explore the workings of vanilla GAN and implement it from scratch.

Vanilla GAN architecture

At its core, a vanilla GAN consists of two neural networks: a generator GG and a discriminator DD. The figure below shows a simplified workflow of the vanilla GAN.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

The generator aims to generate synthetic data samples that resemble the real data pdata(x)\sim p_{data}(x), while the discriminator tries to differentiate between the real xx and fake G(z)G(z)samples. These two networks engage in a min-max game, where the generator aims to fool the discriminator by producing realistic samples, and the discriminator aims to differentiate between real and fake samples accurately.

Implementation of vanilla GAN

Here is a step-by-step implementation of vanilla GAN:

  1. Import the necessary libraries for creating and visualizing the GAN:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Define the required parameters for the model:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Define and build the generator network:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Define and build the discriminator network:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Compile the generator and discriminator networks:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Combine the generator and discriminator and build the GAN:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Load the dataset, preprocess it, and train the model:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Test the generator by passing random noise and checking what it generates:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

We can view the results of the model by running the following live app:

The code won’t run here because we are not using a GPU, which is a requirement for training the vanilla GAN. However, the output can be observed in prerun mode in the Jupyter Notebook below.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Conclusion

Implementing Vanilla GAN from scratch provides a deep understanding of the underlying concepts of generative modeling. By following the steps outlined, we can embark on our journey into the fascinating world of GANs. Experimenting with different architectures, loss functions, and training strategies can further enhance our understanding of creating compelling generative models.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved