2020-07-22 14:42:59 +03:00
|
|
|
# cython: infer_types=True, profile=True, binding=True
|
|
|
|
from typing import Optional
|
2023-06-14 18:48:41 +03:00
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
import numpy
|
2023-06-14 18:48:41 +03:00
|
|
|
from thinc.api import Config, CosineDistance, Model, set_dropout_rate, to_categorical
|
2020-07-22 14:42:59 +03:00
|
|
|
|
|
|
|
from ..tokens.doc cimport Doc
|
|
|
|
|
2023-06-14 18:48:41 +03:00
|
|
|
from ..attrs import ID, POS
|
|
|
|
from ..errors import Errors
|
2020-07-22 14:42:59 +03:00
|
|
|
from ..language import Language
|
2023-06-14 18:48:41 +03:00
|
|
|
from ..training import validate_examples
|
2020-07-31 00:30:54 +03:00
|
|
|
from ._parser_internals import nonproj
|
2023-06-14 18:48:41 +03:00
|
|
|
from .tagger import Tagger
|
|
|
|
from .trainable_pipe import TrainablePipe
|
2020-07-22 14:42:59 +03:00
|
|
|
|
|
|
|
default_model_config = """
|
|
|
|
[model]
|
|
|
|
@architectures = "spacy.MultiTask.v1"
|
|
|
|
maxout_pieces = 3
|
|
|
|
token_vector_width = 96
|
|
|
|
|
|
|
|
[model.tok2vec]
|
2021-04-22 11:04:15 +03:00
|
|
|
@architectures = "spacy.HashEmbedCNN.v2"
|
2020-07-22 14:42:59 +03:00
|
|
|
pretrained_vectors = null
|
|
|
|
width = 96
|
|
|
|
depth = 4
|
|
|
|
embed_size = 2000
|
|
|
|
window_size = 1
|
|
|
|
maxout_pieces = 2
|
|
|
|
subword_features = true
|
|
|
|
"""
|
|
|
|
DEFAULT_MT_MODEL = Config().from_str(default_model_config)["model"]
|
|
|
|
|
|
|
|
|
|
|
|
@Language.factory(
|
|
|
|
"nn_labeller",
|
|
|
|
default_config={"labels": None, "target": "dep_tag_offset", "model": DEFAULT_MT_MODEL}
|
|
|
|
)
|
|
|
|
def make_nn_labeller(nlp: Language, name: str, model: Model, labels: Optional[dict], target: str):
|
|
|
|
return MultitaskObjective(nlp.vocab, model, name)
|
|
|
|
|
|
|
|
|
|
|
|
class MultitaskObjective(Tagger):
|
|
|
|
"""Experimental: Assist training of a parser or tagger, by training a
|
|
|
|
side-objective.
|
|
|
|
"""
|
|
|
|
|
2020-11-11 23:34:12 +03:00
|
|
|
def __init__(self, vocab, model, name="nn_labeller", *, target):
|
2020-07-22 14:42:59 +03:00
|
|
|
self.vocab = vocab
|
|
|
|
self.model = model
|
|
|
|
self.name = name
|
|
|
|
if target == "dep":
|
|
|
|
self.make_label = self.make_dep
|
|
|
|
elif target == "tag":
|
|
|
|
self.make_label = self.make_tag
|
|
|
|
elif target == "ent":
|
|
|
|
self.make_label = self.make_ent
|
|
|
|
elif target == "dep_tag_offset":
|
|
|
|
self.make_label = self.make_dep_tag_offset
|
|
|
|
elif target == "ent_tag":
|
|
|
|
self.make_label = self.make_ent_tag
|
|
|
|
elif target == "sent_start":
|
|
|
|
self.make_label = self.make_sent_start
|
|
|
|
elif hasattr(target, "__call__"):
|
|
|
|
self.make_label = target
|
|
|
|
else:
|
|
|
|
raise ValueError(Errors.E016)
|
2020-11-11 23:34:12 +03:00
|
|
|
cfg = {"labels": {}, "target": target}
|
2020-07-22 14:42:59 +03:00
|
|
|
self.cfg = dict(cfg)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def labels(self):
|
|
|
|
return self.cfg.setdefault("labels", {})
|
|
|
|
|
|
|
|
@labels.setter
|
|
|
|
def labels(self, value):
|
|
|
|
self.cfg["labels"] = value
|
|
|
|
|
|
|
|
def set_annotations(self, docs, dep_ids):
|
|
|
|
pass
|
|
|
|
|
2020-11-11 23:34:12 +03:00
|
|
|
def initialize(self, get_examples, nlp=None, labels=None):
|
2020-08-12 00:29:31 +03:00
|
|
|
if not hasattr(get_examples, "__call__"):
|
|
|
|
err = Errors.E930.format(name="MultitaskObjective", obj=type(get_examples))
|
|
|
|
raise ValueError(err)
|
2020-11-11 23:34:12 +03:00
|
|
|
if labels is not None:
|
|
|
|
self.labels = labels
|
|
|
|
else:
|
|
|
|
for example in get_examples():
|
|
|
|
for token in example.y:
|
|
|
|
label = self.make_label(token)
|
|
|
|
if label is not None and label not in self.labels:
|
|
|
|
self.labels[label] = len(self.labels)
|
2020-09-08 23:44:25 +03:00
|
|
|
self.model.initialize() # TODO: fix initialization by defining X and Y
|
2020-07-22 14:42:59 +03:00
|
|
|
|
|
|
|
def predict(self, docs):
|
|
|
|
tokvecs = self.model.get_ref("tok2vec")(docs)
|
|
|
|
scores = self.model.get_ref("softmax")(tokvecs)
|
|
|
|
return tokvecs, scores
|
|
|
|
|
|
|
|
def get_loss(self, examples, scores):
|
|
|
|
cdef int idx = 0
|
|
|
|
correct = numpy.zeros((scores.shape[0],), dtype="i")
|
|
|
|
guesses = scores.argmax(axis=1)
|
|
|
|
docs = [eg.predicted for eg in examples]
|
|
|
|
for i, eg in enumerate(examples):
|
|
|
|
# Handles alignment for tokenization differences
|
|
|
|
doc_annots = eg.get_aligned() # TODO
|
|
|
|
for j in range(len(eg.predicted)):
|
|
|
|
tok_annots = {key: values[j] for key, values in tok_annots.items()}
|
|
|
|
label = self.make_label(j, tok_annots)
|
|
|
|
if label is None or label not in self.labels:
|
|
|
|
correct[idx] = guesses[idx]
|
|
|
|
else:
|
|
|
|
correct[idx] = self.labels[label]
|
|
|
|
idx += 1
|
|
|
|
correct = self.model.ops.xp.array(correct, dtype="i")
|
|
|
|
d_scores = scores - to_categorical(correct, n_classes=scores.shape[1])
|
|
|
|
loss = (d_scores**2).sum()
|
|
|
|
return float(loss), d_scores
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_dep(token):
|
|
|
|
return token.dep_
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_tag(token):
|
|
|
|
return token.tag_
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_ent(token):
|
|
|
|
if token.ent_iob_ == "O":
|
|
|
|
return "O"
|
|
|
|
else:
|
|
|
|
return token.ent_iob_ + "-" + token.ent_type_
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_dep_tag_offset(token):
|
|
|
|
dep = token.dep_
|
|
|
|
tag = token.tag_
|
|
|
|
offset = token.head.i - token.i
|
|
|
|
offset = min(offset, 2)
|
|
|
|
offset = max(offset, -2)
|
|
|
|
return f"{dep}-{tag}:{offset}"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_ent_tag(token):
|
|
|
|
if token.ent_iob_ == "O":
|
|
|
|
ent = "O"
|
|
|
|
else:
|
|
|
|
ent = token.ent_iob_ + "-" + token.ent_type_
|
|
|
|
tag = token.tag_
|
|
|
|
return f"{tag}-{ent}"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_sent_start(token):
|
|
|
|
"""A multi-task objective for representing sentence boundaries,
|
|
|
|
using BILU scheme. (O is impossible)
|
|
|
|
"""
|
|
|
|
if token.is_sent_start and token.is_sent_end:
|
|
|
|
return "U-SENT"
|
|
|
|
elif token.is_sent_start:
|
|
|
|
return "B-SENT"
|
|
|
|
else:
|
|
|
|
return "I-SENT"
|
|
|
|
|
|
|
|
|
2020-10-08 22:33:49 +03:00
|
|
|
class ClozeMultitask(TrainablePipe):
|
2020-07-22 14:42:59 +03:00
|
|
|
def __init__(self, vocab, model, **cfg):
|
|
|
|
self.vocab = vocab
|
|
|
|
self.model = model
|
|
|
|
self.cfg = cfg
|
|
|
|
self.distance = CosineDistance(ignore_zeros=True, normalize=False) # TODO: in config
|
|
|
|
|
|
|
|
def set_annotations(self, docs, dep_ids):
|
|
|
|
pass
|
|
|
|
|
2020-09-29 13:20:26 +03:00
|
|
|
def initialize(self, get_examples, nlp=None):
|
2020-09-08 23:44:25 +03:00
|
|
|
self.model.initialize() # TODO: fix initialization by defining X and Y
|
2020-07-22 14:42:59 +03:00
|
|
|
X = self.model.ops.alloc((5, self.model.get_ref("tok2vec").get_dim("nO")))
|
2020-09-28 22:35:09 +03:00
|
|
|
self.model.output_layer.initialize(X)
|
2020-07-22 14:42:59 +03:00
|
|
|
|
|
|
|
def predict(self, docs):
|
|
|
|
tokvecs = self.model.get_ref("tok2vec")(docs)
|
|
|
|
vectors = self.model.get_ref("output_layer")(tokvecs)
|
|
|
|
return tokvecs, vectors
|
|
|
|
|
|
|
|
def get_loss(self, examples, vectors, prediction):
|
2020-08-12 00:29:31 +03:00
|
|
|
validate_examples(examples, "ClozeMultitask.get_loss")
|
2020-07-22 14:42:59 +03:00
|
|
|
# The simplest way to implement this would be to vstack the
|
|
|
|
# token.vector values, but that's a bit inefficient, especially on GPU.
|
|
|
|
# Instead we fetch the index into the vectors table for each of our tokens,
|
|
|
|
# and look them up all at once. This prevents data copying.
|
|
|
|
ids = self.model.ops.flatten([eg.predicted.to_array(ID).ravel() for eg in examples])
|
|
|
|
target = vectors[ids]
|
|
|
|
gradient = self.distance.get_grad(prediction, target)
|
|
|
|
loss = self.distance.get_loss(prediction, target)
|
2021-02-07 02:51:56 +03:00
|
|
|
return float(loss), gradient
|
2020-07-22 14:42:59 +03:00
|
|
|
|
2021-01-20 03:49:25 +03:00
|
|
|
def update(self, examples, *, drop=0., sgd=None, losses=None):
|
2020-07-22 14:42:59 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
def rehearse(self, examples, drop=0., sgd=None, losses=None):
|
|
|
|
if losses is not None and self.name not in losses:
|
|
|
|
losses[self.name] = 0.
|
|
|
|
set_dropout_rate(self.model, drop)
|
2020-08-12 00:29:31 +03:00
|
|
|
validate_examples(examples, "ClozeMultitask.rehearse")
|
|
|
|
docs = [eg.predicted for eg in examples]
|
|
|
|
predictions, bp_predictions = self.model.begin_update()
|
2020-07-22 14:42:59 +03:00
|
|
|
loss, d_predictions = self.get_loss(examples, self.vocab.vectors.data, predictions)
|
|
|
|
bp_predictions(d_predictions)
|
|
|
|
if sgd is not None:
|
2020-10-05 17:23:33 +03:00
|
|
|
self.finish_update(sgd)
|
2020-07-22 14:42:59 +03:00
|
|
|
if losses is not None:
|
|
|
|
losses[self.name] += loss
|
2020-08-12 00:29:31 +03:00
|
|
|
return losses
|
2020-07-31 00:30:54 +03:00
|
|
|
|
|
|
|
def add_label(self, label):
|
|
|
|
raise NotImplementedError
|