Let's start writing PyTorch code to create a DCGAN model. Here, we assume that you are using the Python 3.7 environment in Ubuntu 18.04. If not, please refer to Chapter 2, Getting Started with PyTorch 1.3, to learn how to create an Anaconda environment.
First, let's create a Python source file called dcgan.py and import the packages that we need:
import os
import sys
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import utils
Here, NumPy is only used to initialize a random seed. If you don't have NumPy installed, simple replace np.random with random and insert the import random line after import os. In the last...