Skip to content

Commit c2aa096

Browse files
Svetlana Karslioglukiersten-stokessubramenmalfet
authored
Update pyspelling to include beginner tutorials written in Python (#2279)
* Fix kernel dimensions for LeNet model code example (#2192) * Change kernel to 5x5 in 1st Conv2d layer in model init Signed-off-by: Kiersten Stokes <[email protected]> * Change kernel to 5x5 in 2nd Conv2d layer in model init * Fix dimensions of 1st Linear layer to match new expected size --------- Signed-off-by: Kiersten Stokes <[email protected]> Co-authored-by: Suraj Subramanian <[email protected]> Co-authored-by: Svetlana Karslioglu <[email protected]> * Update pyspelling for all beginner tutorials in Python * Update * Update * Fix template tutorial, update gitignore * Apply suggestions from code review --------- Signed-off-by: Kiersten Stokes <[email protected]> Co-authored-by: Kiersten Stokes <[email protected]> Co-authored-by: Suraj Subramanian <[email protected]> Co-authored-by: Nikita Shulga <[email protected]>
1 parent 5974b5c commit c2aa096

21 files changed

+552
-307
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,6 @@ cleanup.sh
124124

125125
# VSCode
126126
*.vscode
127+
128+
# pyspelling
129+
dictionary.dic

.pyspelling.yml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
spellchecker: aspell
22
matrix:
3-
- name: beginner
3+
- name: python
44
sources:
5-
- beginner_source/data_loading_tutorial.py
5+
- beginner_source/*.py
66
dictionary:
77
wordlists:
8-
- tutorials-wordlist.txt
8+
- en-wordlist.txt
99
pipeline:
1010
- pyspelling.filters.python:
1111
group_comments: true
1212
- pyspelling.filters.context:
1313
context_visible_first: true
1414
delimiters:
1515
# Exclude figure rST tags
16-
- open: '\.\.\s+(figure|literalinclude|)::'
16+
- open: '\.\.\s+(figure|literalinclude|math|image|grid)::'
17+
close: '\n'
18+
# Exclude raw directive
19+
- open: '\.\. (raw)::.*$\n*'
1720
close: '\n'
1821
# Exclude Python coding directives
1922
- open: '-\*- coding:'
2023
close: '\n'
24+
# Exclude Authors:
25+
- open: 'Author(|s):'
26+
close: '\n'
27+
# Exclude .rst directives:
28+
- open: ':math:`.*`'
29+
close: ' '
30+
# Ignore multiline content in codeblock
31+
- open: '(?s)^::\n\n '
32+
close: '^\n'
33+
# Ignore reStructuredText block directives
34+
- open: '\.\. (code-block)::.*$\n*'
35+
content: '(?P<first>(^(?P<indent>[ ]+).*$\n))(?P<other>(^([ \t]+.*|[ \t]*)$\n)*)'
36+
close: '(^(?![ \t]+.*$))'
2137
- pyspelling.filters.markdown:
2238
- pyspelling.filters.html:
2339
ignores:
2440
- code
2541
- pre
42+
- pyspelling.filters.url:

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 ([email protected]), Michael Suo ([email protected])*, rev2
5+
**Authors:** James Reed ([email protected]), Michael Suo ([email protected]), 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/chatbot_tutorial.py

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@
3131
# :align: center
3232
# :alt: bot
3333
#
34-
# .. code:: python
35-
#
36-
# > hello?
37-
# Bot: hello .
38-
# > where am I?
39-
# Bot: you re in a hospital .
40-
# > who are you?
41-
# Bot: i m a lawyer .
42-
# > how are you doing?
43-
# Bot: i m fine .
44-
# > are you my friend?
45-
# Bot: no .
46-
# > you're under arrest
47-
# Bot: i m trying to help you !
48-
# > i'm just kidding
49-
# Bot: i m sorry .
50-
# > where are you from?
51-
# Bot: san francisco .
52-
# > it's time for me to leave
53-
# Bot: i know .
54-
# > goodbye
55-
# Bot: goodbye .
34+
# .. code-block:: python
35+
#
36+
# > hello?
37+
# Bot: hello .
38+
# > where am I?
39+
# Bot: you re in a hospital .
40+
# > who are you?
41+
# Bot: i m a lawyer .
42+
# > how are you doing?
43+
# Bot: i m fine .
44+
# > are you my friend?
45+
# Bot: no .
46+
# > you're under arrest
47+
# Bot: i m trying to help you !
48+
# > i'm just kidding
49+
# Bot: i m sorry .
50+
# > where are you from?
51+
# Bot: san francisco .
52+
# > it's time for me to leave
53+
# Bot: i know .
54+
# > goodbye
55+
# Bot: goodbye .
5656
#
5757
# **Tutorial Highlights**
5858
#
@@ -65,7 +65,7 @@
6565
# - Implement greedy-search decoding module
6666
# - Interact with trained chatbot
6767
#
68-
# **Acknowledgements**
68+
# **Acknowledgments**
6969
#
7070
# This tutorial borrows code from the following sources:
7171
#
@@ -75,7 +75,7 @@
7575
# 2) Sean Robertson’s practical-pytorch seq2seq-translation example:
7676
# https://github.com/spro/practical-pytorch/tree/master/seq2seq-translation
7777
#
78-
# 3) FloydHub’s Cornell Movie Corpus preprocessing code:
78+
# 3) FloydHub Cornell Movie Corpus preprocessing code:
7979
# https://github.com/floydhub/textutil-preprocess-cornell-movie-corpus
8080
#
8181

@@ -162,11 +162,11 @@ def printLines(file, n=10):
162162
# contains a tab-separated *query sentence* and a *response sentence* pair.
163163
#
164164
# The following functions facilitate the parsing of the raw
165-
# *utterances.jsonl* data file.
165+
# ``utterances.jsonl`` data file.
166166
#
167167
# - ``loadLinesAndConversations`` splits each line of the file into a dictionary of
168-
# lines with fields: lineID, characterID, and text and then groups them
169-
# into conversations with fields: conversationID, movieID, and lines.
168+
# lines with fields: ``lineID``, ``characterID``, and text and then groups them
169+
# into conversations with fields: ``conversationID``, ``movieID``, and lines.
170170
# - ``extractSentencePairs`` extracts pairs of sentences from
171171
# conversations
172172
#
@@ -215,7 +215,7 @@ def extractSentencePairs(conversations):
215215

216216
######################################################################
217217
# Now we’ll call these functions and create the file. We’ll call it
218-
# *formatted_movie_lines.txt*.
218+
# ``formatted_movie_lines.txt``.
219219
#
220220

221221
# Define path to new file
@@ -359,12 +359,12 @@ def readVocs(datafile, corpus_name):
359359
voc = Voc(corpus_name)
360360
return voc, pairs
361361

362-
# Returns True iff both sentences in a pair 'p' are under the MAX_LENGTH threshold
362+
# Returns True if both sentences in a pair 'p' are under the MAX_LENGTH threshold
363363
def filterPair(p):
364364
# Input sequences need to preserve the last word for EOS token
365365
return len(p[0].split(' ')) < MAX_LENGTH and len(p[1].split(' ')) < MAX_LENGTH
366366

367-
# Filter pairs using filterPair condition
367+
# Filter pairs using the ``filterPair`` condition
368368
def filterPairs(pairs):
369369
return [pair for pair in pairs if filterPair(pair)]
370370

@@ -659,7 +659,7 @@ def __init__(self, hidden_size, embedding, n_layers=1, dropout=0):
659659
self.hidden_size = hidden_size
660660
self.embedding = embedding
661661

662-
# Initialize GRU; the input_size and hidden_size params are both set to 'hidden_size'
662+
# Initialize GRU; the input_size and hidden_size parameters are both set to 'hidden_size'
663663
# because our input size is a word embedding with number of features == hidden_size
664664
self.gru = nn.GRU(hidden_size, hidden_size, n_layers,
665665
dropout=(0 if n_layers == 1 else dropout), bidirectional=True)
@@ -958,7 +958,7 @@ def train(input_variable, lengths, target_variable, mask, max_target_len, encode
958958
input_variable = input_variable.to(device)
959959
target_variable = target_variable.to(device)
960960
mask = mask.to(device)
961-
# Lengths for rnn packing should always be on the cpu
961+
# Lengths for RNN packing should always be on the CPU
962962
lengths = lengths.to("cpu")
963963

964964
# Initialize variables
@@ -1007,7 +1007,7 @@ def train(input_variable, lengths, target_variable, mask, max_target_len, encode
10071007
print_losses.append(mask_loss.item() * nTotal)
10081008
n_totals += nTotal
10091009

1010-
# Perform backpropatation
1010+
# Perform backpropagation
10111011
loss.backward()
10121012

10131013
# Clip gradients: gradients are modified in place
@@ -1032,8 +1032,8 @@ def train(input_variable, lengths, target_variable, mask, max_target_len, encode
10321032
# lifting with the ``train`` function.
10331033
#
10341034
# One thing to note is that when we save our model, we save a tarball
1035-
# containing the encoder and decoder state_dicts (parameters), the
1036-
# optimizers’ state_dicts, the loss, the iteration, etc. Saving the model
1035+
# containing the encoder and decoder ``state_dicts`` (parameters), the
1036+
# optimizers’ ``state_dicts``, the loss, the iteration, etc. Saving the model
10371037
# in this way will give us the ultimate flexibility with the checkpoint.
10381038
# After loading a checkpoint, we will be able to use the model parameters
10391039
# to run inference, or we can continue training right where we left off.
@@ -1240,8 +1240,8 @@ def evaluateInput(encoder, decoder, searcher, voc):
12401240
# Configure models
12411241
model_name = 'cb_model'
12421242
attn_model = 'dot'
1243-
#attn_model = 'general'
1244-
#attn_model = 'concat'
1243+
#``attn_model = 'general'``
1244+
#``attn_model = 'concat'``
12451245
hidden_size = 500
12461246
encoder_n_layers = 2
12471247
decoder_n_layers = 2
@@ -1251,12 +1251,17 @@ def evaluateInput(encoder, decoder, searcher, voc):
12511251
# Set checkpoint to load from; set to None if starting from scratch
12521252
loadFilename = None
12531253
checkpoint_iter = 4000
1254-
#loadFilename = os.path.join(save_dir, model_name, corpus_name,
1255-
# '{}-{}_{}'.format(encoder_n_layers, decoder_n_layers, hidden_size),
1256-
# '{}_checkpoint.tar'.format(checkpoint_iter))
12571254

1255+
#############################################################
1256+
# Sample code to load from a checkpoint:
1257+
#
1258+
# .. code-block:: python
1259+
#
1260+
# loadFilename = os.path.join(save_dir, model_name, corpus_name,
1261+
# '{}-{}_{}'.format(encoder_n_layers, decoder_n_layers, hidden_size),
1262+
# '{}_checkpoint.tar'.format(checkpoint_iter))
12581263

1259-
# Load model if a loadFilename is provided
1264+
# Load model if a ``loadFilename`` is provided
12601265
if loadFilename:
12611266
# If loading on same machine the model was trained on
12621267
checkpoint = torch.load(loadFilename)
@@ -1319,7 +1324,7 @@ def evaluateInput(encoder, decoder, searcher, voc):
13191324
encoder_optimizer.load_state_dict(encoder_optimizer_sd)
13201325
decoder_optimizer.load_state_dict(decoder_optimizer_sd)
13211326

1322-
# If you have cuda, configure cuda to call
1327+
# If you have CUDA, configure CUDA to call
13231328
for state in encoder_optimizer.state.values():
13241329
for k, v in state.items():
13251330
if isinstance(v, torch.Tensor):
@@ -1344,7 +1349,7 @@ def evaluateInput(encoder, decoder, searcher, voc):
13441349
# To chat with your model, run the following block.
13451350
#
13461351

1347-
# Set dropout layers to eval mode
1352+
# Set dropout layers to ``eval`` mode
13481353
encoder.eval()
13491354
decoder.eval()
13501355

0 commit comments

Comments
 (0)