Skip to content

Commit 9e6f2c4

Browse files
authored
Added comments to LSTM Sequence Models tutorial (pytorch#1266)
Co-authored-by: Brian Johnson <[email protected]> Thanks for this work @jecarr!
1 parent 5b42d6c commit 9e6f2c4

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

beginner_source/nlp/sequence_models_tutorial.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
all of its inputs to be 3D tensors. The semantics of the axes of these
2929
tensors is important. The first axis is the sequence itself, the second
3030
indexes instances in the mini-batch, and the third indexes elements of
31-
the input. We haven't discussed mini-batching, so lets just ignore that
31+
the input. We haven't discussed mini-batching, so let's just ignore that
3232
and assume we will always have just 1 dimension on the second axis. If
3333
we want to run the sequence model over the sentence "The cow jumped",
3434
our input should look like
@@ -95,7 +95,9 @@
9595
# In this section, we will use an LSTM to get part of speech tags. We will
9696
# not use Viterbi or Forward-Backward or anything like that, but as a
9797
# (challenging) exercise to the reader, think about how Viterbi could be
98-
# used after you have seen what is going on.
98+
# used after you have seen what is going on. In this example, we also refer
99+
# to embeddings. If you are unfamiliar with embeddings, you can read up
100+
# about them `here <https://pytorch.org/tutorials/beginner/nlp/word_embeddings_tutorial.html>`__.
99101
#
100102
# The model is as follows: let our input sentence be
101103
# :math:`w_1, \dots, w_M`, where :math:`w_i \in V`, our vocab. Also, let
@@ -127,16 +129,19 @@ def prepare_sequence(seq, to_ix):
127129

128130

129131
training_data = [
132+
# Tags are: DET - determiner; NN - noun; V - verb
133+
# For example, the word "The" is a determiner
130134
("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]),
131135
("Everybody read that book".split(), ["NN", "V", "DET", "NN"])
132136
]
133137
word_to_ix = {}
138+
# For each words-list (sentence) and tags-list in each tuple of training_data
134139
for sent, tags in training_data:
135140
for word in sent:
136-
if word not in word_to_ix:
137-
word_to_ix[word] = len(word_to_ix)
141+
if word not in word_to_ix: # word has not been assigned an index yet
142+
word_to_ix[word] = len(word_to_ix) # Assign each word with a unique index
138143
print(word_to_ix)
139-
tag_to_ix = {"DET": 0, "NN": 1, "V": 2}
144+
tag_to_ix = {"DET": 0, "NN": 1, "V": 2} # Assign each tag with a unique index
140145

141146
# These will usually be more like 32 or 64 dimensional.
142147
# We will keep them small, so we can see how the weights change as we train.

0 commit comments

Comments
 (0)