Skip to content

Commit e28cf87

Browse files
committed
cosmetic fixes in nlp/advnaced
1 parent 97da3c3 commit e28cf87

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

beginner_source/nlp/advanced_tutorial.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
* We build the tree bottom up
2727
* Tag the root nodes (the words of the sentence)
2828
* From there, use a neural network and the embeddings
29-
30-
of the words to find combinations that form constituents. Whenever you
31-
form a new constituent, use some sort of technique to get an embedding
32-
of the constituent. In this case, our network architecture will depend
33-
completely on the input sentence. In the sentence "The green cat
34-
scratched the wall", at some point in the model, we will want to combine
35-
the span :math:`(i,j,r) = (1, 3, \text{NP})` (that is, an NP constituent
36-
spans word 1 to word 3, in this case "The green cat").
29+
of the words to find combinations that form constituents. Whenever you
30+
form a new constituent, use some sort of technique to get an embedding
31+
of the constituent. In this case, our network architecture will depend
32+
completely on the input sentence. In the sentence "The green cat
33+
scratched the wall", at some point in the model, we will want to combine
34+
the span :math:`(i,j,r) = (1, 3, \text{NP})` (that is, an NP constituent
35+
spans word 1 to word 3, in this case "The green cat").
3736
3837
However, another sentence might be "Somewhere, the big fat cat scratched
3938
the wall". In this sentence, we will want to form the constituent
@@ -68,7 +67,7 @@
6867
# - Write the recurrence for the viterbi variable at step i for tag k.
6968
# - Modify the above recurrence to compute the forward variables instead.
7069
# - Modify again the above recurrence to compute the forward variables in
71-
# log-space (hint: log-sum-exp)
70+
# log-space (hint: log-sum-exp)
7271
#
7372
# If you can do those three things, you should be able to understand the
7473
# code below. Recall that the CRF computes a conditional probability. Let
@@ -128,8 +127,8 @@
128127
torch.manual_seed(1)
129128

130129
#####################################################################
131-
132130
# Helper functions to make the code more readable.
131+
133132
def to_scalar(var):
134133
# returns a python float
135134
return var.view(-1).data.tolist()[0]
@@ -148,13 +147,13 @@ def prepare_sequence(seq, to_ix):
148147

149148

150149
# Compute log sum exp in a numerically stable way for the forward algorithm
151-
152-
153150
def log_sum_exp(vec):
154151
max_score = vec[0, argmax(vec)]
155152
max_score_broadcast = max_score.view(1, -1).expand(1, vec.size()[1])
156153
return max_score + torch.log(torch.sum(torch.exp(vec - max_score_broadcast)))
157154

155+
#####################################################################
156+
# Create model
158157

159158
class BiLSTM_CRF(nn.Module):
160159

@@ -293,6 +292,8 @@ def forward(self, sentence): # dont confuse this with _forward_alg above.
293292
score, tag_seq = self._viterbi_decode(lstm_feats)
294293
return score, tag_seq
295294

295+
#####################################################################
296+
# Run training
296297

297298
START_TAG = "<START>"
298299
STOP_TAG = "<STOP>"

0 commit comments

Comments
 (0)