Week 4: Conditional GAN


  1. Can you please explain this code?
  2. what is the use of a transformer here.
    Do we really use transformers in GANs?

Hi @starboy

First,transforms.Compose function converts the NumPy images to torch images, and transforms.Normalize normalizes the images with a mean value equal to 0.5 and an std value equal to 0.5 for having a better performance of training.
Then, DataLoader function is used for loading and sampling torch images.
The Loss function of the network is defined as BCEWithLogitsLoss() which combines a Sigmoid layer and the BCELoss in one single class. This version is more numerically stable than using a plain Sigmoid followed by a BCELoss as, by combining the operations into one layer, we take advantage of the log-sum-exp trick for numerical stability. (ref: BCEWithLogitsLoss Function)

As I mentioned this function is for converting NumPy images to torch images, and also normalizing them is done in this function. You can also resize images or crop them in this function (Transforms in Pytorch).


The first few lines are initializing certain parameters.

transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(0.5,), (0.5,) ])

This code actually performs a transformation on the dataset which will be loaded by the DataLoader where we pass the transform as a parameter like this

dataloader = DataLoader(
    MNIST('.', download=False, transform=transform), 

We aren’t using transformers in GANs. You can know more about transforms and DataLoader in the PyTorch documentation.

Hope this helps… :blush:

Copy to clipboard


People above me have already explained the technical part very well. I just want to simply summarize for future readers, I think you are confusing Image Transformation with [1]. We need the transform utility to normalize, resize, crop, etc. as part of the image pre-processing step.


  1. Transformer (machine learning model) - Wikipedia [Internet]. En.wikipedia.org. 2021 [cited 5 September 2021]. Available from: Transformer (machine learning model) - Wikipedia.