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#
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#
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
363363def 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
368368def 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
12411241model_name = 'cb_model'
12421242attn_model = 'dot'
1243- #attn_model = 'general'
1244- #attn_model = 'concat'
1243+ #`` attn_model = 'general'``
1244+ #`` attn_model = 'concat'``
12451245hidden_size = 500
12461246encoder_n_layers = 2
12471247decoder_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
12521252loadFilename = None
12531253checkpoint_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
12601265if 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
13231328for 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
13481353encoder .eval ()
13491354decoder .eval ()
13501355
0 commit comments