Skip to content

Commit cf45525

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

15 files changed

+340
-216
lines changed

.pyspelling.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ spellchecker: aspell
22
matrix:
33
- name: python
44
sources:
5-
- beginner_source/data_loading_tutorial.py
6-
- 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
5+
- beginner_source/*.py
116
dictionary:
127
wordlists:
138
- en-wordlist.txt
@@ -18,7 +13,7 @@ matrix:
1813
context_visible_first: true
1914
delimiters:
2015
# Exclude figure rST tags
21-
- open: '\.\.\s+(figure|literalinclude|math)::'
16+
- open: '\.\.\s+(figure|literalinclude|math|image)::'
2217
close: '\n'
2318
# Exclude Python coding directives
2419
- open: '-\*- coding:'
@@ -29,6 +24,13 @@ matrix:
2924
# Exclude .rst directives:
3025
- open: ':math:`.*`'
3126
close: ' '
27+
# Ignore multiline content in codeblock
28+
- open: '(?s)^::\n\n '
29+
close: '^\n'
30+
# Ignore reStructuredText block directives
31+
- open: '\.\. (code-block)::.*$\n*'
32+
content: '(?P<first>(^(?P<indent>[ ]+).*$\n))(?P<other>(^([ \t]+.*|[ \t]*)$\n)*)'
33+
close: '(^(?![ \t]+.*$))'
3234
- pyspelling.filters.markdown:
3335
- pyspelling.filters.html:
3436
ignores:

beginner_source/fgsm_tutorial.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,20 @@
120120
# There are only three inputs for this tutorial, and are defined as
121121
# follows:
122122
#
123-
# - **epsilons** - List of epsilon values to use for the run. It is
123+
# - ``epsilons`` - List of epsilon values to use for the run. It is
124124
# important to keep 0 in the list because it represents the model
125125
# performance on the original test set. Also, intuitively we would
126126
# expect the larger the epsilon, the more noticeable the perturbations
127127
# but the more effective the attack in terms of degrading model
128128
# accuracy. Since the data range here is :math:`[0,1]`, no epsilon
129129
# value should exceed 1.
130130
#
131-
# - **pretrained_model** - path to the pretrained MNIST model which was
131+
# - ``pretrained_model`` - path to the pretrained MNIST model which was
132132
# trained with
133133
# `pytorch/examples/mnist <https://github.com/pytorch/examples/tree/master/mnist>`__.
134134
# For simplicity, download the pretrained model `here <https://drive.google.com/drive/folders/1fn83DF14tWmit0RTKWRhPq5uVXt73e0h?usp=sharing>`__.
135135
#
136-
# - **use_cuda** - boolean flag to use CUDA if desired and available.
136+
# - ``use_cuda`` - boolean flag to use CUDA if desired and available.
137137
# Note, a GPU with CUDA is not critical for this tutorial as a CPU will
138138
# not take much time.
139139
#
@@ -263,7 +263,7 @@ def test( model, device, test_loader, epsilon ):
263263
output = model(data)
264264
init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
265265

266-
# If the initial prediction is wrong, dont bother attacking, just move on
266+
# If the initial prediction is wrong, don't bother attacking, just move on
267267
if init_pred.item() != target.item():
268268
continue
269269

@@ -276,7 +276,7 @@ def test( model, device, test_loader, epsilon ):
276276
# Calculate gradients of model in backward pass
277277
loss.backward()
278278

279-
# Collect datagrad
279+
# Collect ``datagrad``
280280
data_grad = data.grad.data
281281

282282
# Call FGSM Attack
@@ -366,7 +366,7 @@ def test( model, device, test_loader, epsilon ):
366366
# Remember the idea of no free lunch? In this case, as epsilon increases
367367
# the test accuracy decreases **BUT** the perturbations become more easily
368368
# perceptible. In reality, there is a tradeoff between accuracy
369-
# degredation and perceptibility that an attacker must consider. Here, we
369+
# degradation and perceptibility that an attacker must consider. Here, we
370370
# show some examples of successful adversarial examples at each epsilon
371371
# value. Each row of the plot shows a different epsilon value. The first
372372
# row is the :math:`\epsilon=0` examples which represent the original

beginner_source/flava_finetuning_tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
######################################################################
2525
# Installation
2626
# -----------------
27-
# We will use TextVQA dataset and bert tokenizer from Hugging Face for this
27+
# We will use TextVQA dataset and ``bert tokenizer`` from Hugging Face for this
2828
# tutorial. So you need to install datasets and transformers in addition to TorchMultimodal.
2929
#
3030
# .. note::

beginner_source/hyperparameter_tuning_tutorial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def main(num_samples=10, max_num_epochs=10, gpus_per_trial=2):
389389
grace_period=1,
390390
reduction_factor=2)
391391
reporter = CLIReporter(
392-
# parameter_columns=["l1", "l2", "lr", "batch_size"],
392+
# ``parameter_columns=["l1", "l2", "lr", "batch_size"]``,
393393
metric_columns=["loss", "accuracy", "training_iteration"])
394394
result = tune.run(
395395
partial(train_cifar, data_dir=data_dir),
@@ -425,7 +425,7 @@ def main(num_samples=10, max_num_epochs=10, gpus_per_trial=2):
425425

426426
if __name__ == "__main__":
427427
# sphinx_gallery_start_ignore
428-
# Fixes AttributeError: '_LoggingTee' object has no attribute 'fileno'.
428+
# Fixes ``AttributeError: '_LoggingTee' object has no attribute 'fileno'``.
429429
# This is only needed to run with sphinx-build.
430430
import sys
431431
sys.stdout.fileno = lambda: False

beginner_source/nn_tutorial.py

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
"""
33
What is `torch.nn` *really*?
44
============================
5-
by Jeremy Howard, `fast.ai <https://www.fast.ai>`_. Thanks to Rachel Thomas and Francisco Ingham.
5+
6+
**Authors:** Jeremy Howard, `fast.ai <https://www.fast.ai>`_. Thanks to Rachel Thomas and Francisco Ingham.
67
"""
8+
79
###############################################################################
8-
# We recommend running this tutorial as a notebook, not a script. To download the notebook (.ipynb) file,
10+
# We recommend running this tutorial as a notebook, not a script. To download the notebook (``.ipynb``) file,
911
# click the link at the top of the page.
1012
#
1113
# PyTorch provides the elegantly designed modules and classes `torch.nn <https://pytorch.org/docs/stable/nn.html>`_ ,
@@ -90,7 +92,7 @@
9092
print(y_train.min(), y_train.max())
9193

9294
###############################################################################
93-
# Neural net from scratch (no torch.nn)
95+
# Neural net from scratch (without ``torch.nn``)
9496
# ---------------------------------------------
9597
#
9698
# Let's first create a model using nothing but PyTorch tensor operations. We're assuming
@@ -109,7 +111,7 @@
109111
#
110112
# .. note:: We are initializing the weights here with
111113
# `Xavier initialisation <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
112-
# (by multiplying with 1/sqrt(n)).
114+
# (by multiplying with ``1/sqrt(n)``).
113115

114116
import math
115117

@@ -123,7 +125,7 @@
123125
# let's just write a plain matrix multiplication and broadcasted addition
124126
# to create a simple linear model. We also need an activation function, so
125127
# we'll write `log_softmax` and use it. Remember: although PyTorch
126-
# provides lots of pre-written loss functions, activation functions, and
128+
# provides lots of prewritten loss functions, activation functions, and
127129
# so forth, you can easily write your own using plain python. PyTorch will
128130
# even create fast GPU or vectorized CPU code for your function
129131
# automatically.
@@ -242,7 +244,7 @@ def accuracy(out, yb):
242244
print(loss_func(model(xb), yb), accuracy(model(xb), yb))
243245

244246
###############################################################################
245-
# Using torch.nn.functional
247+
# Using ``torch.nn.functional``
246248
# ------------------------------
247249
#
248250
# We will now refactor our code, so that it does the same thing as before, only
@@ -278,7 +280,7 @@ def model(xb):
278280
print(loss_func(model(xb), yb), accuracy(model(xb), yb))
279281

280282
###############################################################################
281-
# Refactor using nn.Module
283+
# Refactor using ``nn.Module``
282284
# -----------------------------
283285
# Next up, we'll use ``nn.Module`` and ``nn.Parameter``, for a clearer and more
284286
# concise training loop. We subclass ``nn.Module`` (which itself is a class and
@@ -320,22 +322,26 @@ def forward(self, xb):
320322
###############################################################################
321323
# Previously for our training loop we had to update the values for each parameter
322324
# by name, and manually zero out the grads for each parameter separately, like this:
325+
#
323326
# ::
324-
# with torch.no_grad():
325-
# weights -= weights.grad * lr
326-
# bias -= bias.grad * lr
327-
# weights.grad.zero_()
328-
# bias.grad.zero_()
327+
#
328+
# with torch.no_grad():
329+
# weights -= weights.grad * lr
330+
# bias -= bias.grad * lr
331+
# weights.grad.zero_()
332+
# bias.grad.zero_()
329333
#
330334
#
331335
# Now we can take advantage of model.parameters() and model.zero_grad() (which
332336
# are both defined by PyTorch for ``nn.Module``) to make those steps more concise
333337
# and less prone to the error of forgetting some of our parameters, particularly
334338
# if we had a more complicated model:
339+
#
335340
# ::
336-
# with torch.no_grad():
337-
# for p in model.parameters(): p -= p.grad * lr
338-
# model.zero_grad()
341+
#
342+
# with torch.no_grad():
343+
# for p in model.parameters(): p -= p.grad * lr
344+
# model.zero_grad()
339345
#
340346
#
341347
# We'll wrap our little training loop in a ``fit`` function so we can run it
@@ -365,8 +371,8 @@ def fit():
365371
print(loss_func(model(xb), yb))
366372

367373
###############################################################################
368-
# Refactor using nn.Linear
369-
# -------------------------
374+
# Refactor using ``nn.Linear``
375+
# ----------------------------
370376
#
371377
# We continue to refactor our code. Instead of manually defining and
372378
# initializing ``self.weights`` and ``self.bias``, and calculating ``xb @
@@ -398,23 +404,27 @@ def forward(self, xb):
398404
print(loss_func(model(xb), yb))
399405

400406
###############################################################################
401-
# Refactor using optim
407+
# Refactor using ``torch.optim``
402408
# ------------------------------
403409
#
404410
# Pytorch also has a package with various optimization algorithms, ``torch.optim``.
405411
# We can use the ``step`` method from our optimizer to take a forward step, instead
406412
# of manually updating each parameter.
407413
#
408414
# This will let us replace our previous manually coded optimization step:
415+
#
409416
# ::
410-
# with torch.no_grad():
411-
# for p in model.parameters(): p -= p.grad * lr
412-
# model.zero_grad()
417+
#
418+
# with torch.no_grad():
419+
# for p in model.parameters(): p -= p.grad * lr
420+
# model.zero_grad()
413421
#
414422
# and instead use just:
423+
#
415424
# ::
416-
# opt.step()
417-
# opt.zero_grad()
425+
#
426+
# opt.step()
427+
# opt.zero_grad()
418428
#
419429
# (``optim.zero_grad()`` resets the gradient to 0 and we need to call it before
420430
# computing the gradient for the next minibatch.)
@@ -473,15 +483,19 @@ def get_model():
473483
train_ds = TensorDataset(x_train, y_train)
474484

475485
###############################################################################
476-
# Previously, we had to iterate through minibatches of x and y values separately:
486+
# Previously, we had to iterate through minibatches of ``x`` and ``y`` values separately:
487+
#
477488
# ::
478-
# xb = x_train[start_i:end_i]
479-
# yb = y_train[start_i:end_i]
489+
#
490+
# xb = x_train[start_i:end_i]
491+
# yb = y_train[start_i:end_i]
480492
#
481493
#
482494
# Now, we can do these two steps together:
495+
#
483496
# ::
484-
# xb,yb = train_ds[i*bs : i*bs+bs]
497+
#
498+
# xb,yb = train_ds[i*bs : i*bs+bs]
485499
#
486500

487501
model, opt = get_model()
@@ -499,30 +513,34 @@ def get_model():
499513
print(loss_func(model(xb), yb))
500514

501515
###############################################################################
502-
# Refactor using DataLoader
516+
# Refactor using ``DataLoader``
503517
# ------------------------------
504518
#
505-
# Pytorch's ``DataLoader`` is responsible for managing batches. You can
519+
# PyTorch's ``DataLoader`` is responsible for managing batches. You can
506520
# create a ``DataLoader`` from any ``Dataset``. ``DataLoader`` makes it easier
507521
# to iterate over batches. Rather than having to use ``train_ds[i*bs : i*bs+bs]``,
508-
# the DataLoader gives us each minibatch automatically.
522+
# the ``DataLoader`` gives us each minibatch automatically.
509523

510524
from torch.utils.data import DataLoader
511525

512526
train_ds = TensorDataset(x_train, y_train)
513527
train_dl = DataLoader(train_ds, batch_size=bs)
514528

515529
###############################################################################
516-
# Previously, our loop iterated over batches (xb, yb) like this:
530+
# Previously, our loop iterated over batches ``(xb, yb)`` like this:
531+
#
517532
# ::
518-
# for i in range((n-1)//bs + 1):
519-
# xb,yb = train_ds[i*bs : i*bs+bs]
520-
# pred = model(xb)
521533
#
522-
# Now, our loop is much cleaner, as (xb, yb) are loaded automatically from the data loader:
534+
# for i in range((n-1)//bs + 1):
535+
# xb,yb = train_ds[i*bs : i*bs+bs]
536+
# pred = model(xb)
537+
#
538+
# Now, our loop is much cleaner, as ``(xb, yb)`` are loaded automatically from the data loader:
539+
#
523540
# ::
524-
# for xb,yb in train_dl:
525-
# pred = model(xb)
541+
#
542+
# for xb,yb in train_dl:
543+
# pred = model(xb)
526544

527545
model, opt = get_model()
528546

@@ -538,7 +556,7 @@ def get_model():
538556
print(loss_func(model(xb), yb))
539557

540558
###############################################################################
541-
# Thanks to Pytorch's ``nn.Module``, ``nn.Parameter``, ``Dataset``, and ``DataLoader``,
559+
# Thanks to PyTorch's ``nn.Module``, ``nn.Parameter``, ``Dataset``, and ``DataLoader``,
542560
# our training loop is now dramatically smaller and easier to understand. Let's
543561
# now try to add the basic features necessary to create effective models in practice.
544562
#
@@ -573,7 +591,7 @@ def get_model():
573591
#
574592
# (Note that we always call ``model.train()`` before training, and ``model.eval()``
575593
# before inference, because these are used by layers such as ``nn.BatchNorm2d``
576-
# and ``nn.Dropout`` to ensure appropriate behaviour for these different phases.)
594+
# and ``nn.Dropout`` to ensure appropriate behavior for these different phases.)
577595

578596
model, opt = get_model()
579597

@@ -667,11 +685,11 @@ def get_data(train_ds, valid_ds, bs):
667685
# Because none of the functions in the previous section assume anything about
668686
# the model form, we'll be able to use them to train a CNN without any modification.
669687
#
670-
# We will use Pytorch's predefined
688+
# We will use PyTorch's predefined
671689
# `Conv2d <https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d>`_ class
672690
# as our convolutional layer. We define a CNN with 3 convolutional layers.
673691
# Each convolution is followed by a ReLU. At the end, we perform an
674-
# average pooling. (Note that ``view`` is PyTorch's version of numpy's
692+
# average pooling. (Note that ``view`` is PyTorch's version of Numpy's
675693
# ``reshape``)
676694

677695
class Mnist_CNN(nn.Module):
@@ -702,7 +720,7 @@ def forward(self, xb):
702720
fit(epochs, model, loss_func, opt, train_dl, valid_dl)
703721

704722
###############################################################################
705-
# nn.Sequential
723+
# Using ``nn.Sequential``
706724
# ------------------------
707725
#
708726
# ``torch.nn`` has another handy class we can use to simplify our code:
@@ -729,7 +747,7 @@ def preprocess(x):
729747
return x.view(-1, 1, 28, 28)
730748

731749
###############################################################################
732-
# The model created with ``Sequential`` is simply:
750+
# The model created with ``Sequential`` is simple:
733751

734752
model = nn.Sequential(
735753
Lambda(preprocess),
@@ -748,7 +766,7 @@ def preprocess(x):
748766
fit(epochs, model, loss_func, opt, train_dl, valid_dl)
749767

750768
###############################################################################
751-
# Wrapping DataLoader
769+
# Wrapping ``DataLoader``
752770
# -----------------------------
753771
#
754772
# Our CNN is fairly concise, but it only works with MNIST, because:
@@ -862,7 +880,7 @@ def preprocess(x, y):
862880
# ``torch.nn``, ``torch.optim``, ``Dataset``, and ``DataLoader``. So let's summarize
863881
# what we've seen:
864882
#
865-
# - **torch.nn**
883+
# - ``torch.nn``:
866884
#
867885
# + ``Module``: creates a callable which behaves like a function, but can also
868886
# contain state(such as neural net layer weights). It knows what ``Parameter`` (s) it

0 commit comments

Comments
 (0)