Develop a GAN with Pytorch

Implement and train GAN in Python and Pytorch.

Let’s see all of that in action and build a GAN from scratch.

After importing our standard Pytorch modules, we will define the generator and the discriminator. Both of them are simple multi-layer networks.


  • Receives as input 100-length noise vector zz sampled from a distribution
  • Outputs a flattened image in the dimensions of the cifar10 dataset: 32×32×3=307232\times32\times3 = 3072


  • The discriminator receives an input of size = 3072, which is again the flattened cifar10 image or the output of the generator.
  • It outputs a scalar in [0,1].
  • The binary cross-entropy loss is computed based on the output of the discriminator and the data label.

Note that when we train the discriminator, the generated images have the label of 0, and the real images have the label of 1.

Get hands-on with 1200+ tech skills courses.