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 CHW 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 seed 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.02. 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=True)
(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=True)
(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=True)
(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=True)
(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=True)
(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=True)
(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=True)
(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=True)
(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: 2.0937 Loss_G: 5.2059 D(x): 0.5704 D(G(z)): 0.6680 / 0.0090
[0/5][50/1583] Loss_D: 0.1398 Loss_G: 7.0422 D(x): 0.9692 D(G(z)): 0.0695 / 0.0026
[0/5][100/1583] Loss_D: 0.3778 Loss_G: 10.0325 D(x): 0.9794 D(G(z)): 0.1993 / 0.0008
[0/5][150/1583] Loss_D: 0.6097 Loss_G: 8.1973 D(x): 0.9602 D(G(z)): 0.2881 / 0.0100
[0/5][200/1583] Loss_D: 0.5067 Loss_G: 3.3258 D(x): 0.9389 D(G(z)): 0.2912 / 0.0669
[0/5][250/1583] Loss_D: 0.7243 Loss_G: 6.2233 D(x): 0.9623 D(G(z)): 0.3926 / 0.0065
[0/5][300/1583] Loss_D: 0.6328 Loss_G: 3.0371 D(x): 0.7012 D(G(z)): 0.1216 / 0.0761
[0/5][350/1583] Loss_D: 0.6355 Loss_G: 6.2546 D(x): 0.9066 D(G(z)): 0.3466 / 0.0061
[0/5][400/1583] Loss_D: 0.5933 Loss_G: 5.9317 D(x): 0.8420 D(G(z)): 0.2612 / 0.0068
[0/5][450/1583] Loss_D: 0.5035 Loss_G: 5.1992 D(x): 0.8792 D(G(z)): 0.2486 / 0.0155
[0/5][500/1583] Loss_D: 0.3897 Loss_G: 5.0252 D(x): 0.8224 D(G(z)): 0.1146 / 0.0128
[0/5][550/1583] Loss_D: 0.4168 Loss_G: 3.2867 D(x): 0.9445 D(G(z)): 0.2583 / 0.0599
[0/5][600/1583] Loss_D: 1.0883 Loss_G: 7.4265 D(x): 0.8716 D(G(z)): 0.5122 / 0.0017
[0/5][650/1583] Loss_D: 0.6442 Loss_G: 7.5119 D(x): 0.9065 D(G(z)): 0.3583 / 0.0011
[0/5][700/1583] Loss_D: 0.5706 Loss_G: 5.8381 D(x): 0.8609 D(G(z)): 0.2873 / 0.0058
[0/5][750/1583] Loss_D: 0.4335 Loss_G: 3.9697 D(x): 0.7444 D(G(z)): 0.0291 / 0.0306
[0/5][800/1583] Loss_D: 0.4772 Loss_G: 3.5148 D(x): 0.7534 D(G(z)): 0.0779 / 0.0593
[0/5][850/1583] Loss_D: 0.3952 Loss_G: 4.8027 D(x): 0.8665 D(G(z)): 0.1859 / 0.0175
[0/5][900/1583] Loss_D: 0.8296 Loss_G: 2.3242 D(x): 0.5593 D(G(z)): 0.0232 / 0.1687
[0/5][950/1583] Loss_D: 0.4511 Loss_G: 6.3939 D(x): 0.9043 D(G(z)): 0.2250 / 0.0055
[0/5][1000/1583] Loss_D: 0.4997 Loss_G: 3.3984 D(x): 0.7963 D(G(z)): 0.1547 / 0.0616
[0/5][1050/1583] Loss_D: 0.4385 Loss_G: 5.4464 D(x): 0.9051 D(G(z)): 0.2131 / 0.0105
[0/5][1100/1583] Loss_D: 0.6443 Loss_G: 3.1635 D(x): 0.6954 D(G(z)): 0.1101 / 0.0789
[0/5][1150/1583] Loss_D: 0.5154 Loss_G: 4.1813 D(x): 0.8644 D(G(z)): 0.2515 / 0.0253
[0/5][1200/1583] Loss_D: 0.3567 Loss_G: 4.2344 D(x): 0.8536 D(G(z)): 0.1127 / 0.0274
[0/5][1250/1583] Loss_D: 0.4974 Loss_G: 3.7511 D(x): 0.8147 D(G(z)): 0.1877 / 0.0412
[0/5][1300/1583] Loss_D: 0.5431 Loss_G: 3.2542 D(x): 0.7855 D(G(z)): 0.1699 / 0.0637
[0/5][1350/1583] Loss_D: 0.4790 Loss_G: 3.6161 D(x): 0.8129 D(G(z)): 0.1770 / 0.0514
[0/5][1400/1583] Loss_D: 0.7650 Loss_G: 3.1075 D(x): 0.5818 D(G(z)): 0.0539 / 0.0825
[0/5][1450/1583] Loss_D: 0.3656 Loss_G: 3.5022 D(x): 0.8606 D(G(z)): 0.1613 / 0.0479
[0/5][1500/1583] Loss_D: 0.5052 Loss_G: 3.9483 D(x): 0.8602 D(G(z)): 0.2419 / 0.0349
[0/5][1550/1583] Loss_D: 0.5203 Loss_G: 2.4063 D(x): 0.7115 D(G(z)): 0.0640 / 0.1319
[1/5][0/1583] Loss_D: 0.4562 Loss_G: 2.6889 D(x): 0.7655 D(G(z)): 0.1080 / 0.0978
[1/5][50/1583] Loss_D: 1.1706 Loss_G: 4.5271 D(x): 0.9553 D(G(z)): 0.5433 / 0.0223
[1/5][100/1583] Loss_D: 0.5191 Loss_G: 3.3626 D(x): 0.6842 D(G(z)): 0.0326 / 0.0644
[1/5][150/1583] Loss_D: 0.4003 Loss_G: 3.2418 D(x): 0.8212 D(G(z)): 0.1351 / 0.0543
[1/5][200/1583] Loss_D: 0.3443 Loss_G: 4.0008 D(x): 0.8306 D(G(z)): 0.1133 / 0.0298
[1/5][250/1583] Loss_D: 0.9679 Loss_G: 0.7315 D(x): 0.4914 D(G(z)): 0.0269 / 0.5954
[1/5][300/1583] Loss_D: 0.5063 Loss_G: 3.8490 D(x): 0.8268 D(G(z)): 0.2263 / 0.0342
[1/5][350/1583] Loss_D: 0.3628 Loss_G: 3.1286 D(x): 0.7881 D(G(z)): 0.0774 / 0.0694
[1/5][400/1583] Loss_D: 0.5079 Loss_G: 2.3085 D(x): 0.7134 D(G(z)): 0.0987 / 0.1327
[1/5][450/1583] Loss_D: 0.4766 Loss_G: 3.1805 D(x): 0.8356 D(G(z)): 0.2186 / 0.0569
[1/5][500/1583] Loss_D: 0.5757 Loss_G: 4.6767 D(x): 0.8609 D(G(z)): 0.2797 / 0.0184
[1/5][550/1583] Loss_D: 0.3812 Loss_G: 3.2147 D(x): 0.8100 D(G(z)): 0.1094 / 0.0671
[1/5][600/1583] Loss_D: 0.6860 Loss_G: 5.3912 D(x): 0.9041 D(G(z)): 0.3873 / 0.0083
[1/5][650/1583] Loss_D: 0.5732 Loss_G: 4.2323 D(x): 0.8981 D(G(z)): 0.3290 / 0.0234
[1/5][700/1583] Loss_D: 0.8950 Loss_G: 1.7093 D(x): 0.4992 D(G(z)): 0.0333 / 0.2306
[1/5][750/1583] Loss_D: 0.7523 Loss_G: 4.2820 D(x): 0.8530 D(G(z)): 0.3738 / 0.0264
[1/5][800/1583] Loss_D: 0.5836 Loss_G: 3.9263 D(x): 0.8748 D(G(z)): 0.3060 / 0.0322
[1/5][850/1583] Loss_D: 0.5136 Loss_G: 2.6800 D(x): 0.7322 D(G(z)): 0.1256 / 0.0976
[1/5][900/1583] Loss_D: 0.4595 Loss_G: 2.7729 D(x): 0.8015 D(G(z)): 0.1694 / 0.0919
[1/5][950/1583] Loss_D: 0.8420 Loss_G: 1.6139 D(x): 0.5276 D(G(z)): 0.0405 / 0.2764
[1/5][1000/1583] Loss_D: 0.4895 Loss_G: 5.2463 D(x): 0.9031 D(G(z)): 0.2750 / 0.0118
[1/5][1050/1583] Loss_D: 0.4452 Loss_G: 2.5976 D(x): 0.7688 D(G(z)): 0.1195 / 0.1058
[1/5][1100/1583] Loss_D: 0.4181 Loss_G: 3.0341 D(x): 0.7931 D(G(z)): 0.1395 / 0.0694
[1/5][1150/1583] Loss_D: 1.0995 Loss_G: 5.5513 D(x): 0.9474 D(G(z)): 0.5719 / 0.0093
[1/5][1200/1583] Loss_D: 0.5292 Loss_G: 2.0213 D(x): 0.7007 D(G(z)): 0.1056 / 0.1827
[1/5][1250/1583] Loss_D: 0.8488 Loss_G: 5.5229 D(x): 0.9229 D(G(z)): 0.4685 / 0.0063
[1/5][1300/1583] Loss_D: 0.5602 Loss_G: 2.7696 D(x): 0.8196 D(G(z)): 0.2571 / 0.0907
[1/5][1350/1583] Loss_D: 0.4587 Loss_G: 2.3536 D(x): 0.7599 D(G(z)): 0.1262 / 0.1227
[1/5][1400/1583] Loss_D: 0.6043 Loss_G: 1.7421 D(x): 0.6612 D(G(z)): 0.1166 / 0.2153
[1/5][1450/1583] Loss_D: 1.3670 Loss_G: 4.7058 D(x): 0.9592 D(G(z)): 0.6627 / 0.0142
[1/5][1500/1583] Loss_D: 0.5396 Loss_G: 3.7685 D(x): 0.8561 D(G(z)): 0.2838 / 0.0316
[1/5][1550/1583] Loss_D: 0.4826 Loss_G: 3.4635 D(x): 0.8909 D(G(z)): 0.2784 / 0.0436
[2/5][0/1583] Loss_D: 0.7121 Loss_G: 3.1863 D(x): 0.8632 D(G(z)): 0.3753 / 0.0605
[2/5][50/1583] Loss_D: 0.4878 Loss_G: 2.9255 D(x): 0.8097 D(G(z)): 0.2089 / 0.0709
[2/5][100/1583] Loss_D: 0.6254 Loss_G: 3.0343 D(x): 0.8105 D(G(z)): 0.2943 / 0.0682
[2/5][150/1583] Loss_D: 0.5535 Loss_G: 3.0714 D(x): 0.8577 D(G(z)): 0.2981 / 0.0625
[2/5][200/1583] Loss_D: 1.0114 Loss_G: 3.0097 D(x): 0.7061 D(G(z)): 0.4176 / 0.0639
[2/5][250/1583] Loss_D: 0.7103 Loss_G: 2.1468 D(x): 0.6014 D(G(z)): 0.0873 / 0.1562
[2/5][300/1583] Loss_D: 0.3979 Loss_G: 2.3463 D(x): 0.7967 D(G(z)): 0.1294 / 0.1300
[2/5][350/1583] Loss_D: 0.4723 Loss_G: 2.8923 D(x): 0.7918 D(G(z)): 0.1722 / 0.0781
[2/5][400/1583] Loss_D: 0.6970 Loss_G: 3.9798 D(x): 0.9246 D(G(z)): 0.4201 / 0.0278
[2/5][450/1583] Loss_D: 0.5006 Loss_G: 2.6156 D(x): 0.7540 D(G(z)): 0.1502 / 0.0917
[2/5][500/1583] Loss_D: 0.7400 Loss_G: 3.9588 D(x): 0.9152 D(G(z)): 0.4362 / 0.0258
[2/5][550/1583] Loss_D: 0.5433 Loss_G: 2.8931 D(x): 0.7938 D(G(z)): 0.2246 / 0.0769
[2/5][600/1583] Loss_D: 0.5614 Loss_G: 1.8468 D(x): 0.6997 D(G(z)): 0.1351 / 0.1925
[2/5][650/1583] Loss_D: 0.8406 Loss_G: 2.2715 D(x): 0.7561 D(G(z)): 0.3509 / 0.1427
[2/5][700/1583] Loss_D: 0.5620 Loss_G: 1.9101 D(x): 0.7098 D(G(z)): 0.1478 / 0.1887
[2/5][750/1583] Loss_D: 1.0974 Loss_G: 4.6338 D(x): 0.9571 D(G(z)): 0.5922 / 0.0154
[2/5][800/1583] Loss_D: 0.7055 Loss_G: 3.2075 D(x): 0.8590 D(G(z)): 0.3810 / 0.0561
[2/5][850/1583] Loss_D: 0.6310 Loss_G: 1.5437 D(x): 0.6262 D(G(z)): 0.0830 / 0.2562
[2/5][900/1583] Loss_D: 0.5818 Loss_G: 2.4653 D(x): 0.7534 D(G(z)): 0.2059 / 0.1152
[2/5][950/1583] Loss_D: 0.8041 Loss_G: 1.3999 D(x): 0.5735 D(G(z)): 0.1283 / 0.3026
[2/5][1000/1583] Loss_D: 0.6500 Loss_G: 2.8616 D(x): 0.8633 D(G(z)): 0.3621 / 0.0751
[2/5][1050/1583] Loss_D: 1.0551 Loss_G: 1.0898 D(x): 0.4515 D(G(z)): 0.0915 / 0.3976
[2/5][1100/1583] Loss_D: 0.6496 Loss_G: 1.4739 D(x): 0.6488 D(G(z)): 0.1350 / 0.2788
[2/5][1150/1583] Loss_D: 0.5828 Loss_G: 2.7938 D(x): 0.8134 D(G(z)): 0.2763 / 0.0808
[2/5][1200/1583] Loss_D: 0.5071 Loss_G: 2.5985 D(x): 0.8102 D(G(z)): 0.2209 / 0.1004
[2/5][1250/1583] Loss_D: 1.8888 Loss_G: 0.3106 D(x): 0.2145 D(G(z)): 0.0256 / 0.7720
[2/5][1300/1583] Loss_D: 0.6712 Loss_G: 2.0495 D(x): 0.6720 D(G(z)): 0.1749 / 0.1682
[2/5][1350/1583] Loss_D: 0.5031 Loss_G: 2.6094 D(x): 0.8513 D(G(z)): 0.2596 / 0.0931
[2/5][1400/1583] Loss_D: 0.7386 Loss_G: 1.8729 D(x): 0.6248 D(G(z)): 0.1580 / 0.1866
[2/5][1450/1583] Loss_D: 0.8463 Loss_G: 4.7616 D(x): 0.9531 D(G(z)): 0.5004 / 0.0151
[2/5][1500/1583] Loss_D: 0.6152 Loss_G: 2.7197 D(x): 0.8599 D(G(z)): 0.3433 / 0.0820
[2/5][1550/1583] Loss_D: 0.5226 Loss_G: 2.2445 D(x): 0.8066 D(G(z)): 0.2334 / 0.1334
[3/5][0/1583] Loss_D: 1.6711 Loss_G: 0.7036 D(x): 0.2734 D(G(z)): 0.0525 / 0.5439
[3/5][50/1583] Loss_D: 0.5297 Loss_G: 2.0450 D(x): 0.7338 D(G(z)): 0.1631 / 0.1590
[3/5][100/1583] Loss_D: 0.7910 Loss_G: 1.8779 D(x): 0.5417 D(G(z)): 0.0531 / 0.1892
[3/5][150/1583] Loss_D: 1.2618 Loss_G: 4.4833 D(x): 0.9373 D(G(z)): 0.6405 / 0.0177
[3/5][200/1583] Loss_D: 0.7553 Loss_G: 3.7100 D(x): 0.9350 D(G(z)): 0.4532 / 0.0367
[3/5][250/1583] Loss_D: 1.5435 Loss_G: 4.6547 D(x): 0.9462 D(G(z)): 0.6749 / 0.0177
[3/5][300/1583] Loss_D: 0.5270 Loss_G: 2.1528 D(x): 0.7892 D(G(z)): 0.2191 / 0.1458
[3/5][350/1583] Loss_D: 0.5778 Loss_G: 3.4610 D(x): 0.8789 D(G(z)): 0.3249 / 0.0414
[3/5][400/1583] Loss_D: 0.8332 Loss_G: 2.6472 D(x): 0.8856 D(G(z)): 0.4566 / 0.0996
[3/5][450/1583] Loss_D: 0.8148 Loss_G: 2.9643 D(x): 0.8092 D(G(z)): 0.4044 / 0.0693
[3/5][500/1583] Loss_D: 0.9909 Loss_G: 3.2692 D(x): 0.8038 D(G(z)): 0.4719 / 0.0543
[3/5][550/1583] Loss_D: 0.6328 Loss_G: 2.3325 D(x): 0.7905 D(G(z)): 0.2909 / 0.1272
[3/5][600/1583] Loss_D: 0.5588 Loss_G: 2.2826 D(x): 0.7346 D(G(z)): 0.1820 / 0.1291
[3/5][650/1583] Loss_D: 0.6511 Loss_G: 1.7218 D(x): 0.6786 D(G(z)): 0.1755 / 0.2233
[3/5][700/1583] Loss_D: 0.4772 Loss_G: 2.7086 D(x): 0.8623 D(G(z)): 0.2567 / 0.0823
[3/5][750/1583] Loss_D: 0.9364 Loss_G: 2.2281 D(x): 0.7042 D(G(z)): 0.3774 / 0.1379
[3/5][800/1583] Loss_D: 0.7612 Loss_G: 2.2675 D(x): 0.6129 D(G(z)): 0.1506 / 0.1422
[3/5][850/1583] Loss_D: 0.7105 Loss_G: 3.1388 D(x): 0.8508 D(G(z)): 0.3926 / 0.0520
[3/5][900/1583] Loss_D: 0.7367 Loss_G: 3.7931 D(x): 0.8219 D(G(z)): 0.3644 / 0.0307
[3/5][950/1583] Loss_D: 0.4916 Loss_G: 2.2697 D(x): 0.8232 D(G(z)): 0.2290 / 0.1318
[3/5][1000/1583] Loss_D: 1.1119 Loss_G: 1.0894 D(x): 0.4551 D(G(z)): 0.1466 / 0.3911
[3/5][1050/1583] Loss_D: 0.7621 Loss_G: 2.7067 D(x): 0.8011 D(G(z)): 0.3589 / 0.1036
[3/5][1100/1583] Loss_D: 0.5932 Loss_G: 1.7463 D(x): 0.7266 D(G(z)): 0.1907 / 0.2119
[3/5][1150/1583] Loss_D: 0.6627 Loss_G: 1.7998 D(x): 0.6349 D(G(z)): 0.1320 / 0.2166
[3/5][1200/1583] Loss_D: 0.8941 Loss_G: 3.9906 D(x): 0.8579 D(G(z)): 0.4620 / 0.0271
[3/5][1250/1583] Loss_D: 0.6522 Loss_G: 3.0514 D(x): 0.8775 D(G(z)): 0.3664 / 0.0641
[3/5][1300/1583] Loss_D: 0.6379 Loss_G: 1.9345 D(x): 0.7354 D(G(z)): 0.2227 / 0.1773
[3/5][1350/1583] Loss_D: 0.6486 Loss_G: 3.1300 D(x): 0.8120 D(G(z)): 0.3103 / 0.0594
[3/5][1400/1583] Loss_D: 0.5590 Loss_G: 1.6308 D(x): 0.6909 D(G(z)): 0.1155 / 0.2339
[3/5][1450/1583] Loss_D: 0.5835 Loss_G: 2.9319 D(x): 0.8184 D(G(z)): 0.2908 / 0.0698
[3/5][1500/1583] Loss_D: 0.5567 Loss_G: 3.2771 D(x): 0.8212 D(G(z)): 0.2699 / 0.0503
[3/5][1550/1583] Loss_D: 0.5758 Loss_G: 2.0924 D(x): 0.6892 D(G(z)): 0.1460 / 0.1590
[4/5][0/1583] Loss_D: 0.4697 Loss_G: 2.5400 D(x): 0.8463 D(G(z)): 0.2343 / 0.0983
[4/5][50/1583] Loss_D: 0.6174 Loss_G: 1.7786 D(x): 0.6271 D(G(z)): 0.0849 / 0.2095
[4/5][100/1583] Loss_D: 0.5903 Loss_G: 1.5382 D(x): 0.6618 D(G(z)): 0.1168 / 0.2561
[4/5][150/1583] Loss_D: 0.5822 Loss_G: 2.9506 D(x): 0.8507 D(G(z)): 0.3075 / 0.0679
[4/5][200/1583] Loss_D: 0.6217 Loss_G: 2.8885 D(x): 0.8032 D(G(z)): 0.2832 / 0.0761
[4/5][250/1583] Loss_D: 0.4639 Loss_G: 2.6782 D(x): 0.7655 D(G(z)): 0.1446 / 0.0942
[4/5][300/1583] Loss_D: 0.6803 Loss_G: 2.5983 D(x): 0.7471 D(G(z)): 0.2737 / 0.1041
[4/5][350/1583] Loss_D: 1.1599 Loss_G: 3.7561 D(x): 0.8994 D(G(z)): 0.5946 / 0.0393
[4/5][400/1583] Loss_D: 0.4397 Loss_G: 2.6710 D(x): 0.7905 D(G(z)): 0.1626 / 0.0914
[4/5][450/1583] Loss_D: 0.6920 Loss_G: 4.4242 D(x): 0.9129 D(G(z)): 0.3960 / 0.0170
[4/5][500/1583] Loss_D: 0.6012 Loss_G: 2.7502 D(x): 0.8237 D(G(z)): 0.2946 / 0.0884
[4/5][550/1583] Loss_D: 0.4382 Loss_G: 2.7016 D(x): 0.8322 D(G(z)): 0.1978 / 0.0861
[4/5][600/1583] Loss_D: 0.4184 Loss_G: 2.8072 D(x): 0.8396 D(G(z)): 0.1934 / 0.0804
[4/5][650/1583] Loss_D: 0.6794 Loss_G: 1.6490 D(x): 0.6402 D(G(z)): 0.1570 / 0.2234
[4/5][700/1583] Loss_D: 0.7558 Loss_G: 4.3289 D(x): 0.9008 D(G(z)): 0.4300 / 0.0185
[4/5][750/1583] Loss_D: 1.4123 Loss_G: 1.7196 D(x): 0.4854 D(G(z)): 0.3556 / 0.2460
[4/5][800/1583] Loss_D: 0.5764 Loss_G: 1.7502 D(x): 0.6733 D(G(z)): 0.1212 / 0.2047
[4/5][850/1583] Loss_D: 0.6206 Loss_G: 1.5686 D(x): 0.6327 D(G(z)): 0.0906 / 0.2528
[4/5][900/1583] Loss_D: 1.7368 Loss_G: 0.6327 D(x): 0.2384 D(G(z)): 0.0383 / 0.5797
[4/5][950/1583] Loss_D: 0.6034 Loss_G: 2.7328 D(x): 0.8341 D(G(z)): 0.3071 / 0.0804
[4/5][1000/1583] Loss_D: 0.5686 Loss_G: 2.2573 D(x): 0.7429 D(G(z)): 0.1976 / 0.1303
[4/5][1050/1583] Loss_D: 0.8171 Loss_G: 0.8309 D(x): 0.5286 D(G(z)): 0.0917 / 0.4872
[4/5][1100/1583] Loss_D: 0.9072 Loss_G: 0.9942 D(x): 0.4898 D(G(z)): 0.0467 / 0.4138
[4/5][1150/1583] Loss_D: 1.5420 Loss_G: 0.4075 D(x): 0.2873 D(G(z)): 0.0646 / 0.7036
[4/5][1200/1583] Loss_D: 0.5583 Loss_G: 2.7555 D(x): 0.8046 D(G(z)): 0.2583 / 0.0806
[4/5][1250/1583] Loss_D: 0.5443 Loss_G: 2.0632 D(x): 0.7448 D(G(z)): 0.1801 / 0.1619
[4/5][1300/1583] Loss_D: 0.5524 Loss_G: 2.2856 D(x): 0.7977 D(G(z)): 0.2483 / 0.1304
[4/5][1350/1583] Loss_D: 0.7610 Loss_G: 4.9608 D(x): 0.9259 D(G(z)): 0.4534 / 0.0104
[4/5][1400/1583] Loss_D: 0.7584 Loss_G: 1.7758 D(x): 0.5545 D(G(z)): 0.0638 / 0.2071
[4/5][1450/1583] Loss_D: 0.5516 Loss_G: 2.8200 D(x): 0.7589 D(G(z)): 0.2001 / 0.0831
[4/5][1500/1583] Loss_D: 0.5059 Loss_G: 1.9223 D(x): 0.7420 D(G(z)): 0.1481 / 0.1819
[4/5][1550/1583] Loss_D: 0.9297 Loss_G: 4.1584 D(x): 0.8525 D(G(z)): 0.4649 / 0.0245
```

## 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:** ( 28 minutes 15.444 seconds)