Note

Click here to download the full example code

# DCGAN Tutorial¶

**Author**: Nathan Inkawhich

## Introduction¶

This tutorial will give an introduction to DCGANs through an example. We will train a generative adversarial network (GAN) to generate new celebrities after showing it pictures of many real celebrities. Most of the code here is from the dcgan implementation in pytorch/examples, and this document will give a thorough explanation of the implementation and shed light on how and why this model works. But don’t worry, no prior knowledge of GANs is required, but it may require a first-timer to spend some time reasoning about what is actually happening under the hood. Also, for the sake of time it will help to have a GPU, or two. Lets start from the beginning.

## Generative Adversarial Networks¶

### What is a GAN?¶

GANs are a framework for teaching a DL model to capture the training
data’s distribution so we can generate new data from that same
distribution. GANs were invented by Ian Goodfellow in 2014 and first
described in the paper Generative Adversarial
Nets.
They are made of two distinct models, a *generator* and a
*discriminator*. The job of the generator is to spawn ‘fake’ images that
look like the training images. The job of the discriminator is to look
at an image and output whether or not it is a real training image or a
fake image from the generator. During training, the generator is
constantly trying to outsmart the discriminator by generating better and
better fakes, while the discriminator is working to become a better
detective and correctly classify the real and fake images. The
equilibrium of this game is when the generator is generating perfect
fakes that look as if they came directly from the training data, and the
discriminator is left to always guess at 50% confidence that the
generator output is real or fake.

Now, lets define some notation to be used throughout tutorial starting with the discriminator. Let \(x\) be data representing an image. \(D(x)\) is the discriminator network which outputs the (scalar) probability that \(x\) came from training data rather than the generator. Here, since we are dealing with images the input to \(D(x)\) is an image of HWC size 3x64x64. Intuitively, \(D(x)\) should be HIGH when \(x\) comes from training data and LOW when \(x\) comes from the generator. \(D(x)\) can also be thought of as a traditional binary classifier.

For the generator’s notation, let \(z\) be a latent space vector sampled from a standard normal distribution. \(G(z)\) represents the generator function which maps the latent vector \(z\) to data-space. The goal of \(G\) is to estimate the distribution that the training data comes from (\(p_{data}\)) so it can generate fake samples from that estimated distribution (\(p_g\)).

So, \(D(G(z))\) is the probability (scalar) that the output of the generator \(G\) is a real image. As described in Goodfellow’s paper, \(D\) and \(G\) play a minimax game in which \(D\) tries to maximize the probability it correctly classifies reals and fakes (\(logD(x)\)), and \(G\) tries to minimize the probability that \(D\) will predict its outputs are fake (\(log(1-D(G(x)))\)). From the paper, the GAN loss function is

In theory, the solution to this minimax game is where \(p_g = p_{data}\), and the discriminator guesses randomly if the inputs are real or fake. However, the convergence theory of GANs is still being actively researched and in reality models do not always train to this point.

### What is a DCGAN?¶

A DCGAN is a direct extension of the GAN described above, except that it explicitly uses convolutional and convolutional-transpose layers in the discriminator and generator, respectively. It was first described by Radford et. al. in the paper Unsupervised Representation Learning With Deep Convolutional Generative Adversarial Networks. The discriminator is made up of strided convolution layers, batch norm layers, and LeakyReLU activations. The input is a 3x64x64 input image and the output is a scalar probability that the input is from the real data distribution. The generator is comprised of convolutional-transpose layers, batch norm layers, and ReLU activations. The input is a latent vector, \(z\), that is drawn from a standard normal distribution and the output is a 3x64x64 RGB image. The strided conv-transpose layers allow the latent vector to be transformed into a volume with the same shape as an image. In the paper, the authors also give some tips about how to setup the optimizers, how to calculate the loss functions, and how to initialize the model weights, all of which will be explained in the coming sections.

```
from __future__ import print_function
#%matplotlib inline
import argparse
import os
import random
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 numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
# Set random seem for reproducibility
manualSeed = 999
#manualSeed = random.randint(1, 10000) # use if you want new results
print("Random Seed: ", manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)
```

Out:

```
Random Seed: 999
```

## Inputs¶

Let’s define some inputs for the run:

**dataroot**- the path to the root of the dataset folder. We will talk more about the dataset in the next section**workers**- the number of worker threads for loading the data with the DataLoader**batch_size**- the batch size used in training. The DCGAN paper uses a batch size of 128**image_size**- the spatial size of the images used for training. This implementation defaults to 64x64. If another size is desired, the structures of D and G must be changed. See here for more details**nc**- number of color channels in the input images. For color images this is 3**nz**- length of latent vector**ngf**- relates to the depth of feature maps carried through the generator**ndf**- sets the depth of feature maps propagated through the discriminator**num_epochs**- number of training epochs to run. Training for longer will probably lead to better results but will also take much longer**lr**- learning rate for training. As described in the DCGAN paper, this number should be 0.0002**beta1**- beta1 hyperparameter for Adam optimizers. As described in paper, this number should be 0.5**ngpu**- number of GPUs available. If this is 0, code will run in CPU mode. If this number is greater than 0 it will run on that number of GPUs

```
# Root directory for dataset
dataroot = "data/celeba"
# Number of workers for dataloader
workers = 2
# Batch size during training
batch_size = 128
# Spatial size of training images. All images will be resized to this
# size using a transformer.
image_size = 64
# Number of channels in the training images. For color images this is 3
nc = 3
# Size of z latent vector (i.e. size of generator input)
nz = 100
# Size of feature maps in generator
ngf = 64
# Size of feature maps in discriminator
ndf = 64
# Number of training epochs
num_epochs = 5
# Learning rate for optimizers
lr = 0.0002
# Beta1 hyperparam for Adam optimizers
beta1 = 0.5
# Number of GPUs available. Use 0 for CPU mode.
ngpu = 1
```

