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 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.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: 1.9039 Loss_G: 3.6430 D(x): 0.4168 D(G(z)): 0.5231 / 0.0382
[0/5][50/1583] Loss_D: 0.1623 Loss_G: 17.0425 D(x): 0.9550 D(G(z)): 0.0000 / 0.0000
[0/5][100/1583] Loss_D: 1.2992 Loss_G: 13.9824 D(x): 0.5691 D(G(z)): 0.0002 / 0.0000
[0/5][150/1583] Loss_D: 3.8838 Loss_G: 6.9029 D(x): 0.1462 D(G(z)): 0.0003 / 0.0138
[0/5][200/1583] Loss_D: 0.5778 Loss_G: 7.4198 D(x): 0.9373 D(G(z)): 0.3528 / 0.0015
[0/5][250/1583] Loss_D: 0.6361 Loss_G: 5.3856 D(x): 0.8681 D(G(z)): 0.3336 / 0.0108
[0/5][300/1583] Loss_D: 0.4836 Loss_G: 3.1797 D(x): 0.8561 D(G(z)): 0.2256 / 0.0717
[0/5][350/1583] Loss_D: 0.7022 Loss_G: 2.6850 D(x): 0.6493 D(G(z)): 0.1256 / 0.1082
[0/5][400/1583] Loss_D: 0.5376 Loss_G: 3.4470 D(x): 0.7452 D(G(z)): 0.1302 / 0.0569
[0/5][450/1583] Loss_D: 0.7699 Loss_G: 4.6969 D(x): 0.9186 D(G(z)): 0.4030 / 0.0203
[0/5][500/1583] Loss_D: 0.6518 Loss_G: 3.4765 D(x): 0.6775 D(G(z)): 0.0993 / 0.0550
[0/5][550/1583] Loss_D: 0.5472 Loss_G: 4.3045 D(x): 0.8140 D(G(z)): 0.2064 / 0.0262
[0/5][600/1583] Loss_D: 0.4620 Loss_G: 3.0975 D(x): 0.7878 D(G(z)): 0.1596 / 0.0671
[0/5][650/1583] Loss_D: 0.7337 Loss_G: 5.1233 D(x): 0.9252 D(G(z)): 0.3973 / 0.0132
[0/5][700/1583] Loss_D: 0.4099 Loss_G: 4.2863 D(x): 0.8875 D(G(z)): 0.2069 / 0.0242
[0/5][750/1583] Loss_D: 0.5000 Loss_G: 4.5924 D(x): 0.8224 D(G(z)): 0.2065 / 0.0182
[0/5][800/1583] Loss_D: 0.9522 Loss_G: 1.7681 D(x): 0.5797 D(G(z)): 0.1807 / 0.2267
[0/5][850/1583] Loss_D: 0.8191 Loss_G: 7.5684 D(x): 0.8994 D(G(z)): 0.4274 / 0.0011
[0/5][900/1583] Loss_D: 0.5111 Loss_G: 3.8255 D(x): 0.7428 D(G(z)): 0.1100 / 0.0453
[0/5][950/1583] Loss_D: 0.4716 Loss_G: 3.3614 D(x): 0.7476 D(G(z)): 0.0414 / 0.0568
[0/5][1000/1583] Loss_D: 0.3815 Loss_G: 6.0019 D(x): 0.8909 D(G(z)): 0.1874 / 0.0062
[0/5][1050/1583] Loss_D: 0.5665 Loss_G: 4.3631 D(x): 0.8460 D(G(z)): 0.2646 / 0.0248
[0/5][1100/1583] Loss_D: 0.4460 Loss_G: 5.3867 D(x): 0.8612 D(G(z)): 0.1996 / 0.0084
[0/5][1150/1583] Loss_D: 0.8522 Loss_G: 3.0227 D(x): 0.5713 D(G(z)): 0.0799 / 0.0992
[0/5][1200/1583] Loss_D: 0.5639 Loss_G: 3.1870 D(x): 0.7612 D(G(z)): 0.1824 / 0.0645
[0/5][1250/1583] Loss_D: 0.6336 Loss_G: 2.9203 D(x): 0.6426 D(G(z)): 0.0407 / 0.0926
[0/5][1300/1583] Loss_D: 0.9715 Loss_G: 1.8182 D(x): 0.5149 D(G(z)): 0.0353 / 0.2369
[0/5][1350/1583] Loss_D: 0.5746 Loss_G: 2.9618 D(x): 0.6919 D(G(z)): 0.0858 / 0.0921
[0/5][1400/1583] Loss_D: 1.6591 Loss_G: 7.6795 D(x): 0.9695 D(G(z)): 0.7317 / 0.0017
[0/5][1450/1583] Loss_D: 0.6283 Loss_G: 1.9312 D(x): 0.6958 D(G(z)): 0.1252 / 0.2256
[0/5][1500/1583] Loss_D: 0.4230 Loss_G: 3.2575 D(x): 0.8216 D(G(z)): 0.1601 / 0.0587
[0/5][1550/1583] Loss_D: 1.1457 Loss_G: 5.6826 D(x): 0.9196 D(G(z)): 0.5549 / 0.0098
[1/5][0/1583] Loss_D: 0.6552 Loss_G: 4.5755 D(x): 0.8672 D(G(z)): 0.3345 / 0.0159
[1/5][50/1583] Loss_D: 0.6376 Loss_G: 5.4432 D(x): 0.8976 D(G(z)): 0.3635 / 0.0073
[1/5][100/1583] Loss_D: 0.4550 Loss_G: 5.0929 D(x): 0.9211 D(G(z)): 0.2706 / 0.0103
[1/5][150/1583] Loss_D: 0.6165 Loss_G: 6.2415 D(x): 0.9285 D(G(z)): 0.3515 / 0.0056
[1/5][200/1583] Loss_D: 0.2581 Loss_G: 4.1565 D(x): 0.8922 D(G(z)): 0.1071 / 0.0282
[1/5][250/1583] Loss_D: 0.7601 Loss_G: 2.3780 D(x): 0.5887 D(G(z)): 0.0783 / 0.1394
[1/5][300/1583] Loss_D: 0.6612 Loss_G: 4.7874 D(x): 0.8963 D(G(z)): 0.3621 / 0.0158
[1/5][350/1583] Loss_D: 1.1841 Loss_G: 0.9031 D(x): 0.4560 D(G(z)): 0.0746 / 0.5115
[1/5][400/1583] Loss_D: 0.4862 Loss_G: 3.9561 D(x): 0.8108 D(G(z)): 0.1950 / 0.0326
[1/5][450/1583] Loss_D: 0.6431 Loss_G: 2.1948 D(x): 0.6638 D(G(z)): 0.1280 / 0.1557
[1/5][500/1583] Loss_D: 0.4182 Loss_G: 3.7935 D(x): 0.8428 D(G(z)): 0.1739 / 0.0368
[1/5][550/1583] Loss_D: 0.3767 Loss_G: 2.9599 D(x): 0.7961 D(G(z)): 0.0858 / 0.0835
[1/5][600/1583] Loss_D: 0.3043 Loss_G: 3.0177 D(x): 0.8274 D(G(z)): 0.0846 / 0.0716
[1/5][650/1583] Loss_D: 0.7450 Loss_G: 5.0417 D(x): 0.8280 D(G(z)): 0.3633 / 0.0127
[1/5][700/1583] Loss_D: 0.5365 Loss_G: 3.1253 D(x): 0.7902 D(G(z)): 0.2045 / 0.0680
[1/5][750/1583] Loss_D: 0.4844 Loss_G: 3.5575 D(x): 0.8008 D(G(z)): 0.1763 / 0.0444
[1/5][800/1583] Loss_D: 0.8295 Loss_G: 5.9287 D(x): 0.9515 D(G(z)): 0.4805 / 0.0044
[1/5][850/1583] Loss_D: 0.3368 Loss_G: 4.2821 D(x): 0.9162 D(G(z)): 0.1976 / 0.0228
[1/5][900/1583] Loss_D: 0.3382 Loss_G: 3.8576 D(x): 0.7963 D(G(z)): 0.0679 / 0.0366
[1/5][950/1583] Loss_D: 0.6690 Loss_G: 5.2189 D(x): 0.9594 D(G(z)): 0.4098 / 0.0109
[1/5][1000/1583] Loss_D: 0.4415 Loss_G: 3.2653 D(x): 0.7873 D(G(z)): 0.1381 / 0.0641
[1/5][1050/1583] Loss_D: 0.7956 Loss_G: 2.2089 D(x): 0.5397 D(G(z)): 0.0252 / 0.1564
[1/5][1100/1583] Loss_D: 0.6594 Loss_G: 4.5010 D(x): 0.9542 D(G(z)): 0.4047 / 0.0208
[1/5][1150/1583] Loss_D: 0.6477 Loss_G: 2.3961 D(x): 0.7041 D(G(z)): 0.1860 / 0.1253
[1/5][1200/1583] Loss_D: 0.2942 Loss_G: 2.4350 D(x): 0.8897 D(G(z)): 0.1317 / 0.1314
[1/5][1250/1583] Loss_D: 0.5491 Loss_G: 3.5415 D(x): 0.8159 D(G(z)): 0.2472 / 0.0458
[1/5][1300/1583] Loss_D: 1.5971 Loss_G: 5.3750 D(x): 0.9286 D(G(z)): 0.7084 / 0.0101
[1/5][1350/1583] Loss_D: 0.9891 Loss_G: 4.9416 D(x): 0.9030 D(G(z)): 0.5178 / 0.0123
[1/5][1400/1583] Loss_D: 0.9880 Loss_G: 6.0641 D(x): 0.9489 D(G(z)): 0.5518 / 0.0038
[1/5][1450/1583] Loss_D: 0.4607 Loss_G: 3.5871 D(x): 0.8664 D(G(z)): 0.2449 / 0.0387
[1/5][1500/1583] Loss_D: 0.7387 Loss_G: 4.4729 D(x): 0.8854 D(G(z)): 0.4127 / 0.0184
[1/5][1550/1583] Loss_D: 0.5109 Loss_G: 4.2234 D(x): 0.9278 D(G(z)): 0.3200 / 0.0215
[2/5][0/1583] Loss_D: 1.2569 Loss_G: 5.0756 D(x): 0.9567 D(G(z)): 0.6231 / 0.0106
[2/5][50/1583] Loss_D: 0.6032 Loss_G: 2.1508 D(x): 0.6759 D(G(z)): 0.1184 / 0.1531
[2/5][100/1583] Loss_D: 0.5575 Loss_G: 1.9012 D(x): 0.6727 D(G(z)): 0.0946 / 0.1993
[2/5][150/1583] Loss_D: 1.2650 Loss_G: 6.8095 D(x): 0.9683 D(G(z)): 0.6449 / 0.0030
[2/5][200/1583] Loss_D: 1.3255 Loss_G: 4.4727 D(x): 0.8522 D(G(z)): 0.6248 / 0.0185
[2/5][250/1583] Loss_D: 0.5132 Loss_G: 1.9799 D(x): 0.6851 D(G(z)): 0.0691 / 0.1825
[2/5][300/1583] Loss_D: 0.5515 Loss_G: 3.7942 D(x): 0.9155 D(G(z)): 0.3253 / 0.0361
[2/5][350/1583] Loss_D: 0.4837 Loss_G: 2.9767 D(x): 0.8190 D(G(z)): 0.2144 / 0.0703
[2/5][400/1583] Loss_D: 0.8053 Loss_G: 4.3741 D(x): 0.9610 D(G(z)): 0.4821 / 0.0193
[2/5][450/1583] Loss_D: 0.6317 Loss_G: 3.7340 D(x): 0.8761 D(G(z)): 0.3561 / 0.0328
[2/5][500/1583] Loss_D: 1.2596 Loss_G: 4.2363 D(x): 0.8598 D(G(z)): 0.5981 / 0.0279
[2/5][550/1583] Loss_D: 0.8899 Loss_G: 1.5636 D(x): 0.5072 D(G(z)): 0.0754 / 0.2706
[2/5][600/1583] Loss_D: 0.6118 Loss_G: 2.4464 D(x): 0.7250 D(G(z)): 0.1914 / 0.1231
[2/5][650/1583] Loss_D: 1.0333 Loss_G: 0.8846 D(x): 0.4451 D(G(z)): 0.0733 / 0.4491
[2/5][700/1583] Loss_D: 0.5805 Loss_G: 1.9680 D(x): 0.6848 D(G(z)): 0.1370 / 0.1776
[2/5][750/1583] Loss_D: 0.3860 Loss_G: 2.9176 D(x): 0.7787 D(G(z)): 0.1049 / 0.0766
[2/5][800/1583] Loss_D: 0.6389 Loss_G: 2.2189 D(x): 0.7776 D(G(z)): 0.2792 / 0.1337
[2/5][850/1583] Loss_D: 0.7770 Loss_G: 1.5631 D(x): 0.5962 D(G(z)): 0.1375 / 0.2609
[2/5][900/1583] Loss_D: 0.5065 Loss_G: 2.8654 D(x): 0.8059 D(G(z)): 0.2183 / 0.0757
[2/5][950/1583] Loss_D: 0.4982 Loss_G: 2.0642 D(x): 0.7806 D(G(z)): 0.1898 / 0.1626
[2/5][1000/1583] Loss_D: 0.9261 Loss_G: 1.3317 D(x): 0.4728 D(G(z)): 0.0540 / 0.3050
[2/5][1050/1583] Loss_D: 0.4820 Loss_G: 1.7700 D(x): 0.7068 D(G(z)): 0.0875 / 0.2038
[2/5][1100/1583] Loss_D: 0.6923 Loss_G: 1.6688 D(x): 0.6220 D(G(z)): 0.1069 / 0.2353
[2/5][1150/1583] Loss_D: 1.2421 Loss_G: 4.4358 D(x): 0.9254 D(G(z)): 0.6254 / 0.0233
[2/5][1200/1583] Loss_D: 0.4740 Loss_G: 3.1610 D(x): 0.8770 D(G(z)): 0.2684 / 0.0535
[2/5][1250/1583] Loss_D: 0.7198 Loss_G: 1.8454 D(x): 0.6211 D(G(z)): 0.1401 / 0.2045
[2/5][1300/1583] Loss_D: 1.0999 Loss_G: 1.0682 D(x): 0.4041 D(G(z)): 0.0249 / 0.3994
[2/5][1350/1583] Loss_D: 0.7390 Loss_G: 3.7716 D(x): 0.9246 D(G(z)): 0.4440 / 0.0336
[2/5][1400/1583] Loss_D: 0.5468 Loss_G: 1.7105 D(x): 0.6874 D(G(z)): 0.1084 / 0.2230
[2/5][1450/1583] Loss_D: 1.0929 Loss_G: 5.0602 D(x): 0.9583 D(G(z)): 0.6058 / 0.0118
[2/5][1500/1583] Loss_D: 1.0002 Loss_G: 4.6431 D(x): 0.9576 D(G(z)): 0.5541 / 0.0155
[2/5][1550/1583] Loss_D: 0.8935 Loss_G: 4.1536 D(x): 0.9046 D(G(z)): 0.4893 / 0.0248
[3/5][0/1583] Loss_D: 1.0856 Loss_G: 0.9536 D(x): 0.4854 D(G(z)): 0.2057 / 0.4217
[3/5][50/1583] Loss_D: 0.5697 Loss_G: 1.6730 D(x): 0.6802 D(G(z)): 0.1157 / 0.2218
[3/5][100/1583] Loss_D: 0.5715 Loss_G: 1.9225 D(x): 0.6350 D(G(z)): 0.0607 / 0.1763
[3/5][150/1583] Loss_D: 0.9528 Loss_G: 4.1920 D(x): 0.9193 D(G(z)): 0.5310 / 0.0231
[3/5][200/1583] Loss_D: 0.7246 Loss_G: 3.0597 D(x): 0.8723 D(G(z)): 0.4145 / 0.0619
[3/5][250/1583] Loss_D: 0.6740 Loss_G: 2.0123 D(x): 0.6569 D(G(z)): 0.1621 / 0.1732
[3/5][300/1583] Loss_D: 1.5939 Loss_G: 5.8857 D(x): 0.9895 D(G(z)): 0.7263 / 0.0061
[3/5][350/1583] Loss_D: 0.9705 Loss_G: 4.4719 D(x): 0.9321 D(G(z)): 0.5340 / 0.0163
[3/5][400/1583] Loss_D: 0.6419 Loss_G: 2.4159 D(x): 0.7101 D(G(z)): 0.2154 / 0.1219
[3/5][450/1583] Loss_D: 0.7943 Loss_G: 1.3379 D(x): 0.5651 D(G(z)): 0.1057 / 0.3073
[3/5][500/1583] Loss_D: 0.8027 Loss_G: 3.6238 D(x): 0.8676 D(G(z)): 0.4408 / 0.0369
[3/5][550/1583] Loss_D: 0.7058 Loss_G: 1.6305 D(x): 0.6657 D(G(z)): 0.2088 / 0.2380
[3/5][600/1583] Loss_D: 0.3957 Loss_G: 3.3143 D(x): 0.8538 D(G(z)): 0.1939 / 0.0492
[3/5][650/1583] Loss_D: 0.5050 Loss_G: 3.4140 D(x): 0.8605 D(G(z)): 0.2703 / 0.0479
[3/5][700/1583] Loss_D: 0.8274 Loss_G: 3.8389 D(x): 0.8989 D(G(z)): 0.4681 / 0.0282
[3/5][750/1583] Loss_D: 0.6485 Loss_G: 2.8520 D(x): 0.8393 D(G(z)): 0.3393 / 0.0766
[3/5][800/1583] Loss_D: 0.4775 Loss_G: 1.9698 D(x): 0.7461 D(G(z)): 0.1426 / 0.1707
[3/5][850/1583] Loss_D: 0.7534 Loss_G: 2.0913 D(x): 0.6896 D(G(z)): 0.2638 / 0.1567
[3/5][900/1583] Loss_D: 0.6954 Loss_G: 1.5630 D(x): 0.5934 D(G(z)): 0.1073 / 0.2526
[3/5][950/1583] Loss_D: 0.4066 Loss_G: 2.6428 D(x): 0.8383 D(G(z)): 0.1848 / 0.0925
[3/5][1000/1583] Loss_D: 0.8794 Loss_G: 3.9340 D(x): 0.9058 D(G(z)): 0.4914 / 0.0280
[3/5][1050/1583] Loss_D: 0.7215 Loss_G: 3.2713 D(x): 0.9183 D(G(z)): 0.4284 / 0.0567
[3/5][1100/1583] Loss_D: 0.9526 Loss_G: 1.0712 D(x): 0.5250 D(G(z)): 0.1211 / 0.4012
[3/5][1150/1583] Loss_D: 0.5053 Loss_G: 3.3970 D(x): 0.8851 D(G(z)): 0.2847 / 0.0463
[3/5][1200/1583] Loss_D: 0.6092 Loss_G: 2.0971 D(x): 0.6780 D(G(z)): 0.1510 / 0.1479
[3/5][1250/1583] Loss_D: 1.0167 Loss_G: 3.7701 D(x): 0.8978 D(G(z)): 0.5309 / 0.0326
[3/5][1300/1583] Loss_D: 0.4864 Loss_G: 2.6576 D(x): 0.7694 D(G(z)): 0.1705 / 0.0914
[3/5][1350/1583] Loss_D: 0.8895 Loss_G: 3.3027 D(x): 0.8956 D(G(z)): 0.4861 / 0.0534
[3/5][1400/1583] Loss_D: 0.5187 Loss_G: 3.0918 D(x): 0.6626 D(G(z)): 0.0517 / 0.0629
[3/5][1450/1583] Loss_D: 0.3876 Loss_G: 2.6039 D(x): 0.8114 D(G(z)): 0.1443 / 0.1014
[3/5][1500/1583] Loss_D: 0.9103 Loss_G: 4.1674 D(x): 0.9016 D(G(z)): 0.5015 / 0.0229
[3/5][1550/1583] Loss_D: 1.5666 Loss_G: 0.1971 D(x): 0.2774 D(G(z)): 0.0325 / 0.8572
[4/5][0/1583] Loss_D: 0.6330 Loss_G: 2.1783 D(x): 0.6206 D(G(z)): 0.0711 / 0.1513
[4/5][50/1583] Loss_D: 0.6895 Loss_G: 2.8714 D(x): 0.8403 D(G(z)): 0.3442 / 0.0888
[4/5][100/1583] Loss_D: 0.6138 Loss_G: 1.5863 D(x): 0.6593 D(G(z)): 0.1208 / 0.2499
[4/5][150/1583] Loss_D: 1.5914 Loss_G: 0.3108 D(x): 0.2743 D(G(z)): 0.0173 / 0.7675
[4/5][200/1583] Loss_D: 0.6812 Loss_G: 1.6884 D(x): 0.6005 D(G(z)): 0.0902 / 0.2234
[4/5][250/1583] Loss_D: 0.6754 Loss_G: 3.3936 D(x): 0.8221 D(G(z)): 0.3425 / 0.0482
[4/5][300/1583] Loss_D: 0.6001 Loss_G: 1.9812 D(x): 0.6934 D(G(z)): 0.1526 / 0.1778
[4/5][350/1583] Loss_D: 0.6124 Loss_G: 2.1319 D(x): 0.7545 D(G(z)): 0.2389 / 0.1447
[4/5][400/1583] Loss_D: 0.5193 Loss_G: 2.6395 D(x): 0.7865 D(G(z)): 0.2202 / 0.0897
[4/5][450/1583] Loss_D: 0.8493 Loss_G: 4.1933 D(x): 0.9381 D(G(z)): 0.4916 / 0.0212
[4/5][500/1583] Loss_D: 0.8737 Loss_G: 2.9613 D(x): 0.7742 D(G(z)): 0.4105 / 0.0713
[4/5][550/1583] Loss_D: 0.4659 Loss_G: 3.1299 D(x): 0.8958 D(G(z)): 0.2769 / 0.0554
[4/5][600/1583] Loss_D: 0.4793 Loss_G: 2.1660 D(x): 0.7730 D(G(z)): 0.1749 / 0.1407
[4/5][650/1583] Loss_D: 0.6643 Loss_G: 3.0539 D(x): 0.8354 D(G(z)): 0.3395 / 0.0628
[4/5][700/1583] Loss_D: 0.6848 Loss_G: 1.7284 D(x): 0.5853 D(G(z)): 0.0800 / 0.2315
[4/5][750/1583] Loss_D: 0.9084 Loss_G: 1.2711 D(x): 0.4727 D(G(z)): 0.0325 / 0.3291
[4/5][800/1583] Loss_D: 0.5643 Loss_G: 3.4085 D(x): 0.8819 D(G(z)): 0.3203 / 0.0498
[4/5][850/1583] Loss_D: 0.4453 Loss_G: 2.3783 D(x): 0.7791 D(G(z)): 0.1542 / 0.1154
[4/5][900/1583] Loss_D: 0.7906 Loss_G: 1.0319 D(x): 0.5447 D(G(z)): 0.0898 / 0.4044
[4/5][950/1583] Loss_D: 1.0127 Loss_G: 3.1860 D(x): 0.8454 D(G(z)): 0.5072 / 0.0611
[4/5][1000/1583] Loss_D: 0.7230 Loss_G: 2.5728 D(x): 0.8218 D(G(z)): 0.3617 / 0.0966
[4/5][1050/1583] Loss_D: 1.0973 Loss_G: 1.0512 D(x): 0.4182 D(G(z)): 0.0414 / 0.3916
[4/5][1100/1583] Loss_D: 0.8570 Loss_G: 4.4044 D(x): 0.9558 D(G(z)): 0.5050 / 0.0171
[4/5][1150/1583] Loss_D: 0.7619 Loss_G: 1.6416 D(x): 0.5394 D(G(z)): 0.0489 / 0.2501
[4/5][1200/1583] Loss_D: 0.4027 Loss_G: 2.8504 D(x): 0.8377 D(G(z)): 0.1792 / 0.0726
[4/5][1250/1583] Loss_D: 0.5359 Loss_G: 2.1435 D(x): 0.6927 D(G(z)): 0.1181 / 0.1509
[4/5][1300/1583] Loss_D: 0.7062 Loss_G: 2.5684 D(x): 0.7816 D(G(z)): 0.3024 / 0.1092
[4/5][1350/1583] Loss_D: 0.4740 Loss_G: 2.5108 D(x): 0.7948 D(G(z)): 0.1903 / 0.1059
[4/5][1400/1583] Loss_D: 0.8499 Loss_G: 3.3178 D(x): 0.9185 D(G(z)): 0.4923 / 0.0483
[4/5][1450/1583] Loss_D: 0.5121 Loss_G: 2.0755 D(x): 0.7909 D(G(z)): 0.2138 / 0.1506
[4/5][1500/1583] Loss_D: 0.6426 Loss_G: 1.8200 D(x): 0.6044 D(G(z)): 0.0708 / 0.2048
[4/5][1550/1583] Loss_D: 1.0697 Loss_G: 3.4492 D(x): 0.8569 D(G(z)): 0.5077 / 0.0476
```

## 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:** ( 10 minutes 22.719 seconds)