2017-04-15 13:05:47 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2016-10-09 13:24:24 +03:00
|
|
|
|
|
|
|
import random
|
2017-03-11 20:12:48 +03:00
|
|
|
import tqdm
|
2017-05-16 12:21:59 +03:00
|
|
|
from cytoolz import partition_all
|
2017-05-15 22:46:08 +03:00
|
|
|
|
|
|
|
from thinc.neural.optimizers import Adam
|
|
|
|
from thinc.neural.ops import NumpyOps, CupyOps
|
2017-05-18 12:36:53 +03:00
|
|
|
from thinc.neural.train import Trainer as ThincTrainer
|
2017-05-15 22:46:08 +03:00
|
|
|
|
2017-05-16 17:17:30 +03:00
|
|
|
from .syntax.nonproj import PseudoProjectivity
|
2017-04-15 13:05:47 +03:00
|
|
|
from .gold import GoldParse, merge_sents
|
2016-10-09 13:24:24 +03:00
|
|
|
from .scorer import Scorer
|
2017-05-16 12:21:59 +03:00
|
|
|
from .tokens.doc import Doc
|
2017-05-18 12:36:53 +03:00
|
|
|
from . import util
|
2016-10-09 13:24:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Trainer(object):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Manage training of an NLP pipeline.
|
|
|
|
"""
|
2017-05-18 12:36:53 +03:00
|
|
|
def __init__(self, nlp, gold_tuples, **cfg):
|
2016-10-09 13:24:24 +03:00
|
|
|
self.nlp = nlp
|
2017-03-11 20:12:48 +03:00
|
|
|
self.nr_epoch = 0
|
2017-05-16 12:21:59 +03:00
|
|
|
self.optimizer = Adam(NumpyOps(), 0.001)
|
2017-05-17 13:04:50 +03:00
|
|
|
self.gold_tuples = gold_tuples
|
2017-05-18 12:36:53 +03:00
|
|
|
self.cfg = cfg
|
|
|
|
self.batch_size = float(util.env_opt('min_batch_size', 4))
|
|
|
|
self.max_batch_size = util.env_opt('max_batch_size', 64)
|
|
|
|
self.accel_batch_size = util.env_opt('batch_accel', 1.001)
|
2016-10-09 13:24:24 +03:00
|
|
|
|
2016-10-13 04:24:53 +03:00
|
|
|
def epochs(self, nr_epoch, augment_data=None, gold_preproc=False):
|
2016-11-25 17:55:33 +03:00
|
|
|
cached_golds = {}
|
2017-05-18 12:36:53 +03:00
|
|
|
cached_docs = {}
|
2016-11-25 17:55:33 +03:00
|
|
|
def _epoch(indices):
|
2017-05-17 13:04:50 +03:00
|
|
|
all_docs = []
|
|
|
|
all_golds = []
|
|
|
|
for i in indices:
|
2016-11-25 17:55:33 +03:00
|
|
|
raw_text, paragraph_tuples = self.gold_tuples[i]
|
2016-10-13 04:24:53 +03:00
|
|
|
if gold_preproc:
|
|
|
|
raw_text = None
|
|
|
|
else:
|
|
|
|
paragraph_tuples = merge_sents(paragraph_tuples)
|
2016-11-25 17:55:33 +03:00
|
|
|
if augment_data is None:
|
2017-05-18 12:36:53 +03:00
|
|
|
if i not in cached_docs:
|
|
|
|
cached_docs[i] = self.make_docs(raw_text, paragraph_tuples)
|
|
|
|
docs = cached_docs[i]
|
|
|
|
if i not in cached_golds:
|
|
|
|
cached_golds[i] = self.make_golds(docs, paragraph_tuples)
|
|
|
|
golds = cached_golds[i]
|
2016-11-25 17:55:33 +03:00
|
|
|
else:
|
2016-10-09 13:24:24 +03:00
|
|
|
raw_text, paragraph_tuples = augment_data(raw_text, paragraph_tuples)
|
2016-11-25 17:55:33 +03:00
|
|
|
docs = self.make_docs(raw_text, paragraph_tuples)
|
|
|
|
golds = self.make_golds(docs, paragraph_tuples)
|
2017-05-17 13:04:50 +03:00
|
|
|
all_docs.extend(docs)
|
|
|
|
all_golds.extend(golds)
|
2017-05-18 12:36:53 +03:00
|
|
|
|
|
|
|
thinc_trainer = ThincTrainer(self.nlp.pipeline[0].model)
|
|
|
|
thinc_trainer.batch_size = int(self.batch_size)
|
|
|
|
thinc_trainer.nb_epoch = 1
|
|
|
|
for X, y in thinc_trainer.iterate(all_docs, all_golds):
|
2017-05-17 13:04:50 +03:00
|
|
|
yield X, y
|
2017-05-18 12:36:53 +03:00
|
|
|
thinc_trainer.batch_size = min(int(self.batch_size), self.max_batch_size)
|
|
|
|
self.batch_size *= self.accel_batch_size
|
2016-10-09 13:24:24 +03:00
|
|
|
|
2016-11-25 17:55:33 +03:00
|
|
|
indices = list(range(len(self.gold_tuples)))
|
2016-10-09 13:24:24 +03:00
|
|
|
for itn in range(nr_epoch):
|
2016-11-25 17:55:33 +03:00
|
|
|
random.shuffle(indices)
|
|
|
|
yield _epoch(indices)
|
2017-03-11 20:12:48 +03:00
|
|
|
self.nr_epoch += 1
|
|
|
|
|
2016-10-13 04:24:53 +03:00
|
|
|
def evaluate(self, dev_sents, gold_preproc=False):
|
2017-05-18 16:31:15 +03:00
|
|
|
all_docs = []
|
|
|
|
all_golds = []
|
2016-10-09 13:24:24 +03:00
|
|
|
for raw_text, paragraph_tuples in dev_sents:
|
2016-10-13 04:24:53 +03:00
|
|
|
if gold_preproc:
|
|
|
|
raw_text = None
|
|
|
|
else:
|
|
|
|
paragraph_tuples = merge_sents(paragraph_tuples)
|
2016-10-09 13:24:24 +03:00
|
|
|
docs = self.make_docs(raw_text, paragraph_tuples)
|
|
|
|
golds = self.make_golds(docs, paragraph_tuples)
|
2017-05-18 16:31:15 +03:00
|
|
|
all_docs.extend(docs)
|
|
|
|
all_golds.extend(golds)
|
|
|
|
scorer = Scorer()
|
|
|
|
for doc, gold in zip(self.nlp.pipe(all_docs), all_golds):
|
|
|
|
scorer.score(doc, gold)
|
2016-10-09 13:24:24 +03:00
|
|
|
return scorer
|
|
|
|
|
|
|
|
def make_docs(self, raw_text, paragraph_tuples):
|
|
|
|
if raw_text is not None:
|
2017-05-16 12:21:59 +03:00
|
|
|
return [self.nlp.make_doc(raw_text)]
|
2016-10-09 13:24:24 +03:00
|
|
|
else:
|
2017-05-18 12:36:53 +03:00
|
|
|
return [
|
|
|
|
Doc(self.nlp.vocab, words=sent_tuples[0][1])
|
|
|
|
for sent_tuples in paragraph_tuples]
|
2016-10-09 13:24:24 +03:00
|
|
|
|
|
|
|
def make_golds(self, docs, paragraph_tuples):
|
|
|
|
if len(docs) == 1:
|
2016-11-25 17:55:33 +03:00
|
|
|
return [GoldParse.from_annot_tuples(docs[0], sent_tuples[0])
|
2016-10-09 13:24:24 +03:00
|
|
|
for sent_tuples in paragraph_tuples]
|
|
|
|
else:
|
2016-11-25 17:55:33 +03:00
|
|
|
return [GoldParse.from_annot_tuples(doc, sent_tuples[0])
|
2016-10-09 13:24:24 +03:00
|
|
|
for doc, sent_tuples in zip(docs, paragraph_tuples)]
|