From a8e4064dd81dcebea53a2bdbee4a2d6904b23ff5 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 15 Aug 2017 03:14:36 -0500 Subject: [PATCH] Fix tensor gradient in parser --- spacy/syntax/nn_parser.pyx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spacy/syntax/nn_parser.pyx b/spacy/syntax/nn_parser.pyx index fb7099022..5d6f51538 100644 --- a/spacy/syntax/nn_parser.pyx +++ b/spacy/syntax/nn_parser.pyx @@ -633,10 +633,9 @@ cdef class Parser: xp = get_array_module(d_tokvecs) for ids, d_vector, bp_vector in backprops: d_state_features = bp_vector(d_vector, sgd=sgd) - mask = ids >= 0 - indices = xp.nonzero(mask) - self.model[0].ops.scatter_add(d_tokvecs, ids[indices], - d_state_features[indices]) + mask = (ids >= 0).reshape((ids.shape[0], ids.shape[1], 1)) + self.model[0].ops.scatter_add(d_tokvecs, ids, + d_state_features * mask) @property def move_names(self):