Skip to content

Commit 09925a8

Browse files
author
Svetlana Karslioglu
committed
Update
1 parent 761d6c2 commit 09925a8

7 files changed

+179
-102
lines changed

.pyspelling.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ matrix:
44
sources:
55
- beginner_source/data_loading_tutorial.py
66
- beginner_source/chatbot_tutorial.py
7+
- beginner_source/Intro_to_TorchScript_tutorial.py
8+
- beginner_source/dcgan_faces_tutorial.py
9+
- beginner_source/deploy_seq2seq_hybrid_frontend_tutorial.py
10+
- beginner_source/flava_finetuning_tutorial.py
711
dictionary:
812
wordlists:
913
- en-wordlist.txt
@@ -14,11 +18,17 @@ matrix:
1418
context_visible_first: true
1519
delimiters:
1620
# Exclude figure rST tags
17-
- open: '\.\.\s+(figure|literalinclude|)::'
21+
- open: '\.\.\s+(figure|literalinclude|math)::'
1822
close: '\n'
1923
# Exclude Python coding directives
2024
- open: '-\*- coding:'
2125
close: '\n'
26+
# Exclude Authors:
27+
- open: 'Author(|s):'
28+
close: '\n'
29+
# Exclude .rst directives:
30+
- open: ':math:`.*`'
31+
close: ' '
2232
- pyspelling.filters.markdown:
2333
- pyspelling.filters.html:
2434
ignores:

beginner_source/Intro_to_TorchScript_tutorial.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Introduction to TorchScript
33
===========================
44
5-
*James Reed (jamesreed@fb.com), Michael Suo (suo@fb.com)*, rev2
5+
**Authors:** James Reed (jamesreed@fb.com), Michael Suo (suo@fb.com), rev2
66
77
This tutorial is an introduction to TorchScript, an intermediate
88
representation of a PyTorch model (subclass of ``nn.Module``) that
@@ -147,7 +147,7 @@ def forward(self, x, h):
147147

148148

149149
######################################################################
150-
# We’ve once again redefined our MyCell class, but here we’ve defined
150+
# We’ve once again redefined our ``MyCell`` class, but here we’ve defined
151151
# ``MyDecisionGate``. This module utilizes **control flow**. Control flow
152152
# consists of things like loops and ``if``-statements.
153153
#
@@ -202,7 +202,7 @@ def forward(self, x, h):
202202
# inputs* the network might see.
203203
#
204204
# What exactly has this done? It has invoked the ``Module``, recorded the
205-
# operations that occured when the ``Module`` was run, and created an
205+
# operations that occurred when the ``Module`` was run, and created an
206206
# instance of ``torch.jit.ScriptModule`` (of which ``TracedModule`` is an
207207
# instance)
208208
#
@@ -283,7 +283,7 @@ def forward(self, x, h):
283283
# Looking at the ``.code`` output, we can see that the ``if-else`` branch
284284
# is nowhere to be found! Why? Tracing does exactly what we said it would:
285285
# run the code, record the operations *that happen* and construct a
286-
# ScriptModule that does exactly that. Unfortunately, things like control
286+
# ``ScriptModule`` that does exactly that. Unfortunately, things like control
287287
# flow are erased.
288288
#
289289
# How can we faithfully represent this module in TorchScript? We provide a

