spaCy/spacy/training
Daniël de Kok eec5ccd72f
Language.update: ensure that tok2vec gets updated (#12136)
* `Language.update`: ensure that tok2vec gets updated

The components in a pipeline can be updated independently. However,
tok2vec implementations are an exception to this, since they depend on
listeners for their gradients. The update method of a tok2vec
implementation computes the tok2vec forward and passes this along with a
backprop function to the listeners. This backprop function accumulates
gradients for all the listeners. There are two ways in which the
accumulated gradients can be used to update the tok2vec weights:

1. Call the `finish_update` method of tok2vec *after* the `update`
   method is called on all of the pipes that use a tok2vec listener.
2. Pass an optimizer to the `update` method of tok2vec. In this
   case, tok2vec will give the last listener a special backprop
   function that calls `finish_update` on the tok2vec.

Unfortunately, `Language.update` did neither of these. Instead, it
immediately called `finish_update` on every pipe after `update`. As a
result, the tok2vec weights are updated when no gradients have been
accumulated from listeners yet. And the gradients of the listeners are
only used in the next call to `Language.update` (when `finish_update` is
called on tok2vec again).

This change fixes this issue by passing the optimizer to the `update`
method of trainable pipes, leading to use of the second strategy
outlined above.

The main updating loop in `Language.update` is also simplified by using
the `TrainableComponent` protocol consistently.

* Train loop: `sgd` is `Optional[Optimizer]`, do not pass false

* Language.update: call pipe finish_update after all pipe updates

This does correct and fast updates if multiple components update the
same parameters.

* Add comment why we moved `finish_update` to a separate loop
2023-02-03 15:22:25 +01:00
..
converters Rename language codes (Icelandic, multi-language) (#12149) 2023-01-31 17:30:43 +01:00
__init__.pxd Renaming gold & annotation_setter (#6042) 2020-09-09 10:31:03 +02:00
__init__.py Merge remote-tracking branch 'upstream/master' into update-v4-from-master-1 2023-01-27 08:29:09 +01:00
align.pyx Fix alignment for 1-to-1 tokens and lowercasing (#6476) 2020-12-08 14:25:16 +08:00
alignment_array.pxd Alignment: use a simplified ragged type for performance (#10319) 2022-04-01 09:02:06 +02:00
alignment_array.pyx Backport parser/alignment optimizations from feature/refactor-parser (#10952) 2022-06-24 13:39:52 +02:00
alignment.py Alignment: use a simplified ragged type for performance (#10319) 2022-04-01 09:02:06 +02:00
augment.py Preserve missing entity annotation in augmenters (#11540) 2022-09-27 10:16:51 +02:00
batchers.py Fix batching regression (#12094) 2023-01-18 18:28:30 +01:00
callbacks.py 🏷 Add Mypy check to CI and ignore all existing Mypy errors (#9167) 2021-10-14 15:21:40 +02:00
corpus.py Add spacy.PlainTextCorpusReader.v1 (#12122) 2023-01-26 11:33:22 +01:00
example.pxd Make a pre-check to speed up alignment cache (#6139) 2020-09-24 18:13:39 +02:00
example.pyx Merge the parser refactor into v4 (#10940) 2023-01-18 11:27:45 +01:00
gold_io.pyx Fix is_sent_start when converting from JSON (fix #7635) (#7655) 2021-04-08 18:24:52 +10:00
initialize.py Clean up warnings in the test suite (#11331) 2022-08-22 12:04:30 +02:00
iob_utils.py Preserve missing entity annotation in augmenters (#11540) 2022-09-27 10:16:51 +02:00
loggers.py New console logger with expanded progress tracking (#11972) 2022-12-23 15:21:44 +01:00
loop.py Language.update: ensure that tok2vec gets updated (#12136) 2023-02-03 15:22:25 +01:00
pretrain.py Clarify how to fill in init_tok2vec after pretraining (#9639) 2021-11-18 15:38:30 +01:00