You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/06/26 18:46:21 UTC

[GitHub] szha closed pull request #11375: Fix bi-lstm-crf to update crf weights

szha closed pull request #11375: Fix bi-lstm-crf to update crf weights
URL: https://github.com/apache/incubator-mxnet/pull/11375
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/example/gluon/lstm_crf.py b/example/gluon/lstm_crf.py
index 561b4c24bb6..3e95c054fed 100644
--- a/example/gluon/lstm_crf.py
+++ b/example/gluon/lstm_crf.py
@@ -62,8 +62,9 @@ def __init__(self, vocab_size, tag2idx, embedding_dim, hidden_dim):
 
             # Matrix of transition parameters.  Entry i,j is the score of
             # transitioning *to* i *from* j.
-            self.transitions = nd.random.normal(shape=(self.tagset_size, self.tagset_size))
-
+            self.transitions = self.params.get("crf_transition_matrix", 
+                                               shape=(self.tagset_size, self.tagset_size))
+            
             self.hidden = self.init_hidden()
 
     def init_hidden(self):
@@ -85,7 +86,7 @@ def _forward_alg(self, feats):
                 emit_score = feat[next_tag].reshape((1, -1))
                 # the ith entry of trans_score is the score of transitioning to
                 # next_tag from i
-                trans_score = self.transitions[next_tag].reshape((1, -1))
+                trans_score = self.transitions.data()[next_tag].reshape((1, -1))
                 # The ith entry of next_tag_var is the value for the
                 # edge (i -> next_tag) before we do log-sum-exp
                 next_tag_var = alphas + trans_score + emit_score
@@ -93,7 +94,7 @@ def _forward_alg(self, feats):
                 # scores.
                 alphas_t.append(log_sum_exp(next_tag_var))
             alphas = nd.concat(*alphas_t, dim=0).reshape((1, -1))
-        terminal_var = alphas + self.transitions[self.tag2idx[STOP_TAG]]
+        terminal_var = alphas + self.transitions.data()[self.tag2idx[STOP_TAG]]
         alpha = log_sum_exp(terminal_var)
         return alpha
 
@@ -112,8 +113,8 @@ def _score_sentence(self, feats, tags):
         tags = nd.concat(nd.array([self.tag2idx[START_TAG]]), *tags, dim=0)
         for i, feat in enumerate(feats):
             score = score + \
-                self.transitions[to_scalar(tags[i+1]), to_scalar(tags[i])] + feat[to_scalar(tags[i+1])]
-        score = score + self.transitions[self.tag2idx[STOP_TAG],
+                self.transitions.data()[to_scalar(tags[i+1]), to_scalar(tags[i])] + feat[to_scalar(tags[i+1])]
+        score = score + self.transitions.data()[self.tag2idx[STOP_TAG],
                                          to_scalar(tags[int(tags.shape[0]-1)])]
         return score
 
@@ -134,7 +135,7 @@ def _viterbi_decode(self, feats):
                 # from tag i to next_tag.
                 # We don't include the emission scores here because the max
                 # does not depend on them (we add them in below)
-                next_tag_var = vvars + self.transitions[next_tag]
+                next_tag_var = vvars + self.transitions.data()[next_tag]
                 best_tag_id = argmax(next_tag_var)
                 bptrs_t.append(best_tag_id)
                 viterbivars_t.append(next_tag_var[0, best_tag_id])
@@ -144,7 +145,7 @@ def _viterbi_decode(self, feats):
             backpointers.append(bptrs_t)
 
         # Transition to STOP_TAG
-        terminal_var = vvars + self.transitions[self.tag2idx[STOP_TAG]]
+        terminal_var = vvars + self.transitions.data()[self.tag2idx[STOP_TAG]]
         best_tag_id = argmax(terminal_var)
         path_score = terminal_var[0, best_tag_id]
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services