31
31
# :align: center
32
32
# :alt: bot
33
33
#
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 .
56
56
#
57
57
# **Tutorial Highlights**
58
58
#
65
65
# - Implement greedy-search decoding module
66
66
# - Interact with trained chatbot
67
67
#
68
- # **Acknowledgements **
68
+ # **Acknowledgments **
69
69
#
70
70
# This tutorial borrows code from the following sources:
71
71
#
75
75
# 2) Sean Robertson’s practical-pytorch seq2seq-translation example:
76
76
# https://github.com/spro/practical-pytorch/tree/master/seq2seq-translation
77
77
#
78
- # 3) FloydHub’s Cornell Movie Corpus preprocessing code:
78
+ # 3) FloydHub Cornell Movie Corpus preprocessing code:
79
79
# https://github.com/floydhub/textutil-preprocess-cornell-movie-corpus
80
80
#
81
81
@@ -162,11 +162,11 @@ def printLines(file, n=10):
162
162
# contains a tab-separated *query sentence* and a *response sentence* pair.
163
163
#
164
164
# The following functions facilitate the parsing of the raw
165
- # * utterances.jsonl* data file.
165
+ # `` utterances.jsonl`` data file.
166
166
#
167
167
# - ``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.
170
170
# - ``extractSentencePairs`` extracts pairs of sentences from
171
171
# conversations
172
172
#
@@ -215,7 +215,7 @@ def extractSentencePairs(conversations):
215
215
216
216
######################################################################
217
217
# Now we’ll call these functions and create the file. We’ll call it
218
- # * formatted_movie_lines.txt* .
218
+ # `` formatted_movie_lines.txt`` .
219
219
#
220
220
221
221
# Define path to new file
@@ -359,12 +359,12 @@ def readVocs(datafile, corpus_name):
359
359
voc = Voc (corpus_name )
360
360
return voc , pairs
361
361
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
363
363
def filterPair (p ):
364
364
# Input sequences need to preserve the last word for EOS token
365
365
return len (p [0 ].split (' ' )) < MAX_LENGTH and len (p [1 ].split (' ' )) < MAX_LENGTH
366
366
367
- # Filter pairs using filterPair condition
367
+ # Filter pairs using the `` filterPair`` condition
368
368
def filterPairs (pairs ):
369
369
return [pair for pair in pairs if filterPair (pair )]
370
370
@@ -659,7 +659,7 @@ def __init__(self, hidden_size, embedding, n_layers=1, dropout=0):
659
659
self .hidden_size = hidden_size
660
660
self .embedding = embedding
661
661
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'
663
663
# because our input size is a word embedding with number of features == hidden_size
664
664
self .gru = nn .GRU (hidden_size , hidden_size , n_layers ,
665
665
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
958
958
input_variable = input_variable .to (device )
959
959
target_variable = target_variable .to (device )
960
960
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
962
962
lengths = lengths .to ("cpu" )
963
963
964
964
# Initialize variables
@@ -1007,7 +1007,7 @@ def train(input_variable, lengths, target_variable, mask, max_target_len, encode
1007
1007
print_losses .append (mask_loss .item () * nTotal )
1008
1008
n_totals += nTotal
1009
1009
1010
- # Perform backpropatation
1010
+ # Perform backpropagation
1011
1011
loss .backward ()
1012
1012
1013
1013
# Clip gradients: gradients are modified in place
@@ -1032,8 +1032,8 @@ def train(input_variable, lengths, target_variable, mask, max_target_len, encode
1032
1032
# lifting with the ``train`` function.
1033
1033
#
1034
1034
# 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
1037
1037
# in this way will give us the ultimate flexibility with the checkpoint.
1038
1038
# After loading a checkpoint, we will be able to use the model parameters
1039
1039
# to run inference, or we can continue training right where we left off.
@@ -1240,8 +1240,8 @@ def evaluateInput(encoder, decoder, searcher, voc):
1240
1240
# Configure models
1241
1241
model_name = 'cb_model'
1242
1242
attn_model = 'dot'
1243
- #attn_model = 'general'
1244
- #attn_model = 'concat'
1243
+ #`` attn_model = 'general'``
1244
+ #`` attn_model = 'concat'``
1245
1245
hidden_size = 500
1246
1246
encoder_n_layers = 2
1247
1247
decoder_n_layers = 2
@@ -1251,12 +1251,17 @@ def evaluateInput(encoder, decoder, searcher, voc):
1251
1251
# Set checkpoint to load from; set to None if starting from scratch
1252
1252
loadFilename = None
1253
1253
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))
1257
1254
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))
1258
1263
1259
- # Load model if a loadFilename is provided
1264
+ # Load model if a `` loadFilename`` is provided
1260
1265
if loadFilename :
1261
1266
# If loading on same machine the model was trained on
1262
1267
checkpoint = torch .load (loadFilename )
@@ -1319,7 +1324,7 @@ def evaluateInput(encoder, decoder, searcher, voc):
1319
1324
encoder_optimizer .load_state_dict (encoder_optimizer_sd )
1320
1325
decoder_optimizer .load_state_dict (decoder_optimizer_sd )
1321
1326
1322
- # If you have cuda , configure cuda to call
1327
+ # If you have CUDA , configure CUDA to call
1323
1328
for state in encoder_optimizer .state .values ():
1324
1329
for k , v in state .items ():
1325
1330
if isinstance (v , torch .Tensor ):
@@ -1344,7 +1349,7 @@ def evaluateInput(encoder, decoder, searcher, voc):
1344
1349
# To chat with your model, run the following block.
1345
1350
#
1346
1351
1347
- # Set dropout layers to eval mode
1352
+ # Set dropout layers to `` eval`` mode
1348
1353
encoder .eval ()
1349
1354
decoder .eval ()
1350
1355
0 commit comments