beginner_source/dcgan_faces_tutorial.py

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# This tutorial will give an introduction to DCGANs through an example. We
1616
# will train a generative adversarial network (GAN) to generate new
1717
# celebrities after showing it pictures of many real celebrities. Most of
18-
# the code here is from the dcgan implementation in
18+
# the code here is from the DCGAN implementation in
1919
# `pytorch/examples <https://github.com/pytorch/examples>`__, and this
2020
# document will give a thorough explanation of the implementation and shed
2121
# light on how and why this model works. But don’t worry, no prior
@@ -30,8 +30,8 @@
3030
# What is a GAN?
3131
# ~~~~~~~~~~~~~~
3232
#
33-
# GANs are a framework for teaching a DL model to capture the training
34-
# data’s distribution so we can generate new data from that same
33+
# GANs are a framework for teaching a deep learning model to capture the training
34+
# data distribution so we can generate new data from that same
3535
# distribution. GANs were invented by Ian Goodfellow in 2014 and first
3636
# described in the paper `Generative Adversarial
3737
# Nets <https://papers.nips.cc/paper/5423-generative-adversarial-nets.pdf>`__.
@@ -145,35 +145,35 @@
145145
#
146146
# Let’s define some inputs for the run:
147147
#
148-
# - **dataroot** - the path to the root of the dataset folder. We will
149-
# talk more about the dataset in the next section
150-
# - **workers** - the number of worker threads for loading the data with
151-
# the DataLoader
152-
# - **batch_size** - the batch size used in training. The DCGAN paper
153-
# uses a batch size of 128
154-
# - **image_size** - the spatial size of the images used for training.
148+
# - ``dataroot`` - the path to the root of the dataset folder. We will
149+
# talk more about the dataset in the next section.
150+
# - ``workers`` - the number of worker threads for loading the data with
151+
# the ``DataLoader``.
152+
# - ``batch_size`` - the batch size used in training. The DCGAN paper
153+
# uses a batch size of 128.
154+
# - ``image_size`` - the spatial size of the images used for training.
155155
# This implementation defaults to 64x64. If another size is desired,
156156
# the structures of D and G must be changed. See
157157
# `here <https://github.com/pytorch/examples/issues/70>`__ for more
158-
# details
159-
# - **nc** - number of color channels in the input images. For color
160-
# images this is 3
161-
# - **nz** - length of latent vector
162-
# - **ngf** - relates to the depth of feature maps carried through the
163-
# generator
164-
# - **ndf** - sets the depth of feature maps propagated through the
165-
# discriminator
166-
# - **num_epochs** - number of training epochs to run. Training for
158+
# details.
159+
# - ``nc`` - number of color channels in the input images. For color
160+
# images this is 3.
161+
# - ``nz`` - length of latent vector.
162+
# - ``ngf`` - relates to the depth of feature maps carried through the
163+
# generator.
164+
# - ``ndf`` - sets the depth of feature maps propagated through the
165+
# discriminator.
166+
# - ``num_epochs`` - number of training epochs to run. Training for
167167
# longer will probably lead to better results but will also take much
168-
# longer
169-
# - **lr** - learning rate for training. As described in the DCGAN paper,
170-
# this number should be 0.0002
171-
# - **beta1** - beta1 hyperparameter for Adam optimizers. As described in
172-
# paper, this number should be 0.5
173-
# - **ngpu** - number of GPUs available. If this is 0, code will run in
168+
# longer.
169+
# - ``lr`` - learning rate for training. As described in the DCGAN paper,
170+
# this number should be 0.0002.
171+
# - ``beta1`` - beta1 hyperparameter for Adam optimizers. As described in
172+
# paper, this number should be 0.5.
173+
# - ``ngpu`` - number of GPUs available. If this is 0, code will run in
174174
# CPU mode. If this number is greater than 0 it will run on that number
175-
# of GPUs
176-
#
175+
# of GPUs.
176+
#
177177

178178
# Root directory for dataset
179179
dataroot = "data/celeba"
@@ -206,7 +206,7 @@
206206
# Learning rate for optimizers
207207
lr = 0.0002
208208

209-
# Beta1 hyperparam for Adam optimizers
209+
# Beta1 hyperparameter for Adam optimizers
210210
beta1 = 0.5
211211

