2020-04-02 15:46:32 +03:00
|
|
|
# cython: infer_types=True, profile=True
|
2018-09-25 00:14:06 +03:00
|
|
|
cimport numpy as np
|
|
|
|
|
2020-03-02 13:48:10 +03:00
|
|
|
import numpy
|
2020-04-02 15:46:32 +03:00
|
|
|
import srsly
|
|
|
|
from thinc.api import to_categorical
|
2020-01-29 19:06:46 +03:00
|
|
|
|
2020-03-02 13:48:10 +03:00
|
|
|
from ..tokens.doc cimport Doc
|
|
|
|
from ..vocab cimport Vocab
|
|
|
|
from ..morphology cimport Morphology
|
2020-04-02 15:46:32 +03:00
|
|
|
from ..parts_of_speech import IDS as POS_IDS
|
|
|
|
from ..symbols import POS
|
2020-03-02 13:48:10 +03:00
|
|
|
|
2019-03-07 12:46:27 +03:00
|
|
|
from .. import util
|
2019-10-27 15:35:49 +03:00
|
|
|
from ..language import component
|
2020-01-29 19:06:46 +03:00
|
|
|
from ..util import link_vectors_to_models, create_default_optimizer
|
2019-03-07 12:46:27 +03:00
|
|
|
from ..errors import Errors, TempErrors
|
2020-04-02 15:46:32 +03:00
|
|
|
from .pipes import Tagger, _load_cfg
|
|
|
|
from .. import util
|
2018-09-25 00:14:06 +03:00
|
|
|
|
|
|
|
|
2019-10-27 15:35:49 +03:00
|
|
|
@component("morphologizer", assigns=["token.morph", "token.pos"])
|
2020-04-02 15:46:32 +03:00
|
|
|
class Morphologizer(Tagger):
|
2019-10-27 15:35:49 +03:00
|
|
|
|
2020-02-27 20:42:27 +03:00
|
|
|
def __init__(self, vocab, model, **cfg):
|
2018-09-25 00:14:06 +03:00
|
|
|
self.vocab = vocab
|
|
|
|
self.model = model
|
2020-04-02 15:46:32 +03:00
|
|
|
self._rehearsal_model = None
|
2019-12-22 03:53:56 +03:00
|
|
|
self.cfg = dict(sorted(cfg.items()))
|
2020-04-02 15:46:32 +03:00
|
|
|
self.cfg.setdefault("labels", {})
|
|
|
|
self.cfg.setdefault("morph_pos", {})
|
2018-09-25 00:14:06 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def labels(self):
|
2020-04-02 15:46:32 +03:00
|
|
|
return tuple(self.cfg["labels"].keys())
|
|
|
|
|
|
|
|
def add_label(self, label):
|
|
|
|
if not isinstance(label, str):
|
|
|
|
raise ValueError(Errors.E187)
|
|
|
|
if label in self.labels:
|
|
|
|
return 0
|
|
|
|
morph = Morphology.feats_to_dict(label)
|
|
|
|
norm_morph_pos = self.vocab.strings[self.vocab.morphology.add(morph)]
|
|
|
|
pos = morph.get("POS", "")
|
|
|
|
if norm_morph_pos not in self.cfg["labels"]:
|
|
|
|
self.cfg["labels"][norm_morph_pos] = norm_morph_pos
|
|
|
|
self.cfg["morph_pos"][norm_morph_pos] = POS_IDS[pos]
|
|
|
|
return 1
|
2018-09-25 00:14:06 +03:00
|
|
|
|
2020-02-27 20:42:27 +03:00
|
|
|
def begin_training(self, get_examples=lambda: [], pipeline=None, sgd=None,
|
|
|
|
**kwargs):
|
2020-04-02 15:46:32 +03:00
|
|
|
for example in get_examples():
|
|
|
|
for i, morph in enumerate(example.token_annotation.morphs):
|
|
|
|
pos = example.token_annotation.get_pos(i)
|
|
|
|
morph = Morphology.feats_to_dict(morph)
|
|
|
|
norm_morph = self.vocab.strings[self.vocab.morphology.add(morph)]
|
|
|
|
if pos:
|
|
|
|
morph["POS"] = pos
|
|
|
|
norm_morph_pos = self.vocab.strings[self.vocab.morphology.add(morph)]
|
|
|
|
if norm_morph_pos not in self.cfg["labels"]:
|
|
|
|
self.cfg["labels"][norm_morph_pos] = norm_morph
|
|
|
|
self.cfg["morph_pos"][norm_morph_pos] = POS_IDS[pos]
|
2020-02-27 20:42:27 +03:00
|
|
|
self.set_output(len(self.labels))
|
|
|
|
self.model.initialize()
|
2020-04-02 15:46:32 +03:00
|
|
|
link_vectors_to_models(self.vocab)
|
2020-02-27 20:42:27 +03:00
|
|
|
if sgd is None:
|
|
|
|
sgd = self.create_optimizer()
|
|
|
|
return sgd
|
|
|
|
|
2020-04-02 15:46:32 +03:00
|
|
|
def set_annotations(self, docs, batch_tag_ids):
|
2018-09-25 00:14:06 +03:00
|
|
|
if isinstance(docs, Doc):
|
|
|
|
docs = [docs]
|
|
|
|
cdef Doc doc
|
|
|
|
cdef Vocab vocab = self.vocab
|
|
|
|
for i, doc in enumerate(docs):
|
2020-04-02 15:46:32 +03:00
|
|
|
doc_tag_ids = batch_tag_ids[i]
|
|
|
|
if hasattr(doc_tag_ids, "get"):
|
|
|
|
doc_tag_ids = doc_tag_ids.get()
|
|
|
|
for j, tag_id in enumerate(doc_tag_ids):
|
|
|
|
morph = self.labels[tag_id]
|
|
|
|
doc.c[j].morph = self.vocab.morphology.add(self.cfg["labels"][morph])
|
|
|
|
doc.c[j].pos = self.cfg["morph_pos"][morph]
|
|
|
|
|
|
|
|
doc.is_morphed = True
|
2018-09-25 00:14:06 +03:00
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
def get_loss(self, examples, scores):
|
2020-04-02 15:46:32 +03:00
|
|
|
scores = self.model.ops.flatten(scores)
|
|
|
|
tag_index = {tag: i for i, tag in enumerate(self.labels)}
|
2018-09-25 00:14:06 +03:00
|
|
|
cdef int idx = 0
|
2020-04-02 15:46:32 +03:00
|
|
|
correct = numpy.zeros((scores.shape[0],), dtype="i")
|
|
|
|
guesses = scores.argmax(axis=1)
|
|
|
|
known_labels = numpy.ones((scores.shape[0], 1), dtype="f")
|
|
|
|
for ex in examples:
|
|
|
|
gold = ex.gold
|
|
|
|
for i in range(len(gold.morphs)):
|
|
|
|
pos = gold.pos[i] if i < len(gold.pos) else ""
|
|
|
|
morph = gold.morphs[i]
|
|
|
|
feats = Morphology.feats_to_dict(morph)
|
|
|
|
if pos:
|
|
|
|
feats["POS"] = pos
|
|
|
|
if len(feats) > 0:
|
|
|
|
morph = self.vocab.strings[self.vocab.morphology.add(feats)]
|
|
|
|
if morph == "":
|
|
|
|
morph = Morphology.EMPTY_MORPH
|
|
|
|
if morph is None:
|
|
|
|
correct[idx] = guesses[idx]
|
|
|
|
elif morph in tag_index:
|
|
|
|
correct[idx] = tag_index[morph]
|
2018-09-25 00:14:06 +03:00
|
|
|
else:
|
2020-04-02 15:46:32 +03:00
|
|
|
correct[idx] = 0
|
|
|
|
known_labels[idx] = 0.
|
2018-09-25 00:14:06 +03:00
|
|
|
idx += 1
|
2020-04-02 15:46:32 +03:00
|
|
|
correct = self.model.ops.xp.array(correct, dtype="i")
|
|
|
|
d_scores = scores - to_categorical(correct, n_classes=scores.shape[1])
|
|
|
|
d_scores *= self.model.ops.asarray(known_labels)
|
2018-09-25 00:14:06 +03:00
|
|
|
loss = (d_scores**2).sum()
|
2020-04-02 15:46:32 +03:00
|
|
|
docs = [ex.doc for ex in examples]
|
2018-09-25 00:14:06 +03:00
|
|
|
d_scores = self.model.ops.unflatten(d_scores, [len(d) for d in docs])
|
|
|
|
return float(loss), d_scores
|
|
|
|
|
2020-04-02 15:46:32 +03:00
|
|
|
def to_bytes(self, exclude=tuple(), **kwargs):
|
|
|
|
serialize = {}
|
|
|
|
serialize["model"] = self.model.to_bytes
|
|
|
|
serialize["vocab"] = self.vocab.to_bytes
|
|
|
|
serialize["cfg"] = lambda: srsly.json_dumps(self.cfg)
|
|
|
|
exclude = util.get_serialization_exclude(serialize, exclude, kwargs)
|
|
|
|
return util.to_bytes(serialize, exclude)
|
|
|
|
|
|
|
|
def from_bytes(self, bytes_data, exclude=tuple(), **kwargs):
|
|
|
|
def load_model(b):
|
|
|
|
try:
|
|
|
|
self.model.from_bytes(b)
|
|
|
|
except AttributeError:
|
|
|
|
raise ValueError(Errors.E149)
|
|
|
|
|
|
|
|
deserialize = {
|
|
|
|
"vocab": lambda b: self.vocab.from_bytes(b),
|
|
|
|
"cfg": lambda b: self.cfg.update(srsly.json_loads(b)),
|
|
|
|
"model": lambda b: load_model(b),
|
|
|
|
}
|
|
|
|
exclude = util.get_serialization_exclude(deserialize, exclude, kwargs)
|
|
|
|
util.from_bytes(bytes_data, deserialize, exclude)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def to_disk(self, path, exclude=tuple(), **kwargs):
|
|
|
|
serialize = {
|
|
|
|
"vocab": lambda p: self.vocab.to_disk(p),
|
|
|
|
"model": lambda p: p.open("wb").write(self.model.to_bytes()),
|
|
|
|
"cfg": lambda p: srsly.write_json(p, self.cfg),
|
|
|
|
}
|
|
|
|
exclude = util.get_serialization_exclude(serialize, exclude, kwargs)
|
|
|
|
util.to_disk(path, serialize, exclude)
|
|
|
|
|
|
|
|
def from_disk(self, path, exclude=tuple(), **kwargs):
|
|
|
|
def load_model(p):
|
|
|
|
with p.open("rb") as file_:
|
|
|
|
try:
|
|
|
|
self.model.from_bytes(file_.read())
|
|
|
|
except AttributeError:
|
|
|
|
raise ValueError(Errors.E149)
|
|
|
|
|
|
|
|
deserialize = {
|
|
|
|
"vocab": lambda p: self.vocab.from_disk(p),
|
|
|
|
"cfg": lambda p: self.cfg.update(_load_cfg(p)),
|
|
|
|
"model": load_model,
|
|
|
|
}
|
|
|
|
exclude = util.get_serialization_exclude(deserialize, exclude, kwargs)
|
|
|
|
util.from_disk(path, deserialize, exclude)
|
|
|
|
return self
|