From 0aa88518786ca95f5750e3a79a87967bd3558a94 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 14 Oct 2020 15:00:49 +0200 Subject: [PATCH 1/3] always return losses --- spacy/pipeline/tagger.pyx | 5 +++-- spacy/pipeline/trainable_pipe.pyx | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spacy/pipeline/tagger.pyx b/spacy/pipeline/tagger.pyx index 1b0f79cea..3be93c32c 100644 --- a/spacy/pipeline/tagger.pyx +++ b/spacy/pipeline/tagger.pyx @@ -195,7 +195,7 @@ class Tagger(TrainablePipe): validate_examples(examples, "Tagger.update") if not any(len(eg.predicted) if eg.predicted else 0 for eg in examples): # Handle cases where there are no tokens in any docs. - return + return losses set_dropout_rate(self.model, drop) tag_scores, bp_tag_scores = self.model.begin_update([eg.predicted for eg in examples]) for sc in tag_scores: @@ -233,7 +233,7 @@ class Tagger(TrainablePipe): return if not any(len(doc) for doc in docs): # Handle cases where there are no tokens in any docs. - return + return losses set_dropout_rate(self.model, drop) guesses, backprop = self.model.begin_update(docs) target = self._rehearsal_model(examples) @@ -243,6 +243,7 @@ class Tagger(TrainablePipe): if losses is not None: losses.setdefault(self.name, 0.0) losses[self.name] += (gradient**2).sum() + return losses def get_loss(self, examples, scores): """Find the loss and gradient of loss for the batch of documents and diff --git a/spacy/pipeline/trainable_pipe.pyx b/spacy/pipeline/trainable_pipe.pyx index 07cb01059..6cd73d256 100644 --- a/spacy/pipeline/trainable_pipe.pyx +++ b/spacy/pipeline/trainable_pipe.pyx @@ -116,7 +116,7 @@ cdef class TrainablePipe(Pipe): validate_examples(examples, "TrainablePipe.update") if not any(len(eg.predicted) if eg.predicted else 0 for eg in examples): # Handle cases where there are no tokens in any docs. - return + return losses set_dropout_rate(self.model, drop) scores, bp_scores = self.model.begin_update([eg.predicted for eg in examples]) loss, d_scores = self.get_loss(examples, scores) From 478a14a61934e617988f70aaf692fcd6d7b1e226 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 14 Oct 2020 15:01:19 +0200 Subject: [PATCH 2/3] fix few typos --- website/docs/usage/layers-architectures.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/website/docs/usage/layers-architectures.md b/website/docs/usage/layers-architectures.md index e348c4389..9677398cf 100644 --- a/website/docs/usage/layers-architectures.md +++ b/website/docs/usage/layers-architectures.md @@ -503,7 +503,7 @@ overview of the `TrainablePipe` methods used by -### Example: Entity elation extraction component {#component-rel} +### Example: Entity relation extraction component {#component-rel} This section outlines an example use-case of implementing a **novel relation extraction component** from scratch. We'll implement a binary relation @@ -618,7 +618,7 @@ we can define our relation model in a config file as such: # ... [model.get_candidates] -@misc = "rel_cand_generator.v2" +@misc = "rel_cand_generator.v1" max_length = 20 [model.create_candidate_tensor] @@ -687,8 +687,8 @@ Before the model can be used, it needs to be [initialized](/usage/training#initialization). This function receives a callback to access the full **training data set**, or a representative sample. This data set can be used to deduce all **relevant labels**. Alternatively, a list of -labels can be provided to `initialize`, or you can call the -`RelationExtractoradd_label` directly. The number of labels defines the output +labels can be provided to `initialize`, or you can call +`RelationExtractor.add_label` directly. The number of labels defines the output dimensionality of the network, and will be used to do [shape inference](https://thinc.ai/docs/usage-models#validation) throughout the layers of the neural network. This is triggered by calling @@ -729,7 +729,7 @@ and its internal model can be trained and used to make predictions. During training, the function [`update`](/api/pipe#update) is invoked which delegates to [`Model.begin_update`](https://thinc.ai/docs/api-model#begin_update) and a -[`get_loss`](/api/pipe#get_loss) function that **calculate the loss** for a +[`get_loss`](/api/pipe#get_loss) function that **calculates the loss** for a batch of examples, as well as the **gradient** of loss that will be used to update the weights of the model layers. Thinc provides several [loss functions](https://thinc.ai/docs/api-loss) that can be used for the From 44e14ccae87d4077cfc3b730e76ab32bbb15cafb Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 14 Oct 2020 15:11:34 +0200 Subject: [PATCH 3/3] one more losses fix --- spacy/pipeline/tagger.pyx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spacy/pipeline/tagger.pyx b/spacy/pipeline/tagger.pyx index 3be93c32c..16633a7b8 100644 --- a/spacy/pipeline/tagger.pyx +++ b/spacy/pipeline/tagger.pyx @@ -227,10 +227,13 @@ class Tagger(TrainablePipe): DOCS: https://nightly.spacy.io/api/tagger#rehearse """ + if losses is None: + losses = {} + losses.setdefault(self.name, 0.0) validate_examples(examples, "Tagger.rehearse") docs = [eg.predicted for eg in examples] if self._rehearsal_model is None: - return + return losses if not any(len(doc) for doc in docs): # Handle cases where there are no tokens in any docs. return losses @@ -240,9 +243,7 @@ class Tagger(TrainablePipe): gradient = guesses - target backprop(gradient) self.finish_update(sgd) - if losses is not None: - losses.setdefault(self.name, 0.0) - losses[self.name] += (gradient**2).sum() + losses[self.name] += (gradient**2).sum() return losses def get_loss(self, examples, scores):