212212
# Number of GPUs available. Use 0 for CPU mode.
@@ -221,10 +221,10 @@
221221
# dataset <http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html>`__ which can
222222
# be downloaded at the linked site, or in `Google
223223
# Drive <https://drive.google.com/drive/folders/0B7EVK8r0v71pTUZsaXdaSnZBZzg>`__.
224-
# The dataset will download as a file named *img_align_celeba.zip*. Once
225-
# downloaded, create a directory named *celeba* and extract the zip file
226-
# into that directory. Then, set the *dataroot* input for this notebook to
227-
# the *celeba* directory you just created. The resulting directory
224+
# The dataset will download as a file named ``img_align_celeba.zip``. Once
225+
# downloaded, create a directory named ``celeba`` and extract the zip file
226+
# into that directory. Then, set the ``dataroot`` input for this notebook to
227+
# the ``celeba`` directory you just created. The resulting directory
228228
# structure should be:
229229
#
230230
# ::
@@ -237,9 +237,9 @@
237237
# -> 537394.jpg
238238
# ...
239239
#
240-
# This is an important step because we will be using the ImageFolder
240+
# This is an important step because we will be using the ``ImageFolder``
241241
# dataset class, which requires there to be subdirectories in the
242-
# dataset’s root folder. Now, we can create the dataset, create the
242+
# dataset root folder. Now, we can create the dataset, create the
243243
# dataloader, set the device to run on, and finally visualize some of the
244244
# training data.
245245
#
@@ -282,14 +282,14 @@
282282
# ~~~~~~~~~~~~~~~~~~~~~
283283
#
284284
# From the DCGAN paper, the authors specify that all model weights shall
285-
# be randomly initialized from a Normal distribution with mean=0,
286-
# stdev=0.02. The ``weights_init`` function takes an initialized model as
285+
# be randomly initialized from a Normal distribution with ``mean=0``,
286+
# ``stdev=0.02``. The ``weights_init`` function takes an initialized model as
287287
# input and reinitializes all convolutional, convolutional-transpose, and
288288
# batch normalization layers to meet this criteria. This function is
289289
# applied to the models immediately after initialization.
290290
#
291291

292-
# custom weights initialization called on netG and netD
292+
# custom weights initialization called on ``netG`` and ``netD``
293293
def weights_init(m):
294294
classname = m.__class__.__name__
295295
if classname.find('Conv') != -1:
@@ -319,10 +319,10 @@ def weights_init(m):
319319
# .. figure:: /_static/img/dcgan_generator.png
320320
# :alt: dcgan_generator
321321
#
322-
# Notice, how the inputs we set in the input section (*nz*, *ngf*, and
323-
# *nc*) influence the generator architecture in code. *nz* is the length
324-
# of the z input vector, *ngf* relates to the size of the feature maps
325-
# that are propagated through the generator, and *nc* is the number of
322+
# Notice, how the inputs we set in the input section (``nz``, ``ngf``, and
323+
# ``nc``) influence the generator architecture in code. ``nz`` is the length
324+
# of the z input vector, ``ngf`` relates to the size of the feature maps
325+
# that are propagated through the generator, and ``nc`` is the number of
326326
# channels in the output image (set to 3 for RGB images). Below is the
327327
# code for the generator.
328328
#
@@ -338,22 +338,22 @@ def __init__(self, ngpu):
338338
nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
339339
nn.BatchNorm2d(ngf * 8),
340340
nn.ReLU(True),
341-
# state size. (ngf*8) x 4 x 4
341+
# state size. ``(ngf*8) x 4 x 4``
342342
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
343343
nn.BatchNorm2d(ngf * 4),
344344
nn.ReLU(True),
345-
# state size. (ngf*4) x 8 x 8
345+
# state size. ``(ngf*4) x 8 x 8``
346346
nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
347347
nn.BatchNorm2d(ngf * 2),
348348
nn.ReLU(True),
349-
# state size. (ngf*2) x 16 x 16
349+
# state size. ``(ngf*2) x 16 x 16``
350350
nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
351351
nn.BatchNorm2d(ngf),
352352
nn.ReLU(True),
353-
# state size. (ngf) x 32 x 32
353+
# state size. ``(ngf) x 32 x 32``
354354
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
355355
nn.Tanh()
356-
# state size. (nc) x 64 x 64
356+
# state size. ``(nc) x 64 x 64``
357357
)
358358

359359
def forward(self, input):
@@ -369,12 +369,12 @@ def forward(self, input):
369369
# Create the generator
370370
netG = Generator(ngpu).to(device)
371371

372-
# Handle multi-gpu if desired
372+
# Handle multi-GPU if desired
373373
if (device.type == 'cuda') and (ngpu > 1):
374374
netG = nn.DataParallel(netG, list(range(ngpu)))
375375

376-
# Apply the weights_init function to randomly initialize all weights
377-
# to mean=0, stdev=0.02.
376+
# Apply the ``weights_init`` function to randomly initialize all weights
377+
# to ``mean=0``, ``stdev=0.02``.
378378
netG.apply(weights_init)
379379

