From 964707d7956c5d9c0eca72291277fd789b08e15d Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 23 May 2017 05:31:13 -0500 Subject: [PATCH] Restore support for deeper networks in parser --- spacy/syntax/nn_parser.pyx | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/spacy/syntax/nn_parser.pyx b/spacy/syntax/nn_parser.pyx index ff8642401..e24143839 100644 --- a/spacy/syntax/nn_parser.pyx +++ b/spacy/syntax/nn_parser.pyx @@ -336,9 +336,25 @@ cdef class Parser: feat_weights = state2vec.get_feat_weights() cdef int i + cdef np.ndarray token_ids = numpy.zeros((nr_state, nr_feat), dtype='i') + cdef np.ndarray is_valid = numpy.zeros((nr_state, nr_feat), dtype='i') + cdef np.ndarray scores + c_token_ids = token_ids.data + c_is_valid = is_valid.data while not next_step.empty(): - for i in cython.parallel.prange(next_step.size(), num_threads=4, nogil=True): - self._parse_step(next_step[i], feat_weights, nr_class, nr_feat) + for i in range(next_step.size()): + st = next_step[i] + st.set_context_tokens(&c_token_ids[i*nr_feat], nr_feat) + self.moves.set_valid(&c_is_valid[i*nr_class], st) + vectors = state2vec.begin_update(token_ids[:next_step.size()]) + scores = vec2scores(vectors) + c_scores = scores.data + for i in range(next_step.size()): + st = next_step[i] + guess = arg_max_if_valid( + &c_scores[i*nr_class], &c_is_valid[i*nr_class], nr_class) + action = self.moves.c[guess] + action.do(st, action.label) this_step, next_step = next_step, this_step next_step.clear() for st in this_step: @@ -349,6 +365,9 @@ cdef class Parser: cdef void _parse_step(self, StateC* state, const float* feat_weights, int nr_class, int nr_feat) nogil: + '''This only works with no hidden layers -- fast but inaccurate''' + #for i in cython.parallel.prange(next_step.size(), num_threads=4, nogil=True): + # self._parse_step(next_step[i], feat_weights, nr_class, nr_feat) token_ids = calloc(nr_feat, sizeof(int)) scores = calloc(nr_class, sizeof(float)) is_valid = calloc(nr_class, sizeof(int))