spaCy/spacy/tokens
Daniël de Kok efdbb722c5
Store activations in Docs when save_activations is enabled (#11002)
* Store activations in Doc when `store_activations` is enabled

This change adds the new `activations` attribute to `Doc`. This
attribute can be used by trainable pipes to store their activations,
probabilities, and guesses for downstream users.

As an example, this change modifies the `tagger` and `senter` pipes to
add an `store_activations` option. When this option is enabled, the
probabilities and guesses are stored in `set_annotations`.

* Change type of `store_activations` to `Union[bool, List[str]]`

When the value is:

- A bool: all activations are stored when set to `True`.
- A List[str]: the activations named in the list are stored

* Formatting fixes in Tagger

* Support store_activations in spancat and morphologizer

* Make Doc.activations type visible to MyPy

* textcat/textcat_multilabel: add store_activations option

* trainable_lemmatizer/entity_linker: add store_activations option

* parser/ner: do not currently support returning activations

* Extend tagger and senter tests

So that they, like the other tests, also check that we get no
activations if no activations were requested.

* Document `Doc.activations` and `store_activations` in the relevant pipes

* Start errors/warnings at higher numbers to avoid merge conflicts

Between the master and v4 branches.

* Add `store_activations` to docstrings.

* Replace store_activations setter by set_store_activations method

Setters that take a different type than what the getter returns are still
problematic for MyPy. Replace the setter by a method, so that type inference
works everywhere.

* Use dict comprehension suggested by @svlandeg

* Revert "Use dict comprehension suggested by @svlandeg"

This reverts commit 6e7b958f70.

* EntityLinker: add type annotations to _add_activations

* _store_activations: make kwarg-only, remove doc_scores_lens arg

* set_annotations: add type annotations

* Apply suggestions from code review

Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>

* TextCat.predict: return dict

* Make the `TrainablePipe.store_activations` property a bool

This means that we can also bring back `store_activations` setter.

* Remove `TrainablePipe.activations`

We do not need to enumerate the activations anymore since `store_activations` is
`bool`.

* Add type annotations for activations in predict/set_annotations

* Rename `TrainablePipe.store_activations` to `save_activations`

* Error E1400 is not used anymore

This error was used when activations were still `Union[bool, List[str]]`.

* Change wording in API docs after store -> save change

* docs: tag (save_)activations as new in spaCy 4.0

* Fix copied line in morphologizer activations test

* Don't train in any test_save_activations test

* Rename activations

- "probs" -> "probabilities"
- "guesses" -> "label_ids", except in the edit tree lemmatizer, where
  "guesses" -> "tree_ids".

* Remove unused W400 warning.

This warning was used when we still allowed the user to specify
which activations to save.

* Formatting fixes

Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>

* Replace "kb_ids" by a constant

* spancat: replace a cast by an assertion

* Fix EOF spacing

* Fix comments in test_save_activations tests

* Do not set RNG seed in activation saving tests

* Revert "spancat: replace a cast by an assertion"

This reverts commit 0bd5730d16.

Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
2022-09-13 09:51:12 +02:00
..
__init__.pxd * Break up tokens.pyx into tokens/doc.pyx, tokens/token.pyx, tokens/spans.pyx 2015-07-13 20:20:58 +02:00
__init__.py Make stable private modules public and adjust names (#11353) 2022-08-30 13:56:35 +02:00
doc_bin.py Make stable private modules public and adjust names (#11353) 2022-08-30 13:56:35 +02:00
doc.pxd Store activations in Docs when save_activations is enabled (#11002) 2022-09-13 09:51:12 +02:00
doc.pyi Store activations in Docs when save_activations is enabled (#11002) 2022-09-13 09:51:12 +02:00
doc.pyx Store activations in Docs when save_activations is enabled (#11002) 2022-09-13 09:51:12 +02:00
graph.pxd Add SpanGroup and Graph container types to represent arbitrary annotations (#6696) 2021-01-14 17:30:41 +11:00
graph.pyx Refactor error messages to remove hardcoded strings (#10729) 2022-05-02 13:38:46 +02:00
morphanalysis.pxd Morphology/Morphologizer optimizations and refactoring (#11024) 2022-07-15 11:14:08 +02:00
morphanalysis.pyi 🏷 Add Mypy check to CI and ignore all existing Mypy errors (#9167) 2021-10-14 15:21:40 +02:00
morphanalysis.pyx Morphology/Morphologizer optimizations and refactoring (#11024) 2022-07-15 11:14:08 +02:00
retokenizer.pyi Make stable private modules public and adjust names (#11353) 2022-08-30 13:56:35 +02:00
retokenizer.pyx Make stable private modules public and adjust names (#11353) 2022-08-30 13:56:35 +02:00
span_group.pxd Span/SpanGroup: wrap SpanC in shared_ptr (#9869) 2022-01-12 13:38:52 +01:00
span_group.pyi SpanGroup(s)-related optimizations (#11380) 2022-08-31 09:03:20 +02:00
span_group.pyx Merge branch 'develop' into merge-develop-into-v4 2022-09-07 11:35:47 +02:00
span_groups.py Merge branch 'develop' into merge-develop-into-v4 2022-09-07 11:35:47 +02:00
span.pxd Span/SpanGroup: wrap SpanC in shared_ptr (#9869) 2022-01-12 13:38:52 +01:00
span.pyi Make Span/Doc.ents more consistent for ent_kb_id and ent_id (#11328) 2022-08-22 20:28:57 +02:00
span.pyx Make Span/Doc.ents more consistent for ent_kb_id and ent_id (#11328) 2022-08-22 20:28:57 +02:00
token.pxd cleanup 2021-01-13 14:20:05 +01:00
token.pyi 🏷 Add Mypy check to CI and ignore all existing Mypy errors (#9167) 2021-10-14 15:21:40 +02:00
token.pyx Merge remote-tracking branch 'upstream/master' into merge-master-v4-20220728 2022-07-28 13:53:59 +02:00
underscore.py Update typing hints (#10109) 2022-01-28 16:59:54 +01:00