add finish_update to Pipe

This commit is contained in:
svlandeg 2020-10-05 16:23:33 +02:00
parent 251b3eb4e5
commit 65abd77779
9 changed files with 40 additions and 11 deletions

View File

@ -1094,7 +1094,7 @@ class Language:
and hasattr(proc, "model")
and proc.model not in (True, False, None)
):
proc.model.finish_update(sgd)
proc.finish_update(sgd)
return losses
def rehearse(

View File

@ -238,7 +238,7 @@ class EntityLinker(Pipe):
)
bp_context(d_scores)
if sgd is not None:
self.model.finish_update(sgd)
self.finish_update(sgd)
losses[self.name] += loss
if set_annotations:
self.set_annotations(docs, predictions)

View File

@ -209,7 +209,7 @@ class ClozeMultitask(Pipe):
loss, d_predictions = self.get_loss(examples, self.vocab.vectors.data, predictions)
bp_predictions(d_predictions)
if sgd is not None:
self.model.finish_update(sgd)
self.finish_update(sgd)
if losses is not None:
losses[self.name] += loss
return losses

View File

@ -132,7 +132,7 @@ cdef class Pipe:
loss, d_scores = self.get_loss(examples, scores)
bp_scores(d_scores)
if sgd not in (None, False):
self.model.finish_update(sgd)
self.finish_update(sgd)
losses[self.name] += loss
if set_annotations:
docs = [eg.predicted for eg in examples]
@ -245,6 +245,17 @@ cdef class Pipe:
with self.model.use_params(params):
yield
def finish_update(self, sgd):
"""Update parameters using the current parameter gradients.
The Optimizer instance contains the functionality to perform
the stochastic gradient descent.
sgd (thinc.api.Optimizer): The optimizer.
DOCS: https://nightly.spacy.io/api/pipe#finish_update
"""
self.model.finish_update(sgd)
def score(self, examples, **kwargs):
"""Score a batch of examples.

View File

@ -203,7 +203,7 @@ class Tagger(Pipe):
loss, d_tag_scores = self.get_loss(examples, tag_scores)
bp_tag_scores(d_tag_scores)
if sgd not in (None, False):
self.model.finish_update(sgd)
self.finish_update(sgd)
losses[self.name] += loss
if set_annotations:
@ -238,7 +238,7 @@ class Tagger(Pipe):
target = self._rehearsal_model(examples)
gradient = guesses - target
backprop(gradient)
self.model.finish_update(sgd)
self.finish_update(sgd)
if losses is not None:
losses.setdefault(self.name, 0.0)
losses[self.name] += (gradient**2).sum()

View File

@ -212,7 +212,7 @@ class TextCategorizer(Pipe):
loss, d_scores = self.get_loss(examples, scores)
bp_scores(d_scores)
if sgd is not None:
self.model.finish_update(sgd)
self.finish_update(sgd)
losses[self.name] += loss
if set_annotations:
docs = [eg.predicted for eg in examples]
@ -256,7 +256,7 @@ class TextCategorizer(Pipe):
gradient = scores - target
bp_scores(gradient)
if sgd is not None:
self.model.finish_update(sgd)
self.finish_update(sgd)
if losses is not None:
losses[self.name] += (gradient ** 2).sum()
return losses

View File

@ -188,7 +188,7 @@ class Tok2Vec(Pipe):
accumulate_gradient(one_d_tokvecs)
d_docs = bp_tokvecs(d_tokvecs)
if sgd is not None:
self.model.finish_update(sgd)
self.finish_update(sgd)
return d_docs
batch_id = Tok2VecListener.get_batch_id(docs)

View File

@ -315,7 +315,7 @@ cdef class Parser(Pipe):
backprop_tok2vec(golds)
if sgd not in (None, False):
self.model.finish_update(sgd)
self.finish_update(sgd)
if set_annotations:
docs = [eg.predicted for eg in examples]
self.set_annotations(docs, all_states)
@ -367,7 +367,7 @@ cdef class Parser(Pipe):
# Do the backprop
backprop_tok2vec(docs)
if sgd is not None:
self.model.finish_update(sgd)
self.finish_update(sgd)
losses[self.name] += loss / n_scores
del backprop
del backprop_tok2vec

View File

@ -294,6 +294,24 @@ context, the original parameters are restored.
| -------- | -------------------------------------------------- |
| `params` | The parameter values to use in the model. ~~dict~~ |
## Pipe.finish_update {#finish_update tag="method"}
Update parameters using the current parameter gradients. Defaults to calling
[`self.model.finish_update`](https://thinc.ai/docs/api-model#finish_update).
> #### Example
>
> ```python
> pipe = nlp.add_pipe("your_custom_pipe")
> optimizer = nlp.initialize()
> losses = pipe.update(examples, sgd=None)
> pipe.finish_update(sgd)
> ```
| Name | Description |
| ----- | ------------------------------------- |
| `sgd` | An optimizer. ~~Optional[Optimizer]~~ |
## Pipe.add_label {#add_label tag="method"}
> #### Example