2018-07-25 00:38:44 +03:00
|
|
|
import pytest
|
|
|
|
import random
|
|
|
|
import numpy.random
|
2020-02-27 20:42:27 +03:00
|
|
|
|
|
|
|
from spacy import util
|
|
|
|
from spacy.lang.en import English
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.language import Language
|
|
|
|
from spacy.pipeline import TextCategorizer
|
2020-02-27 20:42:27 +03:00
|
|
|
from spacy.tests.util import make_tempdir
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.tokens import Doc
|
|
|
|
from spacy.gold import GoldParse
|
2017-11-07 00:04:29 +03:00
|
|
|
|
2020-01-29 19:06:46 +03:00
|
|
|
TRAIN_DATA = [
|
|
|
|
("I'm so happy.", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
|
|
|
|
("I'm so angry", {"cats": {"POSITIVE": 0.0, "NEGATIVE": 1.0}}),
|
|
|
|
]
|
|
|
|
|
2017-11-07 03:25:54 +03:00
|
|
|
|
2018-08-15 17:56:55 +03:00
|
|
|
@pytest.mark.skip(reason="Test is flakey when run with others")
|
2017-11-07 00:04:29 +03:00
|
|
|
def test_simple_train():
|
|
|
|
nlp = Language()
|
2018-11-27 03:09:36 +03:00
|
|
|
nlp.add_pipe(nlp.create_pipe("textcat"))
|
|
|
|
nlp.get_pipe("textcat").add_label("answer")
|
2017-11-07 00:04:29 +03:00
|
|
|
nlp.begin_training()
|
|
|
|
for i in range(5):
|
2018-11-27 03:09:36 +03:00
|
|
|
for text, answer in [
|
|
|
|
("aaaa", 1.0),
|
|
|
|
("bbbb", 0),
|
|
|
|
("aa", 1.0),
|
|
|
|
("bbbbbbbbb", 0.0),
|
|
|
|
("aaaaaa", 1),
|
|
|
|
]:
|
2019-11-11 19:35:27 +03:00
|
|
|
nlp.update((text, {"cats": {"answer": answer}}))
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = nlp("aaa")
|
|
|
|
assert "answer" in doc.cats
|
|
|
|
assert doc.cats["answer"] >= 0.5
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
@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()
|
2018-11-27 03:09:36 +03:00
|
|
|
letters = ["a", "b", "c"]
|
2018-07-25 00:38:44 +03:00
|
|
|
for w1 in letters:
|
|
|
|
for w2 in letters:
|
2018-11-27 03:09:36 +03:00
|
|
|
cats = {letter: float(w2 == letter) for letter in letters}
|
|
|
|
docs.append((Doc(nlp.vocab, words=["d"] * 3 + [w1, w2] + ["d"] * 3), cats))
|
2018-07-25 00:38:44 +03:00
|
|
|
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:
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = Doc(nlp.vocab, words=["d"] * 3 + [w1, w2] + ["d"] * 3)
|
|
|
|
truth = {letter: w2 == letter for letter in letters}
|
2018-07-25 00:38:44 +03:00
|
|
|
model(doc)
|
|
|
|
for cat, score in doc.cats.items():
|
|
|
|
if not truth[cat]:
|
|
|
|
assert score < 0.5
|
|
|
|
else:
|
|
|
|
assert score > 0.5
|
2019-11-21 18:24:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2020-01-29 19:06:46 +03:00
|
|
|
|
|
|
|
|
2020-02-27 20:42:27 +03:00
|
|
|
def test_overfitting_IO():
|
2020-01-29 19:06:46 +03:00
|
|
|
# Simple test to try and quickly overfit the textcat component - ensuring the ML models work correctly
|
2020-02-27 20:42:27 +03:00
|
|
|
nlp = English()
|
2020-01-29 19:06:46 +03:00
|
|
|
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)
|
2020-02-27 20:42:27 +03:00
|
|
|
assert losses["textcat"] < 0.01
|
2020-01-29 19:06:46 +03:00
|
|
|
|
|
|
|
# test the trained model
|
|
|
|
test_text = "I am happy."
|
|
|
|
doc = nlp(test_text)
|
|
|
|
cats = doc.cats
|
2020-02-27 20:42:27 +03:00
|
|
|
# note that by default, exclusive_classes = false so we need a bigger error margin
|
2020-01-29 19:06:46 +03:00
|
|
|
assert cats["POSITIVE"] > 0.9
|
2020-02-27 20:42:27 +03:00
|
|
|
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)
|