380380
# Print the model
@@ -408,22 +408,22 @@ def __init__(self, ngpu):
408408
super(Discriminator, self).__init__()
409409
self.ngpu = ngpu
410410
self.main = nn.Sequential(
411-
# input is (nc) x 64 x 64
411+
# input is ``(nc) x 64 x 64``
412412
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
413413
nn.LeakyReLU(0.2, inplace=True),
414-
# state size. (ndf) x 32 x 32
414+
# state size. ``(ndf) x 32 x 32``
415415
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
416416
nn.BatchNorm2d(ndf * 2),
417417
nn.LeakyReLU(0.2, inplace=True),
418-
# state size. (ndf*2) x 16 x 16
418+
# state size. ``(ndf*2) x 16 x 16``
419419
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
420420
nn.BatchNorm2d(ndf * 4),
421421
nn.LeakyReLU(0.2, inplace=True),
422-
# state size. (ndf*4) x 8 x 8
422+
# state size. ``(ndf*4) x 8 x 8``
423423
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
424424
nn.BatchNorm2d(ndf * 8),
425425
nn.LeakyReLU(0.2, inplace=True),
426-
# state size. (ndf*8) x 4 x 4
426+
# state size. ``(ndf*8) x 4 x 4``
427427
nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
428428
nn.Sigmoid()
429429
)
@@ -440,12 +440,12 @@ def forward(self, input):
440440
# Create the Discriminator
441441
netD = Discriminator(ngpu).to(device)
442442

443-
# Handle multi-gpu if desired
443+
# Handle multi-GPU if desired
444444
if (device.type == 'cuda') and (ngpu > 1):
445445
netD = nn.DataParallel(netD, list(range(ngpu)))
446446

447-
# Apply the weights_init function to randomly initialize all weights
448-
# to mean=0, stdev=0.2.
447+
# Apply the ``weights_init`` function to randomly initialize all weights
448+
# like this: ``to mean=0, stdev=0.2``.
449449
netD.apply(weights_init)
450450

451451
# Print the model
@@ -485,7 +485,7 @@ def forward(self, input):
485485
# images form out of the noise.
486486
#
487487

488-
# Initialize BCELoss function
488+
# Initialize the ``BCELoss`` function
489489
criterion = nn.BCELoss()
490490

491491
# Create batch of latent vectors that we will use to visualize
@@ -509,7 +509,8 @@ def forward(self, input):
509509
# we can train it. Be mindful that training GANs is somewhat of an art
510510
# form, as incorrect hyperparameter settings lead to mode collapse with
511511
# little explanation of what went wrong. Here, we will closely follow
512-
# Algorithm 1 from Goodfellow’s paper, while abiding by some of the best
512+
# Algorithm 1 from the `Goodfellow’s paper <https://papers.nips.cc/paper/5423-generative-adversarial-nets.pdf>`__,
513+
# while abiding by some of the best
513514
# practices shown in `ganhacks <https://github.com/soumith/ganhacks>`__.
514515
# Namely, we will “construct different mini-batches for real and fake”
515516
# images, and also adjust G’s objective function to maximize
@@ -523,7 +524,8 @@ def forward(self, input):
523524
# terms of Goodfellow, we wish to “update the discriminator by ascending
524525
# its stochastic gradient”. Practically, we want to maximize
525526
# :math:`log(D(x)) + log(1-D(G(z)))`. Due to the separate mini-batch
526-
# suggestion from ganhacks, we will calculate this in two steps. First, we
527+
# suggestion from `ganhacks <https://github.com/soumith/ganhacks>`__,
528+
# we will calculate this in two steps. First, we
527529
# will construct a batch of real samples from the training set, forward
528530
# pass through :math:`D`, calculate the loss (:math:`log(D(x))`), then
529531
# calculate the gradients in a backward pass. Secondly, we will construct
@@ -545,7 +547,7 @@ def forward(self, input):
545547
# G’s gradients in a backward pass, and finally updating G’s parameters
546548
# with an optimizer step. It may seem counter-intuitive to use the real
547549
# labels as GT labels for the loss function, but this allows us to use the
548-
# :math:`log(x)` part of the BCELoss (rather than the :math:`log(1-x)`
550+
# :math:`log(x)` part of the ``BCELoss`` (rather than the :math:`log(1-x)`
549551
# part) which is exactly what we want.
550552
#
551553
# Finally, we will do some statistic reporting and at the end of each

0 commit comments

Comments
 (0)