mirror of
https://github.com/explosion/spaCy.git
synced 2024-11-10 19:57:17 +03:00
add finish_update to Pipe
This commit is contained in:
parent
251b3eb4e5
commit
65abd77779
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user