Thanks @albanD. My code (using the working scenario case 2) is available on Github here:

  
      github.com
  
  
    malzantot/Pytorch-conditional-GANs/blob/master/conditional_dcgan.py 4
""" Conditional DCGAN for MNIST images generations.
    Author: Moustafa Alzantot (malzantot@ucla.edu)
    All rights reserved.
"""

import os
import argparse
import numpy as np
import torch
from torch import nn, optim

import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader

import torchvision
from torchvision.utils import save_image
from torchvision import datasets, transforms




  This file has been truncated. show original

  
  
    
    
  
  


To switch to case 1: you can comment line 170 and replace line 184 by the following line:
errD.backward()

To switch to case 3: you can move line 170 from its position and place it at the top of line 184.
Thanks,
Moustafa