## Data¶

In this tutorial we will use the Celeb-A Faces
dataset which can
be downloaded at the linked site, or in Google
Drive.
The dataset will download as a file named *img_align_celeba.zip*. Once
downloaded, create a directory named *celeba* and extract the zip file
into that directory. Then, set the *dataroot* input for this notebook to
the *celeba* directory you just created. The resulting directory
structure should be:

```
/path/to/celeba
-> img_align_celeba
-> 188242.jpg
-> 173822.jpg
-> 284702.jpg
-> 537394.jpg
...
```

This is an important step because we will be using the ImageFolder dataset class, which requires there to be subdirectories in the dataset’s root folder. Now, we can create the dataset, create the dataloader, set the device to run on, and finally visualize some of the training data.

```
# We can use an image folder dataset the way we have it setup.
# Create the dataset
dataset = dset.ImageFolder(root=dataroot,
transform=transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
# Create the dataloader
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
shuffle=True, num_workers=workers)
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
# Plot some training images
real_batch = next(iter(dataloader))
plt.figure(figsize=(8,8))
plt.axis("off")
plt.title("Training Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(),(1,2,0)))
```

## Implementation¶

With our input parameters set and the dataset prepared, we can now get into the implementation. We will start with the weigth initialization strategy, then talk about the generator, discriminator, loss functions, and training loop in detail.

### Weight Initialization¶

From the DCGAN paper, the authors specify that all model weights shall
be randomly initialized from a Normal distribution with mean=0,
stdev=0.2. The `weights_init`

function takes an initialized model as
input and reinitializes all convolutional, convolutional-transpose, and
batch normalization layers to meet this criteria. This function is
applied to the models immediately after initialization.

```
# custom weights initialization called on netG and netD
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
```

### Generator¶

The generator, \(G\), is designed to map the latent space vector (\(z\)) to data-space. Since our data are images, converting \(z\) to data-space means ultimately creating a RGB image with the same size as the training images (i.e. 3x64x64). In practice, this is accomplished through a series of strided two dimensional convolutional transpose layers, each paired with a 2d batch norm layer and a relu activation. The output of the generator is fed through a tanh function to return it to the input data range of \([-1,1]\). It is worth noting the existence of the batch norm functions after the conv-transpose layers, as this is a critical contribution of the DCGAN paper. These layers help with the flow of gradients during training. An image of the generator from the DCGAN paper is shown below.

Notice, the how the inputs we set in the input section (*nz*, *ngf*, and
*nc*) influence the generator architecture in code. *nz* is the length
of the z input vector, *ngf* relates to the size of the feature maps
that are propagated through the generator, and *nc* is the number of
channels in the output image (set to 3 for RGB images). Below is the
code for the generator.

```
# Generator Code
class Generator(nn.Module):
def __init__(self, ngpu):
super(Generator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 64 x 64
)
def forward(self, input):
return self.main(input)
```

Now, we can instantiate the generator and apply the `weights_init`

function. Check out the printed model to see how the generator object is
structured.

```
# Create the generator
netG = Generator(ngpu).to(device)
# Handle multi-gpu if desired
if (device.type == 'cuda') and (ngpu > 1):
netG = nn.DataParallel(netG, list(range(ngpu)))
# Apply the weights_init function to randomly initialize all weights
# to mean=0, stdev=0.2.
netG.apply(weights_init)
# Print the model
print(netG)
```

Out:

```
Generator(
(main): Sequential(
(0): ConvTranspose2d(100, 512, kernel_size=(4, 4), stride=(1, 1), bias=False)
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
(3): ConvTranspose2d(512, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ReLU(inplace)
(6): ConvTranspose2d(256, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(7): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(8): ReLU(inplace)
(9): ConvTranspose2d(128, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(10): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(11): ReLU(inplace)
(12): ConvTranspose2d(64, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(13): Tanh()
)
)
```

### Discriminator¶

As mentioned, the discriminator, \(D\), is a binary classification network that takes an image as input and outputs a scalar probability that the input image is real (as opposed to fake). Here, \(D\) takes a 3x64x64 input image, processes it through a series of Conv2d, BatchNorm2d, and LeakyReLU layers, and outputs the final probability through a Sigmoid activation function. This architecture can be extended with more layers if necessary for the problem, but there is significance to the use of the strided convolution, BatchNorm, and LeakyReLUs. The DCGAN paper mentions it is a good practice to use strided convolution rather than pooling to downsample because it lets the network learn its own pooling function. Also batch norm and leaky relu functions promote healthy gradient flow which is critical for the learning process of both \(G\) and \(D\).

Discriminator Code

```
class Discriminator(nn.Module):
def __init__(self, ngpu):
super(Discriminator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is (nc) x 64 x 64
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x 32 x 32
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x 16 x 16
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x 8 x 8
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)
def forward(self, input):
return self.main(input)
```

Now, as with the generator, we can create the discriminator, apply the
`weights_init`

function, and print the model’s structure.

```
# Create the Discriminator
netD = Discriminator(ngpu).to(device)
# Handle multi-gpu if desired
if (device.type == 'cuda') and (ngpu > 1):
netD = nn.DataParallel(netD, list(range(ngpu)))
# Apply the weights_init function to randomly initialize all weights
# to mean=0, stdev=0.2.
netD.apply(weights_init)
# Print the model
print(netD)
```

Out:

```
Discriminator(
(main): Sequential(
(0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(1): LeakyReLU(negative_slope=0.2, inplace)
(2): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(3): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(4): LeakyReLU(negative_slope=0.2, inplace)
(5): Conv2d(128, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(7): LeakyReLU(negative_slope=0.2, inplace)
(8): Conv2d(256, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
(9): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(10): LeakyReLU(negative_slope=0.2, inplace)
(11): Conv2d(512, 1, kernel_size=(4, 4), stride=(1, 1), bias=False)
(12): Sigmoid()
)
)
```

