mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-26 01:46:28 +03:00
06f0a8daa0
* fix grad_clip naming * cleaning up pretrained_vectors out of cfg * further refactoring Model init's * move Model building out of pipes * further refactor to require a model config when creating a pipe * small fixes * making cfg in nn_parser more consistent * fixing nr_class for parser * fixing nn_parser's nO * fix printing of loss * architectures in own file per type, consistent naming * convenience methods default_tagger_config and default_tok2vec_config * let create_pipe access default config if available for that component * default_parser_config * move defaults to separate folder * allow reading nlp from package or dir with argument 'name' * architecture spacy.VocabVectors.v1 to read static vectors from file * cleanup * default configs for nel, textcat, morphologizer, tensorizer * fix imports * fixing unit tests * fixes and clean up * fixing defaults, nO, fix unit tests * restore parser IO * fix IO * 'fix' serialization test * add *.cfg to manifest * fix example configs with additional arguments * replace Morpohologizer with Tagger * add IO bit when testing overfitting of tagger (currently failing) * fix IO - don't initialize when reading from disk * expand overfitting tests to also check IO goes OK * remove dropout from HashEmbed to fix Tagger performance * add defaults for sentrec * update thinc * always pass a Model instance to a Pipe * fix piped_added statement * remove obsolete W029 * remove obsolete errors * restore byte checking tests (work again) * clean up test * further test cleanup * convert from config to Model in create_pipe * bring back error when component is not initialized * cleanup * remove calls for nlp2.begin_training * use thinc.api in imports * allow setting charembed's nM and nC * fix for hardcoded nM/nC + unit test * formatting fixes * trigger build
112 lines
3.6 KiB
Python
112 lines
3.6 KiB
Python
import pytest
|
|
import random
|
|
import numpy.random
|
|
|
|
from spacy import util
|
|
from spacy.lang.en import English
|
|
from spacy.language import Language
|
|
from spacy.pipeline import TextCategorizer
|
|
from spacy.tests.util import make_tempdir
|
|
from spacy.tokens import Doc
|
|
from spacy.gold import GoldParse
|
|
|
|
TRAIN_DATA = [
|
|
("I'm so happy.", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
|
|
("I'm so angry", {"cats": {"POSITIVE": 0.0, "NEGATIVE": 1.0}}),
|
|
]
|
|
|
|
|
|
@pytest.mark.skip(reason="Test is flakey when run with others")
|
|
def test_simple_train():
|
|
nlp = Language()
|
|
nlp.add_pipe(nlp.create_pipe("textcat"))
|
|
nlp.get_pipe("textcat").add_label("answer")
|
|
nlp.begin_training()
|
|
for i in range(5):
|
|
for text, answer in [
|
|
("aaaa", 1.0),
|
|
("bbbb", 0),
|
|
("aa", 1.0),
|
|
("bbbbbbbbb", 0.0),
|
|
("aaaaaa", 1),
|
|
]:
|
|
nlp.update((text, {"cats": {"answer": answer}}))
|
|
doc = nlp("aaa")
|
|
assert "answer" in doc.cats
|
|
assert doc.cats["answer"] >= 0.5
|
|
|
|
|
|
@pytest.mark.skip(reason="Test is flakey when run with others")
|
|
def test_textcat_learns_multilabel():
|
|
random.seed(5)
|
|
numpy.random.seed(5)
|
|
docs = []
|
|
nlp = Language()
|
|
letters = ["a", "b", "c"]
|
|
for w1 in letters:
|
|
for w2 in letters:
|
|
cats = {letter: float(w2 == letter) for letter in letters}
|
|
docs.append((Doc(nlp.vocab, words=["d"] * 3 + [w1, w2] + ["d"] * 3), cats))
|
|
random.shuffle(docs)
|
|
model = TextCategorizer(nlp.vocab, width=8)
|
|
for letter in letters:
|
|
model.add_label(letter)
|
|
optimizer = model.begin_training()
|
|
for i in range(30):
|
|
losses = {}
|
|
Ys = [GoldParse(doc, cats=cats) for doc, cats in docs]
|
|
Xs = [doc for doc, cats in docs]
|
|
model.update(Xs, Ys, sgd=optimizer, losses=losses)
|
|
random.shuffle(docs)
|
|
for w1 in letters:
|
|
for w2 in letters:
|
|
doc = Doc(nlp.vocab, words=["d"] * 3 + [w1, w2] + ["d"] * 3)
|
|
truth = {letter: w2 == letter for letter in letters}
|
|
model(doc)
|
|
for cat, score in doc.cats.items():
|
|
if not truth[cat]:
|
|
assert score < 0.5
|
|
else:
|
|
assert score > 0.5
|
|
|
|
|
|
def test_label_types():
|
|
nlp = Language()
|
|
nlp.add_pipe(nlp.create_pipe("textcat"))
|
|
nlp.get_pipe("textcat").add_label("answer")
|
|
with pytest.raises(ValueError):
|
|
nlp.get_pipe("textcat").add_label(9)
|
|
|
|
|
|
def test_overfitting_IO():
|
|
# Simple test to try and quickly overfit the textcat component - ensuring the ML models work correctly
|
|
nlp = English()
|
|
textcat = nlp.create_pipe("textcat")
|
|
for _, annotations in TRAIN_DATA:
|
|
for label, value in annotations.get("cats").items():
|
|
textcat.add_label(label)
|
|
nlp.add_pipe(textcat)
|
|
optimizer = nlp.begin_training()
|
|
|
|
for i in range(50):
|
|
losses = {}
|
|
nlp.update(TRAIN_DATA, sgd=optimizer, losses=losses)
|
|
assert losses["textcat"] < 0.01
|
|
|
|
# test the trained model
|
|
test_text = "I am happy."
|
|
doc = nlp(test_text)
|
|
cats = doc.cats
|
|
# note that by default, exclusive_classes = false so we need a bigger error margin
|
|
assert cats["POSITIVE"] > 0.9
|
|
assert cats["POSITIVE"] + cats["NEGATIVE"] == pytest.approx(1.0, 0.1)
|
|
|
|
# Also test the results are still the same after IO
|
|
with make_tempdir() as tmp_dir:
|
|
nlp.to_disk(tmp_dir)
|
|
nlp2 = util.load_model_from_path(tmp_dir)
|
|
doc2 = nlp2(test_text)
|
|
cats2 = doc2.cats
|
|
assert cats2["POSITIVE"] > 0.9
|
|
assert cats2["POSITIVE"] + cats2["NEGATIVE"] == pytest.approx(1.0, 0.1)
|