spaCy/spacy
Matthew Honnibal bede11b67c
Improve label management in parser and NER (#2108)
This patch does a few smallish things that tighten up the training workflow a little, and allow memory use during training to be reduced by letting the GoldCorpus stream data properly.

Previously, the parser and entity recognizer read and saved labels as lists, with extra labels noted separately. Lists were used becaue ordering is very important, to ensure that the label-to-class mapping is stable.

We now manage labels as nested dictionaries, first keyed by the action, and then keyed by the label. Values are frequencies. The trick is, how do we save new labels? We need to make sure we iterate over these in the same order they're added. Otherwise, we'll get different class IDs, and the model's predictions won't make sense.

To allow stable sorting, we map the new labels to negative values. If we have two new labels, they'll be noted as having "frequency" -1 and -2. The next new label will then have "frequency" -3. When we sort by (frequency, label), we then get a stable sort.

Storing frequencies then allows us to make the next nice improvement. Previously we had to iterate over the whole training set, to pre-process it for the deprojectivisation. This led to storing the whole training set in memory. This was most of the required memory during training.

To prevent this, we now store the frequencies as we stream in the data, and deprojectivize as we go. Once we've built the frequencies, we can then apply a frequency cut-off when we decide how many classes to make.

Finally, to allow proper data streaming, we also have to have some way of shuffling the iterator. This is awkward if the training files have multiple documents in them. To solve this, the GoldCorpus class now writes the training data to disk in msgpack files, one per document. We can then shuffle the data by shuffling the paths.

This is a squash merge, as I made a lot of very small commits. Individual commit messages below.

* Simplify label management for TransitionSystem and its subclasses

* Fix serialization for new label handling format in parser

* Simplify and improve GoldCorpus class. Reduce memory use, write to temp dir

* Set actions in transition system

* Require thinc 6.11.1.dev4

* Fix error in parser init

* Add unicode declaration

* Fix unicode declaration

* Update textcat test

* Try to get model training on less memory

* Print json loc for now

* Try rapidjson to reduce memory use

* Remove rapidjson requirement

* Try rapidjson for reduced mem usage

* Handle None heads when projectivising

* Stream json docs

* Fix train script

* Handle projectivity in GoldParse

* Fix projectivity handling

* Add minibatch_by_words util from ud_train

* Minibatch by number of words in spacy.cli.train

* Move minibatch_by_words util to spacy.util

* Fix label handling

* More hacking at label management in parser

* Fix encoding in msgpack serialization in GoldParse

* Adjust batch sizes in parser training

* Fix minibatch_by_words

* Add merge_subtokens function to pipeline.pyx

* Register merge_subtokens factory

* Restore use of msgpack tmp directory

* Use minibatch-by-words in train

* Handle retokenization in scorer

* Change back-off approach for missing labels. Use 'dep' label

* Update NER for new label management

* Set NER tags for over-segmented words

* Fix label alignment in gold

* Fix label back-off for infrequent labels

* Fix int type in labels dict key

* Fix int type in labels dict key

* Update feature definition for 8 feature set

* Update ud-train script for new label stuff

* Fix json streamer

* Print the line number if conll eval fails

* Update children and sentence boundaries after deprojectivisation

* Export set_children_from_heads from doc.pxd

* Render parses during UD training

* Remove print statement

* Require thinc 6.11.1.dev6. Try adding wheel as install_requires

* Set different dev version, to flush pip cache

* Update thinc version

* Update GoldCorpus docs

* Remove print statements

* Fix formatting and links [ci skip]
2018-03-19 02:58:08 +01:00
..
cli Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
data Make spacy/data a package 2017-03-18 20:04:22 +01:00
displacy Don't use deprecated Doc.merge call in displaCy 2018-01-27 11:25:05 +01:00
lang maybe not a good idea to remove also 2018-03-14 14:47:24 +01:00
syntax Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
tests Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
tokens Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
__init__.pxd * Seems to be working after refactor. Need to wire up more POS tag features, and wire up save/load of POS tags. 2014-10-24 02:23:42 +11:00
__init__.py Remove dummy variable from function calls 2018-01-05 09:37:05 +01:00
__main__.py Link in spaCy CoNLL commands 2018-03-10 23:42:15 +01:00
_align.pyx Fix many-to-one alignment 2018-02-24 16:03:50 +01:00
_matcher2_notes.py Update notes on matcher2 2018-02-13 11:45:45 +01:00
_ml.py Add depth argument for text classifier 2018-03-16 12:37:31 +01:00
about.py Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
attrs.pxd Fix LANG symbol 2018-02-17 18:10:50 +01:00
attrs.pyx missing PrepCase attribute 2018-02-18 14:46:12 +00:00
compat.py Drop six and related hacks as a dependency 2018-02-18 13:29:56 +01:00
glossary.py Fix typo in glossary (resolves #1964) 2018-02-10 11:58:41 +01:00
gold.pxd Add support for sent_start to GoldParse 2017-08-25 20:03:14 -05:00
gold.pyx Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
language.py Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
lemmatizer.py Don't lower-case lemmas of proper nouns 2018-02-21 16:01:16 +01:00
lexeme.pxd WIP on stringstore change. 27 failures 2017-05-28 14:06:40 +02:00
lexeme.pyx added new lexical feat to lexeme 2018-02-11 18:51:48 +01:00
matcher.pyx Move cython declarations in matcher.pyx 2018-02-24 10:32:18 +01:00
morphology.pxd fix typo/missing here too 2018-02-18 14:38:27 +00:00
morphology.pyx fix typo/missing here too 2018-02-18 14:38:27 +00:00
parts_of_speech.pxd Add support for Universal Dependencies v2.0 2017-03-03 13:17:34 +01:00
parts_of_speech.pyx Tidy up rest 2017-10-27 21:07:59 +02:00
pipeline.pxd Fix names of pipeline components 2017-10-26 12:38:23 +02:00
pipeline.pyx Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
scorer.py Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
strings.pxd Try to fix StringStore clean up (see #1506) 2017-11-11 03:11:27 +03:00
strings.pyx Use safer method to get string without hit 2017-11-14 22:58:46 +03:00
structs.pxd Make TokenC.sent_tart an int, to allow ternary value 2017-10-08 19:58:54 +02:00
symbols.pxd Fix inconsistencies in the symbols table 2018-02-18 13:51:31 +01:00
symbols.pyx Fix inconsistencies in the symbols table 2018-02-18 13:51:31 +01:00
tokenizer.pxd Disable tokenizer cache for special-cases. Fixes #1250 2017-10-24 16:08:05 +02:00
tokenizer.pyx Merge pull request #1611 from fsonntag/master 2017-11-29 23:11:23 +01:00
typedefs.pxd Work on changing StringStore to return hashes. 2017-05-28 12:36:27 +02:00
typedefs.pyx Tidy up rest 2017-10-27 21:07:59 +02:00
util.py Improve label management in parser and NER (#2108) 2018-03-19 02:58:08 +01:00
vectors.pyx Fix Vectors pickling 2018-03-10 22:53:42 +01:00
vocab.pxd Add Vocab.cfg attr, to hold stuff like oov probs 2017-10-30 16:08:50 +01:00
vocab.pyx Make Vocab.__contains__ work with ints. Fixes #1868 2018-01-23 23:26:47 +01:00