### Loss Functions and Optimizers¶

With \(D\) and \(G\) setup, we can specify how they learn through the loss functions and optimizers. We will use the Binary Cross Entropy loss (BCELoss) function which is defined in PyTorch as:

Notice how this function provides the calculation of both log components in the objective function (i.e. \(log(D(x))\) and \(log(1-D(G(z)))\)). We can specify what part of the BCE equation to use with the \(y\) input. This is accomplished in the training loop which is coming up soon, but it is important to understand how we can choose which component we wish to calculate just by changing \(y\) (i.e. GT labels).

Next, we define our real label as 1 and the fake label as 0. These labels will be used when calculating the losses of \(D\) and \(G\), and this is also the convention used in the original GAN paper. Finally, we set up two separate optimizers, one for \(D\) and one for \(G\). As specified in the DCGAN paper, both are Adam optimizers with learning rate 0.0002 and Beta1 = 0.5. For keeping track of the generator’s learning progression, we will generate a fixed batch of latent vectors that are drawn from a Gaussian distribution (i.e. fixed_noise) . In the training loop, we will periodically input this fixed_noise into \(G\), and over the iterations we will see images form out of the noise.

```
# Initialize BCELoss function
criterion = nn.BCELoss()
# Create batch of latent vectors that we will use to visualize
# the progression of the generator
fixed_noise = torch.randn(64, nz, 1, 1, device=device)
# Establish convention for real and fake labels during training
real_label = 1
fake_label = 0
# Setup Adam optimizers for both G and D
optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999))
```

### Training¶

Finally, now that we have all of the parts of the GAN framework defined, we can train it. Be mindful that training GANs is somewhat of an art form, as incorrect hyperparameter settings lead to mode collapse with little explanation of what went wrong. Here, we will closely follow Algorithm 1 from Goodfellow’s paper, while abiding by some of the best practices shown in ganhacks. Namely, we will “construct different mini-batches for real and fake” images, and also adjust G’s objective function to maximize \(logD(G(z))\). Training is split up into two main parts. Part 1 updates the Discriminator and Part 2 updates the Generator.

**Part 1 - Train the Discriminator**

Recall, the goal of training the discriminator is to maximize the
probability of correctly classifying a given input as real or fake. In
terms of Goodfellow, we wish to “update the discriminator by ascending
its stochastic gradient”. Practically, we want to maximize
\(log(D(x)) + log(1-D(G(z)))\). Due to the separate mini-batch
suggestion from ganhacks, we will calculate this in two steps. First, we
will construct a batch of real samples from the training set, forward
pass through \(D\), calculate the loss (\(log(D(x))\)), then
calculate the gradients in a backward pass. Secondly, we will construct
a batch of fake samples with the current generator, forward pass this
batch through \(D\), calculate the loss (\(log(1-D(G(z)))\)),
and *accumulate* the gradients with a backward pass. Now, with the
gradients accumulated from both the all-real and all-fake batches, we
call a step of the Discriminator’s optimizer.

**Part 2 - Train the Generator**

As stated in the original paper, we want to train the Generator by
minimizing \(log(1-D(G(z)))\) in an effort to generate better fakes.
As mentioned, this was shown by Goodfellow to not provide sufficient
gradients, especially early in the learning process. As a fix, we
instead wish to maximize \(log(D(G(z)))\). In the code we accomplish
this by: classifying the Generator output from Part 1 with the
Discriminator, computing G’s loss *using real labels as GT*, computing
G’s gradients in a backward pass, and finally updating G’s parameters
with an optimizer step. It may seem counter-intuitive to use the real
labels as GT labels for the loss function, but this allows us to use the
\(log(x)\) part of the BCELoss (rather than the \(log(1-x)\)
part) which is exactly what we want.

Finally, we will do some statistic reporting and at the end of each epoch we will push our fixed_noise batch through the generator to visually track the progress of G’s training. The training statistics reported are:

**Loss_D**- discriminator loss calculated as the sum of losses for the all real and all fake batches (\(log(D(x)) + log(D(G(z)))\)).**Loss_G**- generator loss calculated as \(log(D(G(z)))\)**D(x)**- the average output (across the batch) of the discriminator for the all real batch. This should start close to 1 then theoretically converge to 0.5 when G gets better. Think about why this is.**D(G(z))**- average discriminator outputs for the all fake batch. The first number is before D is updated and the second number is after D is updated. These numbers should start near 0 and converge to 0.5 as G gets better. Think about why this is.

**Note:** This step might take a while, depending on how many epochs you
run and if you removed some data from the dataset.

