From 9e711c34761ef9d160651a453ce574b72dcc535b Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sat, 27 May 2017 18:32:46 -0500 Subject: [PATCH] Divide d_loss by batch size --- spacy/pipeline.pyx | 2 ++ spacy/syntax/nn_parser.pyx | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spacy/pipeline.pyx b/spacy/pipeline.pyx index 98b79d709..9abb70b40 100644 --- a/spacy/pipeline.pyx +++ b/spacy/pipeline.pyx @@ -228,6 +228,7 @@ class NeuralTagger(object): idx += 1 correct = self.model.ops.xp.array(correct, dtype='i') d_scores = scores - to_categorical(correct, nb_classes=scores.shape[1]) + d_scores /= d_scores.shape[0] loss = (d_scores**2).sum() d_scores = self.model.ops.unflatten(d_scores, [len(d) for d in docs]) return float(loss), d_scores @@ -292,6 +293,7 @@ class NeuralLabeller(NeuralTagger): idx += 1 correct = self.model.ops.xp.array(correct, dtype='i') d_scores = scores - to_categorical(correct, nb_classes=scores.shape[1]) + d_scores /= d_scores.shape[0] loss = (d_scores**2).sum() d_scores = self.model.ops.unflatten(d_scores, [len(d) for d in docs]) return float(loss), d_scores diff --git a/spacy/syntax/nn_parser.pyx b/spacy/syntax/nn_parser.pyx index ffd7c8da6..320f3c620 100644 --- a/spacy/syntax/nn_parser.pyx +++ b/spacy/syntax/nn_parser.pyx @@ -450,7 +450,7 @@ cdef class Parser: scores, bp_scores = vec2scores.begin_update(vector, drop=drop) d_scores = self.get_batch_loss(states, golds, scores) - d_vector = bp_scores(d_scores, sgd=sgd) + d_vector = bp_scores(d_scores / d_scores.shape[0], sgd=sgd) if drop != 0: d_vector *= mask