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 deep learning model to capture the training
data 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(z)))\)). 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.

```
#%matplotlib inline
import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
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)
torch.use_deterministic_algorithms(True) # Needed for reproducible results
```

```
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 hyperparameter 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 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)))
```

```
<matplotlib.image.AxesImage object at 0x7eff6839b6a0>
```

## Implementation¶

With our input parameters set and the dataset prepared, we can now get into the implementation. We will start with the weight 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, 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.02``.
netG.apply(weights_init)
# Print the model
print(netG)
```

```
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
# like this: ``to mean=0, stdev=0.2``.
netD.apply(weights_init)
# Print the model
print(netD)
```

```
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 the ``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 the 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 \(log(D(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(1 - 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, dtype=torch.float, 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, accumulated (summed) with previous gradients
errD_fake.backward()
D_G_z1 = output.mean().item()
# Compute error of D as sum over the fake and the real 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
```

```
Starting Training Loop...
[0/5][0/1583] Loss_D: 1.6264 Loss_G: 5.5241 D(x): 0.5733 D(G(z)): 0.5501 / 0.0065
[0/5][50/1583] Loss_D: 0.4867 Loss_G: 23.6968 D(x): 0.7953 D(G(z)): 0.0000 / 0.0000
[0/5][100/1583] Loss_D: 2.5141 Loss_G: 9.1898 D(x): 0.3103 D(G(z)): 0.0005 / 0.0010
[0/5][150/1583] Loss_D: 1.3334 Loss_G: 11.0144 D(x): 0.9558 D(G(z)): 0.5972 / 0.0005
[0/5][200/1583] Loss_D: 0.2614 Loss_G: 3.9277 D(x): 0.8884 D(G(z)): 0.0711 / 0.0463
[0/5][250/1583] Loss_D: 0.5136 Loss_G: 4.2173 D(x): 0.7381 D(G(z)): 0.0880 / 0.0237
[0/5][300/1583] Loss_D: 0.4657 Loss_G: 4.1822 D(x): 0.7723 D(G(z)): 0.0916 / 0.0355
[0/5][350/1583] Loss_D: 0.9770 Loss_G: 2.8231 D(x): 0.5278 D(G(z)): 0.0130 / 0.1089
[0/5][400/1583] Loss_D: 0.3964 Loss_G: 4.7523 D(x): 0.8808 D(G(z)): 0.1776 / 0.0203
[0/5][450/1583] Loss_D: 0.5551 Loss_G: 3.2633 D(x): 0.7401 D(G(z)): 0.1123 / 0.0627
[0/5][500/1583] Loss_D: 0.1560 Loss_G: 5.5647 D(x): 0.9178 D(G(z)): 0.0535 / 0.0108
[0/5][550/1583] Loss_D: 0.3635 Loss_G: 4.4356 D(x): 0.8673 D(G(z)): 0.1588 / 0.0174
[0/5][600/1583] Loss_D: 0.6866 Loss_G: 6.1235 D(x): 0.9232 D(G(z)): 0.3678 / 0.0050
[0/5][650/1583] Loss_D: 0.8678 Loss_G: 7.4252 D(x): 0.8247 D(G(z)): 0.4007 / 0.0016
[0/5][700/1583] Loss_D: 0.5982 Loss_G: 2.7339 D(x): 0.6953 D(G(z)): 0.0781 / 0.1058
[0/5][750/1583] Loss_D: 0.3316 Loss_G: 4.5667 D(x): 0.8222 D(G(z)): 0.0550 / 0.0192
[0/5][800/1583] Loss_D: 0.9966 Loss_G: 3.1604 D(x): 0.5127 D(G(z)): 0.0453 / 0.0813
[0/5][850/1583] Loss_D: 0.4703 Loss_G: 4.8854 D(x): 0.9134 D(G(z)): 0.2793 / 0.0119
[0/5][900/1583] Loss_D: 2.0396 Loss_G: 0.5048 D(x): 0.2566 D(G(z)): 0.0063 / 0.6597
[0/5][950/1583] Loss_D: 0.3546 Loss_G: 3.6701 D(x): 0.8211 D(G(z)): 0.0858 / 0.0438
[0/5][1000/1583] Loss_D: 0.3682 Loss_G: 4.6290 D(x): 0.7789 D(G(z)): 0.0245 / 0.0175
[0/5][1050/1583] Loss_D: 0.2332 Loss_G: 4.1375 D(x): 0.8266 D(G(z)): 0.0121 / 0.0285
[0/5][1100/1583] Loss_D: 0.5907 Loss_G: 4.5189 D(x): 0.8805 D(G(z)): 0.2912 / 0.0235
[0/5][1150/1583] Loss_D: 0.7389 Loss_G: 3.8319 D(x): 0.5936 D(G(z)): 0.0183 / 0.0429
[0/5][1200/1583] Loss_D: 0.3712 Loss_G: 5.1008 D(x): 0.8599 D(G(z)): 0.1423 / 0.0119
[0/5][1250/1583] Loss_D: 0.5538 Loss_G: 3.1801 D(x): 0.7159 D(G(z)): 0.1137 / 0.0709
[0/5][1300/1583] Loss_D: 0.3850 Loss_G: 4.9219 D(x): 0.8526 D(G(z)): 0.1544 / 0.0185
[0/5][1350/1583] Loss_D: 1.3188 Loss_G: 1.6691 D(x): 0.3727 D(G(z)): 0.0071 / 0.3054
[0/5][1400/1583] Loss_D: 0.6639 Loss_G: 2.3084 D(x): 0.6123 D(G(z)): 0.0416 / 0.1512
[0/5][1450/1583] Loss_D: 0.6998 Loss_G: 3.1477 D(x): 0.7357 D(G(z)): 0.2022 / 0.0840
[0/5][1500/1583] Loss_D: 0.6197 Loss_G: 4.3177 D(x): 0.8433 D(G(z)): 0.2951 / 0.0242
[0/5][1550/1583] Loss_D: 0.4891 Loss_G: 2.1394 D(x): 0.7141 D(G(z)): 0.0740 / 0.1587
[1/5][0/1583] Loss_D: 0.4124 Loss_G: 5.3367 D(x): 0.9328 D(G(z)): 0.2640 / 0.0081
[1/5][50/1583] Loss_D: 0.6757 Loss_G: 1.9683 D(x): 0.5960 D(G(z)): 0.0346 / 0.2049
[1/5][100/1583] Loss_D: 0.6215 Loss_G: 3.8144 D(x): 0.7988 D(G(z)): 0.2606 / 0.0378
[1/5][150/1583] Loss_D: 0.8298 Loss_G: 5.5381 D(x): 0.9685 D(G(z)): 0.4707 / 0.0093
[1/5][200/1583] Loss_D: 0.6841 Loss_G: 1.9023 D(x): 0.6219 D(G(z)): 0.0221 / 0.2229
[1/5][250/1583] Loss_D: 0.5636 Loss_G: 2.9753 D(x): 0.7958 D(G(z)): 0.2101 / 0.0699
[1/5][300/1583] Loss_D: 0.9252 Loss_G: 6.0408 D(x): 0.9565 D(G(z)): 0.5119 / 0.0054
[1/5][350/1583] Loss_D: 0.4470 Loss_G: 3.1591 D(x): 0.7473 D(G(z)): 0.0897 / 0.0694
[1/5][400/1583] Loss_D: 0.3551 Loss_G: 3.7846 D(x): 0.8181 D(G(z)): 0.1068 / 0.0415
[1/5][450/1583] Loss_D: 0.5369 Loss_G: 3.2295 D(x): 0.6836 D(G(z)): 0.0544 / 0.0806
[1/5][500/1583] Loss_D: 0.8548 Loss_G: 5.5802 D(x): 0.9299 D(G(z)): 0.4544 / 0.0083
[1/5][550/1583] Loss_D: 0.9153 Loss_G: 6.5604 D(x): 0.9183 D(G(z)): 0.4834 / 0.0036
[1/5][600/1583] Loss_D: 0.5143 Loss_G: 2.6324 D(x): 0.6864 D(G(z)): 0.0452 / 0.1180
[1/5][650/1583] Loss_D: 2.2246 Loss_G: 9.3076 D(x): 0.9840 D(G(z)): 0.8323 / 0.0003
[1/5][700/1583] Loss_D: 0.5008 Loss_G: 3.2983 D(x): 0.7489 D(G(z)): 0.1276 / 0.0606
[1/5][750/1583] Loss_D: 1.1972 Loss_G: 1.2992 D(x): 0.4174 D(G(z)): 0.0160 / 0.3509
[1/5][800/1583] Loss_D: 0.3927 Loss_G: 3.5768 D(x): 0.7838 D(G(z)): 0.1006 / 0.0497
[1/5][850/1583] Loss_D: 1.9340 Loss_G: 2.3098 D(x): 0.2331 D(G(z)): 0.0057 / 0.1770
[1/5][900/1583] Loss_D: 0.4095 Loss_G: 3.6839 D(x): 0.8835 D(G(z)): 0.2146 / 0.0380
[1/5][950/1583] Loss_D: 0.6111 Loss_G: 4.6925 D(x): 0.8937 D(G(z)): 0.3464 / 0.0150
[1/5][1000/1583] Loss_D: 0.4565 Loss_G: 2.6885 D(x): 0.7095 D(G(z)): 0.0455 / 0.0982
[1/5][1050/1583] Loss_D: 0.7760 Loss_G: 5.2855 D(x): 0.9369 D(G(z)): 0.4586 / 0.0081
[1/5][1100/1583] Loss_D: 0.3640 Loss_G: 2.9452 D(x): 0.8190 D(G(z)): 0.1036 / 0.0737
[1/5][1150/1583] Loss_D: 1.7231 Loss_G: 0.4178 D(x): 0.2732 D(G(z)): 0.0063 / 0.6973
[1/5][1200/1583] Loss_D: 0.3893 Loss_G: 3.2459 D(x): 0.8482 D(G(z)): 0.1754 / 0.0558
[1/5][1250/1583] Loss_D: 0.4883 Loss_G: 4.0702 D(x): 0.8895 D(G(z)): 0.2764 / 0.0254
[1/5][1300/1583] Loss_D: 0.6364 Loss_G: 4.5892 D(x): 0.9387 D(G(z)): 0.3831 / 0.0177
[1/5][1350/1583] Loss_D: 0.8878 Loss_G: 3.9322 D(x): 0.8395 D(G(z)): 0.4436 / 0.0316
[1/5][1400/1583] Loss_D: 0.6954 Loss_G: 3.9376 D(x): 0.9262 D(G(z)): 0.4215 / 0.0281
[1/5][1450/1583] Loss_D: 0.8726 Loss_G: 5.2951 D(x): 0.9453 D(G(z)): 0.4882 / 0.0086
[1/5][1500/1583] Loss_D: 0.8962 Loss_G: 4.5230 D(x): 0.9073 D(G(z)): 0.4794 / 0.0200
[1/5][1550/1583] Loss_D: 0.6485 Loss_G: 1.9720 D(x): 0.6020 D(G(z)): 0.0528 / 0.1862
[2/5][0/1583] Loss_D: 0.4783 Loss_G: 3.6082 D(x): 0.9415 D(G(z)): 0.3109 / 0.0396
[2/5][50/1583] Loss_D: 0.8680 Loss_G: 4.3605 D(x): 0.9242 D(G(z)): 0.4899 / 0.0188
[2/5][100/1583] Loss_D: 0.9935 Loss_G: 4.2950 D(x): 0.8657 D(G(z)): 0.4873 / 0.0211
[2/5][150/1583] Loss_D: 0.4068 Loss_G: 2.4337 D(x): 0.7774 D(G(z)): 0.1198 / 0.1155
[2/5][200/1583] Loss_D: 0.8808 Loss_G: 1.8986 D(x): 0.6127 D(G(z)): 0.2340 / 0.1943
[2/5][250/1583] Loss_D: 0.5017 Loss_G: 2.9155 D(x): 0.8460 D(G(z)): 0.2540 / 0.0699
[2/5][300/1583] Loss_D: 0.5673 Loss_G: 2.8909 D(x): 0.8128 D(G(z)): 0.2580 / 0.0731
[2/5][350/1583] Loss_D: 0.6520 Loss_G: 1.7312 D(x): 0.6512 D(G(z)): 0.1222 / 0.2207
[2/5][400/1583] Loss_D: 0.4483 Loss_G: 2.6954 D(x): 0.8401 D(G(z)): 0.2078 / 0.0906
[2/5][450/1583] Loss_D: 0.4352 Loss_G: 3.4165 D(x): 0.8048 D(G(z)): 0.1552 / 0.0515
[2/5][500/1583] Loss_D: 0.5237 Loss_G: 3.7652 D(x): 0.8895 D(G(z)): 0.2947 / 0.0348
[2/5][550/1583] Loss_D: 0.6641 Loss_G: 3.4410 D(x): 0.8768 D(G(z)): 0.3550 / 0.0479
[2/5][600/1583] Loss_D: 0.6932 Loss_G: 3.9171 D(x): 0.8997 D(G(z)): 0.4035 / 0.0266
[2/5][650/1583] Loss_D: 0.8502 Loss_G: 1.1680 D(x): 0.5558 D(G(z)): 0.1342 / 0.3544
[2/5][700/1583] Loss_D: 0.6878 Loss_G: 2.7555 D(x): 0.8652 D(G(z)): 0.3741 / 0.0851
[2/5][750/1583] Loss_D: 0.7586 Loss_G: 2.7588 D(x): 0.7930 D(G(z)): 0.3546 / 0.0812
[2/5][800/1583] Loss_D: 0.6810 Loss_G: 3.5065 D(x): 0.9286 D(G(z)): 0.4182 / 0.0417
[2/5][850/1583] Loss_D: 0.9433 Loss_G: 0.6397 D(x): 0.4512 D(G(z)): 0.0329 / 0.5743
[2/5][900/1583] Loss_D: 0.4093 Loss_G: 2.6539 D(x): 0.8049 D(G(z)): 0.1446 / 0.0923
[2/5][950/1583] Loss_D: 0.8883 Loss_G: 3.3076 D(x): 0.8388 D(G(z)): 0.4598 / 0.0505
[2/5][1000/1583] Loss_D: 0.6832 Loss_G: 1.2879 D(x): 0.5981 D(G(z)): 0.0694 / 0.3328
[2/5][1050/1583] Loss_D: 0.7747 Loss_G: 1.3637 D(x): 0.5429 D(G(z)): 0.0565 / 0.3038
[2/5][1100/1583] Loss_D: 1.1433 Loss_G: 4.9729 D(x): 0.9748 D(G(z)): 0.6250 / 0.0103
[2/5][1150/1583] Loss_D: 0.8255 Loss_G: 3.4693 D(x): 0.7846 D(G(z)): 0.3904 / 0.0414
[2/5][1200/1583] Loss_D: 0.4738 Loss_G: 2.2615 D(x): 0.7435 D(G(z)): 0.1216 / 0.1308
[2/5][1250/1583] Loss_D: 0.6634 Loss_G: 2.3137 D(x): 0.6846 D(G(z)): 0.2014 / 0.1316
[2/5][1300/1583] Loss_D: 0.4236 Loss_G: 3.4382 D(x): 0.8943 D(G(z)): 0.2457 / 0.0438
[2/5][1350/1583] Loss_D: 0.4987 Loss_G: 2.5699 D(x): 0.8182 D(G(z)): 0.2302 / 0.1021
[2/5][1400/1583] Loss_D: 0.4090 Loss_G: 3.2936 D(x): 0.8465 D(G(z)): 0.1854 / 0.0559
[2/5][1450/1583] Loss_D: 0.8556 Loss_G: 4.2185 D(x): 0.9340 D(G(z)): 0.5040 / 0.0198
[2/5][1500/1583] Loss_D: 0.7793 Loss_G: 3.3846 D(x): 0.7977 D(G(z)): 0.3750 / 0.0494
[2/5][1550/1583] Loss_D: 0.7127 Loss_G: 3.0205 D(x): 0.8502 D(G(z)): 0.3708 / 0.0702
[3/5][0/1583] Loss_D: 0.4481 Loss_G: 2.7159 D(x): 0.8419 D(G(z)): 0.2097 / 0.0902
[3/5][50/1583] Loss_D: 0.6123 Loss_G: 1.5616 D(x): 0.6838 D(G(z)): 0.1486 / 0.2467
[3/5][100/1583] Loss_D: 0.6815 Loss_G: 2.2791 D(x): 0.6885 D(G(z)): 0.2107 / 0.1309
[3/5][150/1583] Loss_D: 0.5393 Loss_G: 2.0140 D(x): 0.7651 D(G(z)): 0.2090 / 0.1626
[3/5][200/1583] Loss_D: 0.9130 Loss_G: 1.7783 D(x): 0.6456 D(G(z)): 0.3085 / 0.2130
[3/5][250/1583] Loss_D: 0.8400 Loss_G: 4.0762 D(x): 0.9299 D(G(z)): 0.4877 / 0.0217
[3/5][300/1583] Loss_D: 0.6168 Loss_G: 1.8973 D(x): 0.7691 D(G(z)): 0.2578 / 0.1907
[3/5][350/1583] Loss_D: 0.5406 Loss_G: 2.8563 D(x): 0.8417 D(G(z)): 0.2731 / 0.0784
[3/5][400/1583] Loss_D: 0.5150 Loss_G: 2.0460 D(x): 0.7616 D(G(z)): 0.1817 / 0.1560
[3/5][450/1583] Loss_D: 0.9772 Loss_G: 4.9377 D(x): 0.9223 D(G(z)): 0.5440 / 0.0108
[3/5][500/1583] Loss_D: 0.6446 Loss_G: 1.4891 D(x): 0.6372 D(G(z)): 0.1186 / 0.2692
[3/5][550/1583] Loss_D: 1.1277 Loss_G: 3.0366 D(x): 0.8151 D(G(z)): 0.5392 / 0.0692
[3/5][600/1583] Loss_D: 0.6485 Loss_G: 2.0437 D(x): 0.6351 D(G(z)): 0.1120 / 0.1650
[3/5][650/1583] Loss_D: 0.6849 Loss_G: 2.3605 D(x): 0.6866 D(G(z)): 0.2212 / 0.1204
[3/5][700/1583] Loss_D: 0.6694 Loss_G: 3.2277 D(x): 0.8605 D(G(z)): 0.3645 / 0.0514
[3/5][750/1583] Loss_D: 0.5931 Loss_G: 2.3110 D(x): 0.7739 D(G(z)): 0.2510 / 0.1278
[3/5][800/1583] Loss_D: 0.7251 Loss_G: 1.5585 D(x): 0.5846 D(G(z)): 0.0998 / 0.2459
[3/5][850/1583] Loss_D: 1.3302 Loss_G: 3.8882 D(x): 0.7256 D(G(z)): 0.5574 / 0.0385
[3/5][900/1583] Loss_D: 0.7570 Loss_G: 3.0618 D(x): 0.7665 D(G(z)): 0.3443 / 0.0621
[3/5][950/1583] Loss_D: 0.4830 Loss_G: 2.1480 D(x): 0.7365 D(G(z)): 0.1281 / 0.1469
[3/5][1000/1583] Loss_D: 0.7231 Loss_G: 2.5734 D(x): 0.7655 D(G(z)): 0.3194 / 0.1016
[3/5][1050/1583] Loss_D: 0.7488 Loss_G: 2.6256 D(x): 0.7719 D(G(z)): 0.3364 / 0.0949
[3/5][1100/1583] Loss_D: 0.5305 Loss_G: 2.7396 D(x): 0.8107 D(G(z)): 0.2460 / 0.0814
[3/5][1150/1583] Loss_D: 0.4852 Loss_G: 2.6081 D(x): 0.7336 D(G(z)): 0.1328 / 0.0994
[3/5][1200/1583] Loss_D: 0.4899 Loss_G: 2.2377 D(x): 0.7832 D(G(z)): 0.1851 / 0.1351
[3/5][1250/1583] Loss_D: 0.6456 Loss_G: 2.7028 D(x): 0.8512 D(G(z)): 0.3529 / 0.0827
[3/5][1300/1583] Loss_D: 0.5933 Loss_G: 2.7994 D(x): 0.7544 D(G(z)): 0.2294 / 0.0831
[3/5][1350/1583] Loss_D: 0.6568 Loss_G: 1.3400 D(x): 0.6651 D(G(z)): 0.1592 / 0.3002
[3/5][1400/1583] Loss_D: 0.9779 Loss_G: 3.8328 D(x): 0.9079 D(G(z)): 0.5252 / 0.0327
[3/5][1450/1583] Loss_D: 0.4729 Loss_G: 2.3939 D(x): 0.7708 D(G(z)): 0.1564 / 0.1147
[3/5][1500/1583] Loss_D: 0.5011 Loss_G: 2.4985 D(x): 0.7625 D(G(z)): 0.1704 / 0.1080
[3/5][1550/1583] Loss_D: 0.5559 Loss_G: 2.6146 D(x): 0.8040 D(G(z)): 0.2550 / 0.0919
[4/5][0/1583] Loss_D: 0.4146 Loss_G: 2.1797 D(x): 0.7973 D(G(z)): 0.1482 / 0.1386
[4/5][50/1583] Loss_D: 2.1389 Loss_G: 0.5755 D(x): 0.1782 D(G(z)): 0.0521 / 0.6111
[4/5][100/1583] Loss_D: 1.0924 Loss_G: 0.9867 D(x): 0.4125 D(G(z)): 0.0618 / 0.4178
[4/5][150/1583] Loss_D: 0.6577 Loss_G: 1.8641 D(x): 0.6250 D(G(z)): 0.0982 / 0.1954
[4/5][200/1583] Loss_D: 1.6027 Loss_G: 0.8761 D(x): 0.2561 D(G(z)): 0.0173 / 0.4751
[4/5][250/1583] Loss_D: 1.4811 Loss_G: 0.9588 D(x): 0.3076 D(G(z)): 0.0355 / 0.4337
[4/5][300/1583] Loss_D: 0.5630 Loss_G: 3.2903 D(x): 0.8187 D(G(z)): 0.2738 / 0.0486
[4/5][350/1583] Loss_D: 0.5370 Loss_G: 2.2925 D(x): 0.7213 D(G(z)): 0.1473 / 0.1315
[4/5][400/1583] Loss_D: 0.6533 Loss_G: 2.3776 D(x): 0.7763 D(G(z)): 0.2761 / 0.1181
[4/5][450/1583] Loss_D: 0.7982 Loss_G: 3.6684 D(x): 0.8931 D(G(z)): 0.4488 / 0.0362
[4/5][500/1583] Loss_D: 0.5172 Loss_G: 2.0685 D(x): 0.7681 D(G(z)): 0.1917 / 0.1544
[4/5][550/1583] Loss_D: 0.5329 Loss_G: 2.3093 D(x): 0.6883 D(G(z)): 0.1082 / 0.1345
[4/5][600/1583] Loss_D: 1.3266 Loss_G: 0.4480 D(x): 0.3642 D(G(z)): 0.0758 / 0.6764
[4/5][650/1583] Loss_D: 0.5588 Loss_G: 2.4996 D(x): 0.8737 D(G(z)): 0.3141 / 0.1042
[4/5][700/1583] Loss_D: 0.8567 Loss_G: 2.0117 D(x): 0.7347 D(G(z)): 0.3581 / 0.1748
[4/5][750/1583] Loss_D: 0.4709 Loss_G: 2.1114 D(x): 0.7839 D(G(z)): 0.1688 / 0.1513
[4/5][800/1583] Loss_D: 0.6003 Loss_G: 3.3473 D(x): 0.8736 D(G(z)): 0.3328 / 0.0485
[4/5][850/1583] Loss_D: 0.6803 Loss_G: 2.9746 D(x): 0.8258 D(G(z)): 0.3414 / 0.0714
[4/5][900/1583] Loss_D: 0.6275 Loss_G: 2.2067 D(x): 0.7189 D(G(z)): 0.2114 / 0.1374
[4/5][950/1583] Loss_D: 0.7962 Loss_G: 3.7993 D(x): 0.9010 D(G(z)): 0.4455 / 0.0334
[4/5][1000/1583] Loss_D: 0.5967 Loss_G: 2.5142 D(x): 0.8364 D(G(z)): 0.2995 / 0.1031
[4/5][1050/1583] Loss_D: 1.4893 Loss_G: 5.1686 D(x): 0.9639 D(G(z)): 0.7062 / 0.0096
[4/5][1100/1583] Loss_D: 0.3840 Loss_G: 2.0821 D(x): 0.7954 D(G(z)): 0.1242 / 0.1565
[4/5][1150/1583] Loss_D: 0.5884 Loss_G: 3.4136 D(x): 0.8797 D(G(z)): 0.3395 / 0.0415
[4/5][1200/1583] Loss_D: 0.4886 Loss_G: 1.8685 D(x): 0.6905 D(G(z)): 0.0689 / 0.1884
[4/5][1250/1583] Loss_D: 0.5402 Loss_G: 2.6150 D(x): 0.8652 D(G(z)): 0.2891 / 0.0962
[4/5][1300/1583] Loss_D: 0.4056 Loss_G: 3.0137 D(x): 0.8994 D(G(z)): 0.2358 / 0.0679
[4/5][1350/1583] Loss_D: 1.0278 Loss_G: 1.9718 D(x): 0.7192 D(G(z)): 0.4162 / 0.1790
[4/5][1400/1583] Loss_D: 0.5550 Loss_G: 3.4878 D(x): 0.8658 D(G(z)): 0.3071 / 0.0414
[4/5][1450/1583] Loss_D: 1.2138 Loss_G: 1.0905 D(x): 0.4345 D(G(z)): 0.1591 / 0.4022
[4/5][1500/1583] Loss_D: 0.6302 Loss_G: 3.7710 D(x): 0.8966 D(G(z)): 0.3643 / 0.0323
[4/5][1550/1583] Loss_D: 1.2720 Loss_G: 0.7794 D(x): 0.3710 D(G(z)): 0.0575 / 0.5089
```

## 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.

**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.

```
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())
```