```
# Training Loop
# Lists to keep track of progress
img_list = []
G_losses = []
D_losses = []
iters = 0
print("Starting Training Loop...")
# For each epoch
for epoch in range(num_epochs):
# For each batch in the dataloader
for i, data in enumerate(dataloader, 0):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
## Train with all-real batch
netD.zero_grad()
# Format batch
real_cpu = data[0].to(device)
b_size = real_cpu.size(0)
label = torch.full((b_size,), real_label, device=device)
# Forward pass real batch through D
output = netD(real_cpu).view(-1)
# Calculate loss on all-real batch
errD_real = criterion(output, label)
# Calculate gradients for D in backward pass
errD_real.backward()
D_x = output.mean().item()
## Train with all-fake batch
# Generate batch of latent vectors
noise = torch.randn(b_size, nz, 1, 1, device=device)
# Generate fake image batch with G
fake = netG(noise)
label.fill_(fake_label)
# Classify all fake batch with D
output = netD(fake.detach()).view(-1)
# Calculate D's loss on the all-fake batch
errD_fake = criterion(output, label)
# Calculate the gradients for this batch
errD_fake.backward()
D_G_z1 = output.mean().item()
# Add the gradients from the all-real and all-fake batches
errD = errD_real + errD_fake
# Update D
optimizerD.step()
############################
# (2) Update G network: maximize log(D(G(z)))
###########################
netG.zero_grad()
label.fill_(real_label) # fake labels are real for generator cost
# Since we just updated D, perform another forward pass of all-fake batch through D
output = netD(fake).view(-1)
# Calculate G's loss based on this output
errG = criterion(output, label)
# Calculate gradients for G
errG.backward()
D_G_z2 = output.mean().item()
# Update G
optimizerG.step()
# Output training stats
if i % 50 == 0:
print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f'
% (epoch, num_epochs, i, len(dataloader),
errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
# Save Losses for plotting later
G_losses.append(errG.item())
D_losses.append(errD.item())
# Check how the generator is doing by saving G's output on fixed_noise
if (iters % 500 == 0) or ((epoch == num_epochs-1) and (i == len(dataloader)-1)):
with torch.no_grad():
fake = netG(fixed_noise).detach().cpu()
img_list.append(vutils.make_grid(fake, padding=2, normalize=True))
iters += 1
```

Out:

