Skip to content

Commit 339bf13

Browse files
author
Svetlana Karslioglu
authored
Pyspelling: Advanced Python tutorials (#2293)
* Pyspelling: Advanced Python tutorials
1 parent 4903352 commit 339bf13

7 files changed

+74
-54
lines changed

.pyspelling.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ matrix:
44
sources:
55
- beginner_source/*.py
66
- intermediate_source/*.py
7+
- advanced_source/*.py
78
dictionary:
89
wordlists:
910
- en-wordlist.txt

advanced_source/ddp_pipeline.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def forward(self, x):
7575
# As a result, our focus is on ``nn.TransformerEncoder`` and we split the model
7676
# such that half of the ``nn.TransformerEncoderLayer`` are on one GPU and the
7777
# other half are on another. To do this, we pull out the ``Encoder`` and
78-
# ``Decoder`` sections into seperate modules and then build an nn.Sequential
78+
# ``Decoder`` sections into separate modules and then build an ``nn.Sequential``
7979
# representing the original Transformer module.
8080

8181

@@ -151,16 +151,17 @@ def run_worker(rank, world_size):
151151
# length 6:
152152
#
153153
# .. math::
154-
# \begin{bmatrix}
155-
# \text{A} & \text{B} & \text{C} & \ldots & \text{X} & \text{Y} & \text{Z}
156-
# \end{bmatrix}
157-
# \Rightarrow
158-
# \begin{bmatrix}
159-
# \begin{bmatrix}\text{A} \\ \text{B} \\ \text{C} \\ \text{D} \\ \text{E} \\ \text{F}\end{bmatrix} &
160-
# \begin{bmatrix}\text{G} \\ \text{H} \\ \text{I} \\ \text{J} \\ \text{K} \\ \text{L}\end{bmatrix} &
161-
# \begin{bmatrix}\text{M} \\ \text{N} \\ \text{O} \\ \text{P} \\ \text{Q} \\ \text{R}\end{bmatrix} &
162-
# \begin{bmatrix}\text{S} \\ \text{T} \\ \text{U} \\ \text{V} \\ \text{W} \\ \text{X}\end{bmatrix}
163-
# \end{bmatrix}
154+
#
155+
# \begin{bmatrix}
156+
# \text{A} & \text{B} & \text{C} & \ldots & \text{X} & \text{Y} & \text{Z}
157+
# \end{bmatrix}
158+
# \Rightarrow
159+
# \begin{bmatrix}
160+
# \begin{bmatrix}\text{A} \\ \text{B} \\ \text{C} \\ \text{D} \\ \text{E} \\ \text{F}\end{bmatrix} &
161+
# \begin{bmatrix}\text{G} \\ \text{H} \\ \text{I} \\ \text{J} \\ \text{K} \\ \text{L}\end{bmatrix} &
162+
# \begin{bmatrix}\text{M} \\ \text{N} \\ \text{O} \\ \text{P} \\ \text{Q} \\ \text{R}\end{bmatrix} &
163+
# \begin{bmatrix}\text{S} \\ \text{T} \\ \text{U} \\ \text{V} \\ \text{W} \\ \text{X}\end{bmatrix}
164+
# \end{bmatrix}
164165
#
165166
# These columns are treated as independent by the model, which means that
166167
# the dependence of ``G`` and ``F`` can not be learned, but allows more
@@ -192,11 +193,11 @@ def data_process(raw_text_iter):
192193
device = torch.device(2 * rank)
193194

194195
def batchify(data, bsz, rank, world_size, is_train=False):
195-
# Divide the dataset into bsz parts.
196+
# Divide the dataset into ``bsz`` parts.
196197
nbatch = data.size(0) // bsz
197198
# Trim off any extra elements that wouldn't cleanly fit (remainders).
198199
data = data.narrow(0, 0, nbatch * bsz)
199-
# Evenly divide the data across the bsz batches.
200+
# Evenly divide the data across the ``bsz`` batches.
200201
data = data.view(bsz, -1).t().contiguous()
201202
# Divide the data across the ranks only for training data.
202203
if is_train:
@@ -261,14 +262,14 @@ def get_batch(source, i):
261262
#
262263
# The pipeline is then initialized with 8 transformer layers on one GPU and 8
263264
# transformer layers on the other GPU. One pipe is setup across GPUs 0 and 1 and
264-
# another across GPUs 2 and 3. Both pipes are then replicated using DistributedDataParallel.
265+
# another across GPUs 2 and 3. Both pipes are then replicated using ``DistributedDataParallel``.
265266

266267
# In 'run_worker'
267268
ntokens = len(vocab) # the size of vocabulary
268269
emsize = 4096 # embedding dimension
269-
nhid = 4096 # the dimension of the feedforward network model in nn.TransformerEncoder
270-
nlayers = 8 # the number of nn.TransformerEncoderLayer in nn.TransformerEncoder
271-
nhead = 16 # the number of heads in the multiheadattention models
270+
nhid = 4096 # the dimension of the feedforward network model in ``nn.TransformerEncoder``
271+
nlayers = 8 # the number of ``nn.TransformerEncoderLayer`` in ``nn.TransformerEncoder``
272+
nhead = 16 # the number of heads in the Multihead Attention models
272273
dropout = 0.2 # the dropout value
273274

274275
from torch.distributed import rpc
@@ -287,7 +288,7 @@ def get_batch(source, i):
287288
)
288289
)
289290

290-
# Num gpus for model parallelism.
291+
# Number of GPUs for model parallelism.
291292
num_gpus = 2
292293
partition_len = ((nlayers - 1) // num_gpus) + 1
293294

advanced_source/dynamic_quantization_tutorial.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ def tokenize(self, path):
130130
corpus = Corpus(model_data_filepath + 'wikitext-2')
131131

132132
######################################################################
133-
# 3. Load the pre-trained model
133+
# 3. Load the pretrained model
134134
# -----------------------------
135135
#
136136
# This is a tutorial on dynamic quantization, a quantization technique
137137
# that is applied after a model has been trained. Therefore, we'll simply load some
138-
# pre-trained weights into this model architecture; these weights were obtained
138+
# pretrained weights into this model architecture; these weights were obtained
139139
# by training for five epochs using the default settings in the word language model
140140
# example.
141141

@@ -159,7 +159,7 @@ def tokenize(self, path):
159159
print(model)
160160

161161
######################################################################
162-
# Now let's generate some text to ensure that the pre-trained model is working
162+
# Now let's generate some text to ensure that the pretrained model is working
163163
# properly - similarly to before, we follow
164164
# `here <https://github.com/pytorch/examples/blob/master/word_language_model/generate.py>`_
165165

@@ -200,11 +200,11 @@ def tokenize(self, path):
200200

201201
# create test data set
202202
def batchify(data, bsz):
203-
# Work out how cleanly we can divide the dataset into bsz parts.
203+
# Work out how cleanly we can divide the dataset into ``bsz`` parts.
204204
nbatch = data.size(0) // bsz
205205
# Trim off any extra elements that wouldn't cleanly fit (remainders).
206206
data = data.narrow(0, 0, nbatch * bsz)
207-
# Evenly divide the data across the bsz batches.
207+
# Evenly divide the data across the ``bsz`` batches.
208208
return data.view(bsz, -1).t().contiguous()
209209

210210
test_data = batchify(corpus.test, eval_batch_size)

advanced_source/neural_style_tutorial.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# - ``PIL``, ``PIL.Image``, ``matplotlib.pyplot`` (load and display
4545
# images)
4646
# - ``torchvision.transforms`` (transform PIL images into tensors)
47-
# - ``torchvision.models`` (train or load pre-trained models)
47+
# - ``torchvision.models`` (train or load pretrained models)
4848
# - ``copy`` (to deep copy the models; system package)
4949

5050
from __future__ import print_function
@@ -84,7 +84,7 @@
8484
# torch library are trained with tensor values ranging from 0 to 1. If you
8585
# try to feed the networks with 0 to 255 tensor images, then the activated
8686
# feature maps will be unable to sense the intended content and style.
87-
# However, pre-trained networks from the Caffe library are trained with 0
87+
# However, pretrained networks from the Caffe library are trained with 0
8888
# to 255 tensor images.
8989
#
9090
#
@@ -96,7 +96,7 @@
9696
# with name ``images`` in your current working directory.
9797

9898
# desired size of the output image
99-
imsize = 512 if torch.cuda.is_available() else 128 # use small size if no gpu
99+
imsize = 512 if torch.cuda.is_available() else 128 # use small size if no GPU
100100

101101
loader = transforms.Compose([
102102
transforms.Resize(imsize), # scale imported image
@@ -220,7 +220,7 @@ def gram_matrix(input):
220220
# b=number of feature maps
221221
# (c,d)=dimensions of a f. map (N=c*d)
222222

223-
features = input.view(a * b, c * d) # resise F_XL into \hat F_XL
223+
features = input.view(a * b, c * d) # resize F_XL into \hat F_XL
224224

225225
G = torch.mm(features, features.t()) # compute the gram product
226226

@@ -251,7 +251,7 @@ def forward(self, input):
251251
# Importing the Model
252252
# -------------------
253253
#
254-
# Now we need to import a pre-trained neural network. We will use a 19
254+
# Now we need to import a pretrained neural network. We will use a 19
255255
# layer VGG network like the one used in the paper.
256256
#
257257
# PyTorch’s implementation of VGG is a module divided into two child
@@ -277,7 +277,7 @@ def forward(self, input):
277277
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
278278

279279
# create a module to normalize input image so we can easily put it in a
280-
# nn.Sequential
280+
# ``nn.Sequential``
281281
class Normalization(nn.Module):
282282
def __init__(self, mean, std):
283283
super(Normalization, self).__init__()
@@ -288,14 +288,14 @@ def __init__(self, mean, std):
288288
self.std = torch.tensor(std).view(-1, 1, 1)
289289

290290
def forward(self, img):
291-
# normalize img
291+
# normalize ``img``
292292
return (img - self.mean) / self.std
293293

294294

295295
######################################################################
296296
# A ``Sequential`` module contains an ordered list of child modules. For
297-
# instance, ``vgg19.features`` contains a sequence (Conv2d, ReLU, MaxPool2d,
298-
# Conv2d, ReLU…) aligned in the right order of depth. We need to add our
297+
# instance, ``vgg19.features`` contains a sequence (``Conv2d``, ``ReLU``, ``MaxPool2d``,
298+
# ``Conv2d``, ``ReLU``…) aligned in the right order of depth. We need to add our
299299
# content loss and style loss layers immediately after the convolution
300300
# layer they are detecting. To do this we must create a new ``Sequential``
301301
# module that has content loss and style loss modules correctly inserted.
@@ -312,12 +312,12 @@ def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
312312
# normalization module
313313
normalization = Normalization(normalization_mean, normalization_std).to(device)
314314

315-
# just in order to have an iterable access to or list of content/syle
315+
# just in order to have an iterable access to or list of content/style
316316
# losses
317317
content_losses = []
318318
style_losses = []
319319

320-
# assuming that cnn is a nn.Sequential, so we make a new nn.Sequential
320+
# assuming that ``cnn`` is a ``nn.Sequential``, so we make a new ``nn.Sequential``
321321
# to put in modules that are supposed to be activated sequentially
322322
model = nn.Sequential(normalization)
323323

@@ -328,8 +328,8 @@ def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
328328
name = 'conv_{}'.format(i)
329329
elif isinstance(layer, nn.ReLU):
330330
name = 'relu_{}'.format(i)
331-
# The in-place version doesn't play very nicely with the ContentLoss
332-
# and StyleLoss we insert below. So we replace with out-of-place
331+
# The in-place version doesn't play very nicely with the ``ContentLoss``
332+
# and ``StyleLoss`` we insert below. So we replace with out-of-place
333333
# ones here.
334334
layer = nn.ReLU(inplace=False)
335335
elif isinstance(layer, nn.MaxPool2d):
@@ -371,8 +371,11 @@ def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
371371
#
372372

373373
input_img = content_img.clone()
374-
# if you want to use white noise instead uncomment the below line:
375-
# input_img = torch.randn(content_img.data.size(), device=device)
374+
# if you want to use white noise by using the following code:
375+
#
376+
# ::
377+
#
378+
# input_img = torch.randn(content_img.data.size(), device=device)
376379

377380
# add the original input image to the figure:
378381
plt.figure()
@@ -385,7 +388,7 @@ def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
385388
#
386389
# As Leon Gatys, the author of the algorithm, suggested `here <https://discuss.pytorch.org/t/pytorch-tutorial-for-neural-transfert-of-artistic-style/336/20?u=alexis-jacq>`__, we will use
387390
# L-BFGS algorithm to run our gradient descent. Unlike training a network,
388-
# we want to train the input image in order to minimise the content/style
391+
# we want to train the input image in order to minimize the content/style
389392
# losses. We will create a PyTorch L-BFGS optimizer ``optim.LBFGS`` and pass
390393
# our image to it as the tensor to optimize.
391394
#
@@ -400,7 +403,7 @@ def get_input_optimizer(input_img):
400403
# Finally, we must define a function that performs the neural transfer. For
401404
# each iteration of the networks, it is fed an updated input and computes
402405
# new losses. We will run the ``backward`` methods of each loss module to
403-
# dynamicaly compute their gradients. The optimizer requires a “closure”
406+
# dynamically compute their gradients. The optimizer requires a “closure”
404407
# function, which reevaluates the module and returns the loss.
405408
#
406409
# We still have one final constraint to address. The network may try to

advanced_source/numpy_extensions_tutorial.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
Creating Extensions Using numpy and scipy
3+
Creating Extensions Using NumPy and SciPy
44
=========================================
55
**Author**: `Adam Paszke <https://github.com/apaszke>`_
66
@@ -27,7 +27,7 @@
2727
# This layer doesn’t particularly do anything useful or mathematically
2828
# correct.
2929
#
30-
# It is aptly named BadFFTFunction
30+
# It is aptly named ``BadFFTFunction``
3131
#
3232
# **Layer Implementation**
3333

@@ -48,7 +48,7 @@ def backward(ctx, grad_output):
4848
return grad_output.new(result)
4949

5050
# since this layer does not have any parameters, we can
51-
# simply declare this as a function, rather than as an nn.Module class
51+
# simply declare this as a function, rather than as an ``nn.Module`` class
5252

5353

5454
def incorrect_fft(input):
@@ -75,7 +75,7 @@ def incorrect_fft(input):
7575
# Implementation of a layer with learnable weights, where cross-correlation
7676
# has a filter (kernel) that represents weights.
7777
#
78-
# The backward pass computes the gradient wrt the input and the gradient wrt the filter.
78+
# The backward pass computes the gradient ``wrt`` the input and the gradient ``wrt`` the filter.
7979

8080
from numpy import flip
8181
import numpy as np

advanced_source/super_resolution_with_onnxruntime.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
# and is widely used in image processing or video editing. For this
3838
# tutorial, we will use a small super-resolution model.
3939
#
40-
# First, let's create a SuperResolution model in PyTorch.
40+
# First, let's create a ``SuperResolution`` model in PyTorch.
4141
# This model uses the efficient sub-pixel convolution layer described in
4242
# `"Real-Time Single Image and Video Super-Resolution Using an Efficient
4343
# Sub-Pixel Convolutional Neural Network" - Shi et al <https://arxiv.org/abs/1609.05158>`__
4444
# for increasing the resolution of an image by an upscale factor.
45-
# The model expects the Y component of the YCbCr of an image as an input, and
45+
# The model expects the Y component of the ``YCbCr`` of an image as an input, and
4646
# outputs the upscaled Y component in super resolution.
4747
#
4848
# `The
@@ -87,7 +87,7 @@ def _initialize_weights(self):
8787

8888
######################################################################
8989
# Ordinarily, you would now train this model; however, for this tutorial,
90-
# we will instead download some pre-trained weights. Note that this model
90+
# we will instead download some pretrained weights. Note that this model
9191
# was not trained fully for good accuracy and is used here for
9292
# demonstration purposes only.
9393
#
@@ -154,9 +154,9 @@ def _initialize_weights(self):
154154
# the same values when run in ONNX Runtime.
155155
#
156156
# But before verifying the model's output with ONNX Runtime, we will check
157-
# the ONNX model with ONNX's API.
157+
# the ONNX model with ONNX API.
158158
# First, ``onnx.load("super_resolution.onnx")`` will load the saved model and
159-
# will output a onnx.ModelProto structure (a top-level file/container format for bundling a ML model.
159+
# will output a ``onnx.ModelProto`` structure (a top-level file/container format for bundling a ML model.
160160
# For more information `onnx.proto documentation <https://github.com/onnx/onnx/blob/master/onnx/onnx.proto>`__.).
161161
# Then, ``onnx.checker.check_model(onnx_model)`` will verify the model's structure
162162
# and confirm that the model has a valid schema.
@@ -181,7 +181,7 @@ def _initialize_weights(self):
181181
# In order to run the model with ONNX Runtime, we need to create an
182182
# inference session for the model with the chosen configuration
183183
# parameters (here we use the default config).
184-
# Once the session is created, we evaluate the model using the run() api.
184+
# Once the session is created, we evaluate the model using the run() API.
185185
# The output of this call is a list containing the outputs of the model
186186
# computed by ONNX Runtime.
187187
#
@@ -205,7 +205,7 @@ def to_numpy(tensor):
205205

206206
######################################################################
207207
# We should see that the output of PyTorch and ONNX Runtime runs match
208-
# numerically with the given precision (rtol=1e-03 and atol=1e-05).
208+
# numerically with the given precision (``rtol=1e-03`` and ``atol=1e-05``).
209209
# As a side-note, if they do not match then there is an issue in the
210210
# ONNX exporter, so please contact us in that case.
211211
#
@@ -230,13 +230,13 @@ def to_numpy(tensor):
230230
#
231231

232232
######################################################################
233-
# First, let's load the image, pre-process it using standard PIL
233+
# First, let's load the image, preprocess it using standard PIL
234234
# python library. Note that this preprocessing is the standard practice of
235235
# processing data for training/testing neural networks.
236236
#
237237
# We first resize the image to fit the size of the model's input (224x224).
238238
# Then we split the image into its Y, Cb, and Cr components.
239-
# These components represent a greyscale image (Y), and
239+
# These components represent a grayscale image (Y), and
240240
# the blue-difference (Cb) and red-difference (Cr) chroma components.
241241
# The Y component being more sensitive to the human eye, we are
242242
# interested in this component which we will be transforming.
@@ -262,7 +262,7 @@ def to_numpy(tensor):
262262

263263
######################################################################
264264
# Now, as a next step, let's take the tensor representing the
265-
# greyscale resized cat image and run the super-resolution model in
265+
# grayscale resized cat image and run the super-resolution model in
266266
# ONNX Runtime as explained previously.
267267
#
268268

0 commit comments

Comments
 (0)