```
Starting Training Loop...
[0/5][0/1583] Loss_D: 1.7410 Loss_G: 4.7765 D(x): 0.5343 D(G(z)): 0.5771 / 0.0136
[0/5][50/1583] Loss_D: 0.0382 Loss_G: 28.6215 D(x): 0.9812 D(G(z)): 0.0000 / 0.0000
[0/5][100/1583] Loss_D: 0.1879 Loss_G: 8.3892 D(x): 0.9060 D(G(z)): 0.0102 / 0.0006
[0/5][150/1583] Loss_D: 0.2393 Loss_G: 5.9645 D(x): 0.8468 D(G(z)): 0.0360 / 0.0053
[0/5][200/1583] Loss_D: 1.5163 Loss_G: 11.7773 D(x): 0.9790 D(G(z)): 0.6641 / 0.0000
[0/5][250/1583] Loss_D: 0.6208 Loss_G: 2.9454 D(x): 0.7191 D(G(z)): 0.0934 / 0.0953
[0/5][300/1583] Loss_D: 1.6575 Loss_G: 2.0008 D(x): 0.3745 D(G(z)): 0.0163 / 0.2246
[0/5][350/1583] Loss_D: 0.3401 Loss_G: 3.8493 D(x): 0.8068 D(G(z)): 0.0612 / 0.0353
[0/5][400/1583] Loss_D: 0.4630 Loss_G: 4.0048 D(x): 0.8442 D(G(z)): 0.2018 / 0.0312
[0/5][450/1583] Loss_D: 0.7405 Loss_G: 7.6614 D(x): 0.9148 D(G(z)): 0.3973 / 0.0013
[0/5][500/1583] Loss_D: 0.5280 Loss_G: 3.5317 D(x): 0.7611 D(G(z)): 0.1415 / 0.0555
[0/5][550/1583] Loss_D: 0.5328 Loss_G: 5.1301 D(x): 0.8792 D(G(z)): 0.2706 / 0.0123
[0/5][600/1583] Loss_D: 0.3276 Loss_G: 5.1835 D(x): 0.8412 D(G(z)): 0.0810 / 0.0129
[0/5][650/1583] Loss_D: 0.5694 Loss_G: 3.6770 D(x): 0.7396 D(G(z)): 0.1011 / 0.0423
[0/5][700/1583] Loss_D: 0.4701 Loss_G: 4.2490 D(x): 0.8396 D(G(z)): 0.1848 / 0.0248
[0/5][750/1583] Loss_D: 0.7458 Loss_G: 6.1934 D(x): 0.8278 D(G(z)): 0.3456 / 0.0039
[0/5][800/1583] Loss_D: 0.6787 Loss_G: 3.5948 D(x): 0.7446 D(G(z)): 0.2131 / 0.0515
[0/5][850/1583] Loss_D: 0.4864 Loss_G: 3.1261 D(x): 0.7681 D(G(z)): 0.1163 / 0.0689
[0/5][900/1583] Loss_D: 0.2352 Loss_G: 5.4004 D(x): 0.8531 D(G(z)): 0.0386 / 0.0118
[0/5][950/1583] Loss_D: 0.4484 Loss_G: 3.5127 D(x): 0.8098 D(G(z)): 0.1370 / 0.0488
[0/5][1000/1583] Loss_D: 0.4393 Loss_G: 3.3456 D(x): 0.7584 D(G(z)): 0.0773 / 0.0521
[0/5][1050/1583] Loss_D: 0.6448 Loss_G: 5.1818 D(x): 0.8601 D(G(z)): 0.3150 / 0.0139
[0/5][1100/1583] Loss_D: 0.6886 Loss_G: 3.9602 D(x): 0.8349 D(G(z)): 0.3085 / 0.0405
[0/5][1150/1583] Loss_D: 0.5596 Loss_G: 3.0755 D(x): 0.7377 D(G(z)): 0.1397 / 0.0860
[0/5][1200/1583] Loss_D: 0.4376 Loss_G: 4.1380 D(x): 0.8391 D(G(z)): 0.1916 / 0.0270
[0/5][1250/1583] Loss_D: 1.7260 Loss_G: 8.8831 D(x): 0.9793 D(G(z)): 0.7278 / 0.0004
[0/5][1300/1583] Loss_D: 0.4657 Loss_G: 5.2694 D(x): 0.8797 D(G(z)): 0.2494 / 0.0086
[0/5][1350/1583] Loss_D: 1.5693 Loss_G: 1.4831 D(x): 0.3211 D(G(z)): 0.0012 / 0.3283
[0/5][1400/1583] Loss_D: 0.4077 Loss_G: 4.4397 D(x): 0.9359 D(G(z)): 0.2587 / 0.0188
[0/5][1450/1583] Loss_D: 0.4069 Loss_G: 5.3055 D(x): 0.9359 D(G(z)): 0.2616 / 0.0094
[0/5][1500/1583] Loss_D: 0.2776 Loss_G: 4.0276 D(x): 0.8624 D(G(z)): 0.0991 / 0.0302
[0/5][1550/1583] Loss_D: 0.3434 Loss_G: 3.1824 D(x): 0.8062 D(G(z)): 0.0846 / 0.0598
[1/5][0/1583] Loss_D: 0.3786 Loss_G: 4.6799 D(x): 0.9320 D(G(z)): 0.2276 / 0.0179
[1/5][50/1583] Loss_D: 0.6948 Loss_G: 5.6480 D(x): 0.8975 D(G(z)): 0.3946 / 0.0064
[1/5][100/1583] Loss_D: 0.6347 Loss_G: 2.5258 D(x): 0.6015 D(G(z)): 0.0123 / 0.1295
[1/5][150/1583] Loss_D: 0.4071 Loss_G: 3.1594 D(x): 0.7879 D(G(z)): 0.1014 / 0.0656
[1/5][200/1583] Loss_D: 0.4934 Loss_G: 4.4241 D(x): 0.8450 D(G(z)): 0.2303 / 0.0203
[1/5][250/1583] Loss_D: 0.2959 Loss_G: 4.0930 D(x): 0.8944 D(G(z)): 0.1432 / 0.0272
[1/5][300/1583] Loss_D: 0.6205 Loss_G: 2.2349 D(x): 0.6507 D(G(z)): 0.0767 / 0.1612
[1/5][350/1583] Loss_D: 0.5175 Loss_G: 2.0007 D(x): 0.7009 D(G(z)): 0.0844 / 0.1868
[1/5][400/1583] Loss_D: 0.3397 Loss_G: 3.5013 D(x): 0.9096 D(G(z)): 0.1915 / 0.0481
[1/5][450/1583] Loss_D: 0.6137 Loss_G: 2.3944 D(x): 0.6858 D(G(z)): 0.1335 / 0.1228
[1/5][500/1583] Loss_D: 0.5970 Loss_G: 5.3760 D(x): 0.9620 D(G(z)): 0.3842 / 0.0082
[1/5][550/1583] Loss_D: 1.4892 Loss_G: 8.5210 D(x): 0.9473 D(G(z)): 0.6857 / 0.0007
[1/5][600/1583] Loss_D: 0.3399 Loss_G: 3.2890 D(x): 0.8550 D(G(z)): 0.1432 / 0.0555
[1/5][650/1583] Loss_D: 0.2677 Loss_G: 4.1367 D(x): 0.8948 D(G(z)): 0.1238 / 0.0260
[1/5][700/1583] Loss_D: 0.3439 Loss_G: 2.6406 D(x): 0.8519 D(G(z)): 0.1444 / 0.0940
[1/5][750/1583] Loss_D: 0.4051 Loss_G: 2.8622 D(x): 0.7914 D(G(z)): 0.1235 / 0.0809
[1/5][800/1583] Loss_D: 0.4592 Loss_G: 3.1358 D(x): 0.8006 D(G(z)): 0.1693 / 0.0661
[1/5][850/1583] Loss_D: 0.3739 Loss_G: 2.5033 D(x): 0.8342 D(G(z)): 0.1521 / 0.1086
[1/5][900/1583] Loss_D: 0.5859 Loss_G: 4.1363 D(x): 0.8984 D(G(z)): 0.3315 / 0.0245
[1/5][950/1583] Loss_D: 0.4409 Loss_G: 2.6979 D(x): 0.8190 D(G(z)): 0.1812 / 0.0908
[1/5][1000/1583] Loss_D: 0.5467 Loss_G: 3.7356 D(x): 0.9013 D(G(z)): 0.3198 / 0.0351
[1/5][1050/1583] Loss_D: 0.4799 Loss_G: 2.2812 D(x): 0.7185 D(G(z)): 0.0998 / 0.1325
[1/5][1100/1583] Loss_D: 0.7847 Loss_G: 4.1627 D(x): 0.8427 D(G(z)): 0.4045 / 0.0246
[1/5][1150/1583] Loss_D: 1.6589 Loss_G: 0.4691 D(x): 0.2646 D(G(z)): 0.0316 / 0.6738
[1/5][1200/1583] Loss_D: 0.5393 Loss_G: 2.7207 D(x): 0.8212 D(G(z)): 0.2507 / 0.0895
[1/5][1250/1583] Loss_D: 0.5201 Loss_G: 3.0458 D(x): 0.8047 D(G(z)): 0.2249 / 0.0630
[1/5][1300/1583] Loss_D: 0.8307 Loss_G: 4.2386 D(x): 0.9372 D(G(z)): 0.4798 / 0.0219
[1/5][1350/1583] Loss_D: 0.5316 Loss_G: 2.8804 D(x): 0.8365 D(G(z)): 0.2666 / 0.0745
[1/5][1400/1583] Loss_D: 0.5392 Loss_G: 2.8461 D(x): 0.6808 D(G(z)): 0.0649 / 0.0811
[1/5][1450/1583] Loss_D: 0.7400 Loss_G: 4.1406 D(x): 0.9020 D(G(z)): 0.4218 / 0.0245
[1/5][1500/1583] Loss_D: 0.4529 Loss_G: 2.8046 D(x): 0.9317 D(G(z)): 0.2887 / 0.0830
[1/5][1550/1583] Loss_D: 0.4394 Loss_G: 2.8364 D(x): 0.8334 D(G(z)): 0.1973 / 0.0799
[2/5][0/1583] Loss_D: 0.6620 Loss_G: 2.2626 D(x): 0.6470 D(G(z)): 0.1175 / 0.1517
[2/5][50/1583] Loss_D: 0.7125 Loss_G: 2.2557 D(x): 0.6616 D(G(z)): 0.1947 / 0.1390
[2/5][100/1583] Loss_D: 0.5895 Loss_G: 1.4966 D(x): 0.6830 D(G(z)): 0.1351 / 0.2789
[2/5][150/1583] Loss_D: 0.4708 Loss_G: 2.6764 D(x): 0.7735 D(G(z)): 0.1583 / 0.0892
[2/5][200/1583] Loss_D: 0.9255 Loss_G: 1.6462 D(x): 0.4831 D(G(z)): 0.0446 / 0.2447
[2/5][250/1583] Loss_D: 0.8347 Loss_G: 1.6854 D(x): 0.5132 D(G(z)): 0.0234 / 0.2429
[2/5][300/1583] Loss_D: 2.2011 Loss_G: 5.6124 D(x): 0.9631 D(G(z)): 0.8226 / 0.0078
[2/5][350/1583] Loss_D: 0.4677 Loss_G: 2.8677 D(x): 0.8516 D(G(z)): 0.2379 / 0.0748
[2/5][400/1583] Loss_D: 0.5535 Loss_G: 3.9264 D(x): 0.8848 D(G(z)): 0.3199 / 0.0274
[2/5][450/1583] Loss_D: 0.4146 Loss_G: 2.2503 D(x): 0.7654 D(G(z)): 0.1132 / 0.1295
[2/5][500/1583] Loss_D: 1.2845 Loss_G: 3.9054 D(x): 0.9113 D(G(z)): 0.6245 / 0.0332
[2/5][550/1583] Loss_D: 1.1137 Loss_G: 0.8221 D(x): 0.3881 D(G(z)): 0.0308 / 0.4965
[2/5][600/1583] Loss_D: 0.5427 Loss_G: 2.4799 D(x): 0.7772 D(G(z)): 0.2203 / 0.1045
[2/5][650/1583] Loss_D: 0.8611 Loss_G: 1.3133 D(x): 0.5427 D(G(z)): 0.1372 / 0.3343
[2/5][700/1583] Loss_D: 0.7056 Loss_G: 1.4510 D(x): 0.5705 D(G(z)): 0.0434 / 0.2727
[2/5][750/1583] Loss_D: 1.0672 Loss_G: 0.6588 D(x): 0.4446 D(G(z)): 0.0800 / 0.5561
[2/5][800/1583] Loss_D: 0.6318 Loss_G: 2.3128 D(x): 0.7096 D(G(z)): 0.2052 / 0.1250
[2/5][850/1583] Loss_D: 0.5844 Loss_G: 2.0583 D(x): 0.6933 D(G(z)): 0.1512 / 0.1568
[2/5][900/1583] Loss_D: 0.6434 Loss_G: 3.3785 D(x): 0.9028 D(G(z)): 0.3808 / 0.0459
[2/5][950/1583] Loss_D: 0.9772 Loss_G: 1.5877 D(x): 0.4557 D(G(z)): 0.0436 / 0.2734
[2/5][1000/1583] Loss_D: 0.5086 Loss_G: 2.6174 D(x): 0.7344 D(G(z)): 0.1486 / 0.1032
[2/5][1050/1583] Loss_D: 0.5009 Loss_G: 2.5071 D(x): 0.8357 D(G(z)): 0.2433 / 0.1033
[2/5][1100/1583] Loss_D: 1.6306 Loss_G: 6.0666 D(x): 0.9635 D(G(z)): 0.7148 / 0.0045
[2/5][1150/1583] Loss_D: 0.5919 Loss_G: 3.1436 D(x): 0.8098 D(G(z)): 0.2817 / 0.0586
[2/5][1200/1583] Loss_D: 0.4379 Loss_G: 2.3297 D(x): 0.8080 D(G(z)): 0.1813 / 0.1199
[2/5][1250/1583] Loss_D: 0.9353 Loss_G: 4.2903 D(x): 0.9435 D(G(z)): 0.5400 / 0.0201
[2/5][1300/1583] Loss_D: 0.8704 Loss_G: 5.1165 D(x): 0.9448 D(G(z)): 0.5134 / 0.0106
[2/5][1350/1583] Loss_D: 0.4931 Loss_G: 2.4716 D(x): 0.7555 D(G(z)): 0.1543 / 0.1054
[2/5][1400/1583] Loss_D: 0.8312 Loss_G: 4.1027 D(x): 0.9450 D(G(z)): 0.4923 / 0.0244
[2/5][1450/1583] Loss_D: 0.7116 Loss_G: 1.7397 D(x): 0.6756 D(G(z)): 0.1986 / 0.2238
[2/5][1500/1583] Loss_D: 0.5270 Loss_G: 2.2238 D(x): 0.8103 D(G(z)): 0.2427 / 0.1361
[2/5][1550/1583] Loss_D: 0.7797 Loss_G: 2.7019 D(x): 0.8527 D(G(z)): 0.4092 / 0.0914
[3/5][0/1583] Loss_D: 0.7323 Loss_G: 1.5646 D(x): 0.6279 D(G(z)): 0.1732 / 0.2536
[3/5][50/1583] Loss_D: 0.7553 Loss_G: 1.0858 D(x): 0.5495 D(G(z)): 0.0555 / 0.4005
[3/5][100/1583] Loss_D: 0.4828 Loss_G: 2.7008 D(x): 0.8163 D(G(z)): 0.2178 / 0.0914
[3/5][150/1583] Loss_D: 0.5946 Loss_G: 2.7186 D(x): 0.7612 D(G(z)): 0.2300 / 0.0896
[3/5][200/1583] Loss_D: 0.7206 Loss_G: 4.0731 D(x): 0.9339 D(G(z)): 0.4379 / 0.0240
[3/5][250/1583] Loss_D: 0.5469 Loss_G: 2.1401 D(x): 0.6994 D(G(z)): 0.1315 / 0.1532
[3/5][300/1583] Loss_D: 0.5985 Loss_G: 1.9012 D(x): 0.6571 D(G(z)): 0.1108 / 0.1794
[3/5][350/1583] Loss_D: 0.4473 Loss_G: 2.5483 D(x): 0.8027 D(G(z)): 0.1696 / 0.1114
[3/5][400/1583] Loss_D: 1.2903 Loss_G: 1.8902 D(x): 0.3376 D(G(z)): 0.0204 / 0.2009
[3/5][450/1583] Loss_D: 0.6487 Loss_G: 2.0348 D(x): 0.7607 D(G(z)): 0.2623 / 0.1700
[3/5][500/1583] Loss_D: 0.6244 Loss_G: 2.5118 D(x): 0.7329 D(G(z)): 0.2225 / 0.1041
[3/5][550/1583] Loss_D: 0.4046 Loss_G: 2.8724 D(x): 0.8913 D(G(z)): 0.2269 / 0.0745
[3/5][600/1583] Loss_D: 0.5322 Loss_G: 2.0076 D(x): 0.7145 D(G(z)): 0.1442 / 0.1742
[3/5][650/1583] Loss_D: 0.7304 Loss_G: 2.9482 D(x): 0.9056 D(G(z)): 0.4146 / 0.0758
[3/5][700/1583] Loss_D: 0.7246 Loss_G: 4.0229 D(x): 0.9593 D(G(z)): 0.4328 / 0.0297
[3/5][750/1583] Loss_D: 0.5929 Loss_G: 2.9571 D(x): 0.8985 D(G(z)): 0.3419 / 0.0705
[3/5][800/1583] Loss_D: 1.0501 Loss_G: 1.0762 D(x): 0.4228 D(G(z)): 0.0499 / 0.4094
[3/5][850/1583] Loss_D: 0.7131 Loss_G: 1.4547 D(x): 0.6032 D(G(z)): 0.1159 / 0.2825
[3/5][900/1583] Loss_D: 1.2416 Loss_G: 0.3249 D(x): 0.3932 D(G(z)): 0.0863 / 0.7531
[3/5][950/1583] Loss_D: 0.7577 Loss_G: 3.5412 D(x): 0.9351 D(G(z)): 0.4484 / 0.0394
[3/5][1000/1583] Loss_D: 1.2167 Loss_G: 4.5171 D(x): 0.9243 D(G(z)): 0.6311 / 0.0167
[3/5][1050/1583] Loss_D: 0.9970 Loss_G: 4.5438 D(x): 0.9038 D(G(z)): 0.5161 / 0.0176
[3/5][1100/1583] Loss_D: 0.9369 Loss_G: 3.9288 D(x): 0.9316 D(G(z)): 0.5269 / 0.0286
[3/5][1150/1583] Loss_D: 0.6565 Loss_G: 1.9839 D(x): 0.6602 D(G(z)): 0.1643 / 0.1726
[3/5][1200/1583] Loss_D: 0.5270 Loss_G: 2.2179 D(x): 0.7609 D(G(z)): 0.1830 / 0.1398
[3/5][1250/1583] Loss_D: 0.8713 Loss_G: 3.6291 D(x): 0.9179 D(G(z)): 0.4975 / 0.0387
[3/5][1300/1583] Loss_D: 0.7290 Loss_G: 2.6316 D(x): 0.7861 D(G(z)): 0.3323 / 0.1032
[3/5][1350/1583] Loss_D: 0.5897 Loss_G: 2.6585 D(x): 0.7616 D(G(z)): 0.2294 / 0.0884
[3/5][1400/1583] Loss_D: 0.5566 Loss_G: 2.5326 D(x): 0.8282 D(G(z)): 0.2756 / 0.1002
[3/5][1450/1583] Loss_D: 0.6251 Loss_G: 1.7158 D(x): 0.6515 D(G(z)): 0.1333 / 0.2239
[3/5][1500/1583] Loss_D: 0.6675 Loss_G: 1.7885 D(x): 0.6843 D(G(z)): 0.1988 / 0.1965
[3/5][1550/1583] Loss_D: 1.0200 Loss_G: 3.4574 D(x): 0.8559 D(G(z)): 0.5002 / 0.0503
[4/5][0/1583] Loss_D: 0.7700 Loss_G: 1.4115 D(x): 0.5761 D(G(z)): 0.1227 / 0.2881
[4/5][50/1583] Loss_D: 1.0598 Loss_G: 1.1720 D(x): 0.4073 D(G(z)): 0.0297 / 0.3643
[4/5][100/1583] Loss_D: 0.6948 Loss_G: 2.7301 D(x): 0.7320 D(G(z)): 0.2667 / 0.0855
[4/5][150/1583] Loss_D: 0.7640 Loss_G: 3.2689 D(x): 0.9185 D(G(z)): 0.4438 / 0.0511
[4/5][200/1583] Loss_D: 0.8262 Loss_G: 4.4338 D(x): 0.9437 D(G(z)): 0.4973 / 0.0174
[4/5][250/1583] Loss_D: 0.6680 Loss_G: 1.7715 D(x): 0.5958 D(G(z)): 0.0723 / 0.2155
[4/5][300/1583] Loss_D: 0.6329 Loss_G: 1.7099 D(x): 0.6963 D(G(z)): 0.1938 / 0.2146
[4/5][350/1583] Loss_D: 0.6982 Loss_G: 3.1792 D(x): 0.7933 D(G(z)): 0.3142 / 0.0589
[4/5][400/1583] Loss_D: 0.6753 Loss_G: 2.2458 D(x): 0.7821 D(G(z)): 0.2965 / 0.1332
[4/5][450/1583] Loss_D: 0.5765 Loss_G: 1.6604 D(x): 0.7104 D(G(z)): 0.1759 / 0.2255
[4/5][500/1583] Loss_D: 0.4970 Loss_G: 3.6595 D(x): 0.9034 D(G(z)): 0.2995 / 0.0345
[4/5][550/1583] Loss_D: 0.5597 Loss_G: 2.0412 D(x): 0.7801 D(G(z)): 0.2382 / 0.1484
[4/5][600/1583] Loss_D: 0.8558 Loss_G: 1.1745 D(x): 0.5035 D(G(z)): 0.0563 / 0.3608
[4/5][650/1583] Loss_D: 0.7401 Loss_G: 4.0250 D(x): 0.9110 D(G(z)): 0.4253 / 0.0252
[4/5][700/1583] Loss_D: 0.7533 Loss_G: 3.7020 D(x): 0.9041 D(G(z)): 0.4316 / 0.0345
[4/5][750/1583] Loss_D: 0.7711 Loss_G: 3.1788 D(x): 0.8689 D(G(z)): 0.4187 / 0.0583
[4/5][800/1583] Loss_D: 0.5269 Loss_G: 3.1927 D(x): 0.8526 D(G(z)): 0.2805 / 0.0512
[4/5][850/1583] Loss_D: 1.2121 Loss_G: 0.7910 D(x): 0.3907 D(G(z)): 0.0774 / 0.5077
[4/5][900/1583] Loss_D: 0.6486 Loss_G: 1.4854 D(x): 0.6226 D(G(z)): 0.1039 / 0.2792
[4/5][950/1583] Loss_D: 0.5064 Loss_G: 2.8585 D(x): 0.8361 D(G(z)): 0.2547 / 0.0743
[4/5][1000/1583] Loss_D: 0.8017 Loss_G: 3.0785 D(x): 0.9049 D(G(z)): 0.4482 / 0.0626
[4/5][1050/1583] Loss_D: 0.5519 Loss_G: 2.9069 D(x): 0.8438 D(G(z)): 0.2856 / 0.0714
[4/5][1100/1583] Loss_D: 0.9782 Loss_G: 4.2041 D(x): 0.9340 D(G(z)): 0.5486 / 0.0200
[4/5][1150/1583] Loss_D: 0.6122 Loss_G: 2.9777 D(x): 0.8378 D(G(z)): 0.3243 / 0.0647
[4/5][1200/1583] Loss_D: 0.6109 Loss_G: 3.1296 D(x): 0.8463 D(G(z)): 0.3228 / 0.0573
[4/5][1250/1583] Loss_D: 0.7900 Loss_G: 1.3621 D(x): 0.5532 D(G(z)): 0.0906 / 0.2959
[4/5][1300/1583] Loss_D: 1.8835 Loss_G: 0.4880 D(x): 0.2267 D(G(z)): 0.0340 / 0.6576
[4/5][1350/1583] Loss_D: 0.8850 Loss_G: 4.3860 D(x): 0.9622 D(G(z)): 0.5214 / 0.0178
[4/5][1400/1583] Loss_D: 0.7436 Loss_G: 3.7837 D(x): 0.8967 D(G(z)): 0.4397 / 0.0294
[4/5][1450/1583] Loss_D: 1.1125 Loss_G: 4.9629 D(x): 0.9183 D(G(z)): 0.5748 / 0.0128
[4/5][1500/1583] Loss_D: 1.8636 Loss_G: 5.4418 D(x): 0.9556 D(G(z)): 0.7277 / 0.0091
[4/5][1550/1583] Loss_D: 0.6687 Loss_G: 1.5855 D(x): 0.6254 D(G(z)): 0.1284 / 0.2513
```

## Results¶

Finally, lets check out how we did. Here, we will look at three different results. First, we will see how D and G’s losses changed during training. Second, we will visualize G’s output on the fixed_noise batch for every epoch. And third, we will look at a batch of real data next to a batch of fake data from G.

**Loss versus training iteration**

Below is a plot of D & G’s losses versus training iterations.

```
plt.figure(figsize=(10,5))
plt.title("Generator and Discriminator Loss During Training")
plt.plot(G_losses,label="G")
plt.plot(D_losses,label="D")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
plt.show()
```

**Visualization of G’s progression**

Remember how we saved the generator’s output on the fixed_noise batch after every epoch of training. Now, we can visualize the training progression of G with an animation. Press the play button to start the animation.

```
#%%capture
fig = plt.figure(figsize=(8,8))
plt.axis("off")
ims = [[plt.imshow(np.transpose(i,(1,2,0)), animated=True)] for i in img_list]
ani = animation.ArtistAnimation(fig, ims, interval=1000, repeat_delay=1000, blit=True)
HTML(ani.to_jshtml())
```

**Real Images vs. Fake Images**

Finally, lets take a look at some real images and fake images side by side.

```
# Grab a batch of real images from the dataloader
real_batch = next(iter(dataloader))
# Plot the real images
plt.figure(figsize=(15,15))
plt.subplot(1,2,1)
plt.axis("off")
plt.title("Real Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=5, normalize=True).cpu(),(1,2,0)))
# Plot the fake images from the last epoch
plt.subplot(1,2,2)
plt.axis("off")
plt.title("Fake Images")
plt.imshow(np.transpose(img_list[-1],(1,2,0)))
plt.show()
```

## Where to Go Next¶

We have reached the end of our journey, but there are several places you could go from here. You could:

- Train for longer to see how good the results get
- Modify this model to take a different dataset and possibly change the size of the images and the model architecture
- Check out some other cool GAN projects here
- Create GANs that generate music

**Total running time of the script:** ( 29 minutes 10.185 seconds)