2017-03-23 10:08:41 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals, division, print_function
|
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
import plac
|
2017-05-17 10:04:50 +00:00
|
|
|
from pathlib import Path
|
2017-05-21 14:07:06 +00:00
|
|
|
import tqdm
|
2017-09-21 00:17:10 +00:00
|
|
|
from thinc.neural._classes.model import Model
|
2017-05-23 08:06:53 +00:00
|
|
|
from timeit import default_timer as timer
|
2017-03-23 10:08:41 +00:00
|
|
|
|
2018-02-17 17:11:11 +00:00
|
|
|
from ..attrs import PROB, IS_OOV, CLUSTER, LANG
|
2017-05-25 21:16:30 +00:00
|
|
|
from ..gold import GoldCorpus, minibatch
|
2017-05-07 21:25:29 +00:00
|
|
|
from ..util import prints
|
2017-03-23 10:08:41 +00:00
|
|
|
from .. import util
|
2017-09-25 17:00:47 +00:00
|
|
|
from .. import about
|
2017-05-18 09:36:53 +00:00
|
|
|
from .. import displacy
|
2017-06-05 01:18:37 +00:00
|
|
|
from ..compat import json_dumps
|
2017-03-23 10:08:41 +00:00
|
|
|
|
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
@plac.annotations(
|
|
|
|
lang=("model language", "positional", None, str),
|
|
|
|
output_dir=("output directory to store model in", "positional", None, str),
|
2017-10-27 12:38:39 +00:00
|
|
|
train_data=("location of JSON-formatted training data", "positional",
|
|
|
|
None, str),
|
|
|
|
dev_data=("location of JSON-formatted development data (optional)",
|
|
|
|
"positional", None, str),
|
2017-05-22 10:28:58 +00:00
|
|
|
n_iter=("number of iterations", "option", "n", int),
|
|
|
|
n_sents=("number of sentences", "option", "ns", int),
|
2017-06-03 21:10:23 +00:00
|
|
|
use_gpu=("Use GPU", "option", "g", int),
|
2017-09-23 01:00:40 +00:00
|
|
|
vectors=("Model to load vectors from", "option", "v"),
|
2017-05-22 10:28:58 +00:00
|
|
|
no_tagger=("Don't train tagger", "flag", "T", bool),
|
|
|
|
no_parser=("Don't train parser", "flag", "P", bool),
|
2017-08-20 16:07:00 +00:00
|
|
|
no_entities=("Don't train NER", "flag", "N", bool),
|
2018-02-18 09:59:11 +00:00
|
|
|
parser_multitasks=("Side objectives for parser CNN, e.g. dep dep,tag", "option", "pt", str),
|
|
|
|
entity_multitasks=("Side objectives for ner CNN, e.g. dep dep,tag", "option", "et", str),
|
2017-08-20 16:07:00 +00:00
|
|
|
gold_preproc=("Use gold preprocessing", "flag", "G", bool),
|
2017-09-26 15:59:34 +00:00
|
|
|
version=("Model version", "option", "V", str),
|
2017-10-27 12:38:39 +00:00
|
|
|
meta_path=("Optional path to meta.json. All relevant properties will be "
|
|
|
|
"overwritten.", "option", "m", Path))
|
2018-01-04 20:33:47 +00:00
|
|
|
def train(lang, output_dir, train_data, dev_data, n_iter=30, n_sents=0,
|
2018-02-17 21:03:54 +00:00
|
|
|
parser_multitasks='', entity_multitasks='',
|
2017-10-31 18:21:05 +00:00
|
|
|
use_gpu=-1, vectors=None, no_tagger=False,
|
2017-10-30 17:00:10 +00:00
|
|
|
no_parser=False, no_entities=False, gold_preproc=False,
|
|
|
|
version="0.0.0", meta_path=None):
|
2017-05-27 18:01:46 +00:00
|
|
|
"""
|
|
|
|
Train a model. Expects data in spaCy's JSON format.
|
|
|
|
"""
|
2018-02-13 11:42:23 +00:00
|
|
|
util.fix_random_seed()
|
2017-06-03 18:28:20 +00:00
|
|
|
util.set_env_log(True)
|
2017-05-22 10:28:58 +00:00
|
|
|
n_sents = n_sents or None
|
2017-05-07 21:25:29 +00:00
|
|
|
output_path = util.ensure_path(output_dir)
|
|
|
|
train_path = util.ensure_path(train_data)
|
|
|
|
dev_path = util.ensure_path(dev_data)
|
2017-09-25 17:00:47 +00:00
|
|
|
meta_path = util.ensure_path(meta_path)
|
2017-05-07 21:25:29 +00:00
|
|
|
if not output_path.exists():
|
2017-06-05 01:18:37 +00:00
|
|
|
output_path.mkdir()
|
2017-05-07 21:25:29 +00:00
|
|
|
if not train_path.exists():
|
2017-05-22 10:28:58 +00:00
|
|
|
prints(train_path, title="Training data not found", exits=1)
|
2017-05-07 21:25:29 +00:00
|
|
|
if dev_path and not dev_path.exists():
|
2017-05-22 10:28:58 +00:00
|
|
|
prints(dev_path, title="Development data not found", exits=1)
|
2017-09-25 17:00:47 +00:00
|
|
|
if meta_path is not None and not meta_path.exists():
|
|
|
|
prints(meta_path, title="meta.json not found", exits=1)
|
|
|
|
meta = util.read_json(meta_path) if meta_path else {}
|
|
|
|
if not isinstance(meta, dict):
|
|
|
|
prints("Expected dict but got: {}".format(type(meta)),
|
|
|
|
title="Not a valid meta.json format", exits=1)
|
2017-10-11 06:49:12 +00:00
|
|
|
meta.setdefault('lang', lang)
|
|
|
|
meta.setdefault('name', 'unnamed')
|
2017-03-23 10:08:41 +00:00
|
|
|
|
2017-10-02 15:20:19 +00:00
|
|
|
pipeline = ['tagger', 'parser', 'ner']
|
2017-10-27 12:38:39 +00:00
|
|
|
if no_tagger and 'tagger' in pipeline:
|
|
|
|
pipeline.remove('tagger')
|
|
|
|
if no_parser and 'parser' in pipeline:
|
|
|
|
pipeline.remove('parser')
|
|
|
|
if no_entities and 'ner' in pipeline:
|
|
|
|
pipeline.remove('ner')
|
2017-03-23 10:08:41 +00:00
|
|
|
|
2017-05-25 21:16:30 +00:00
|
|
|
# Take dropout and batch size as generators of values -- dropout
|
|
|
|
# starts high and decays sharply, to force the optimizer to explore.
|
|
|
|
# Batch size starts at 1 and grows, so that we make updates quickly
|
|
|
|
# at the beginning of training.
|
2017-10-08 07:08:12 +00:00
|
|
|
dropout_rates = util.decaying(util.env_opt('dropout_from', 0.2),
|
|
|
|
util.env_opt('dropout_to', 0.2),
|
|
|
|
util.env_opt('dropout_decay', 0.0))
|
2017-05-25 21:16:30 +00:00
|
|
|
batch_sizes = util.compounding(util.env_opt('batch_from', 1),
|
2017-10-08 07:08:12 +00:00
|
|
|
util.env_opt('batch_to', 16),
|
2017-05-25 21:16:30 +00:00
|
|
|
util.env_opt('batch_compound', 1.001))
|
2017-11-03 00:54:54 +00:00
|
|
|
max_doc_len = util.env_opt('max_doc_len', 5000)
|
2017-05-22 15:41:39 +00:00
|
|
|
corpus = GoldCorpus(train_path, dev_path, limit=n_sents)
|
2017-06-05 01:18:37 +00:00
|
|
|
n_train_words = corpus.count_train()
|
2017-03-23 10:08:41 +00:00
|
|
|
|
2017-09-23 01:00:40 +00:00
|
|
|
lang_class = util.get_lang_class(lang)
|
2017-10-10 04:41:16 +00:00
|
|
|
nlp = lang_class()
|
2017-10-11 06:55:52 +00:00
|
|
|
meta['pipeline'] = pipeline
|
|
|
|
nlp.meta.update(meta)
|
2017-09-23 01:00:40 +00:00
|
|
|
if vectors:
|
2018-03-28 14:33:43 +00:00
|
|
|
print("Load vectors model", vectors)
|
2017-09-23 01:00:40 +00:00
|
|
|
util.load_model(vectors, vocab=nlp.vocab)
|
2018-02-17 17:11:11 +00:00
|
|
|
for lex in nlp.vocab:
|
|
|
|
values = {}
|
|
|
|
for attr, func in nlp.vocab.lex_attr_getters.items():
|
|
|
|
# These attrs are expected to be set by data. Others should
|
|
|
|
# be set by calling the language functions.
|
|
|
|
if attr not in (CLUSTER, PROB, IS_OOV, LANG):
|
|
|
|
values[lex.vocab.strings[attr]] = func(lex.orth_)
|
|
|
|
lex.set_attrs(**values)
|
|
|
|
lex.is_oov = False
|
2017-10-10 04:41:16 +00:00
|
|
|
for name in pipeline:
|
|
|
|
nlp.add_pipe(nlp.create_pipe(name), name=name)
|
2018-02-18 09:59:11 +00:00
|
|
|
if parser_multitasks:
|
|
|
|
for objective in parser_multitasks.split(','):
|
|
|
|
nlp.parser.add_multitask_objective(objective)
|
|
|
|
if entity_multitasks:
|
|
|
|
for objective in entity_multitasks.split(','):
|
|
|
|
nlp.entity.add_multitask_objective(objective)
|
2017-06-03 21:10:23 +00:00
|
|
|
optimizer = nlp.begin_training(lambda: corpus.train_tuples, device=use_gpu)
|
2017-09-02 17:46:01 +00:00
|
|
|
nlp._optimizer = None
|
2017-05-25 21:16:30 +00:00
|
|
|
|
2017-09-28 13:05:31 +00:00
|
|
|
print("Itn.\tP.Loss\tN.Loss\tUAS\tNER P.\tNER R.\tNER F.\tTag %\tToken %")
|
2017-05-26 10:52:09 +00:00
|
|
|
try:
|
2017-09-23 01:00:40 +00:00
|
|
|
train_docs = corpus.train_docs(nlp, projectivize=True, noise_level=0.0,
|
|
|
|
gold_preproc=gold_preproc, max_length=0)
|
|
|
|
train_docs = list(train_docs)
|
2017-05-26 10:52:09 +00:00
|
|
|
for i in range(n_iter):
|
2017-06-05 01:18:37 +00:00
|
|
|
with tqdm.tqdm(total=n_train_words, leave=False) as pbar:
|
2017-05-26 10:52:09 +00:00
|
|
|
losses = {}
|
|
|
|
for batch in minibatch(train_docs, size=batch_sizes):
|
2017-11-03 00:54:54 +00:00
|
|
|
batch = [(d, g) for (d, g) in batch if len(d) < max_doc_len]
|
|
|
|
if not batch:
|
|
|
|
continue
|
2017-05-26 10:52:09 +00:00
|
|
|
docs, golds = zip(*batch)
|
|
|
|
nlp.update(docs, golds, sgd=optimizer,
|
2017-09-24 10:00:37 +00:00
|
|
|
drop=next(dropout_rates), losses=losses)
|
2017-06-05 01:18:37 +00:00
|
|
|
pbar.update(sum(len(doc) for doc in docs))
|
2017-05-25 21:16:30 +00:00
|
|
|
|
2017-05-26 10:52:09 +00:00
|
|
|
with nlp.use_params(optimizer.averages):
|
2017-06-03 18:28:20 +00:00
|
|
|
util.set_env_log(False)
|
|
|
|
epoch_model_path = output_path / ('model%d' % i)
|
|
|
|
nlp.to_disk(epoch_model_path)
|
2017-10-10 17:51:20 +00:00
|
|
|
nlp_loaded = util.load_model_from_path(epoch_model_path)
|
2017-10-09 13:05:37 +00:00
|
|
|
dev_docs = list(corpus.dev_docs(
|
2017-06-03 18:28:20 +00:00
|
|
|
nlp_loaded,
|
2017-10-09 13:05:37 +00:00
|
|
|
gold_preproc=gold_preproc))
|
|
|
|
nwords = sum(len(doc_gold[0]) for doc_gold in dev_docs)
|
|
|
|
start_time = timer()
|
|
|
|
scorer = nlp_loaded.evaluate(dev_docs)
|
|
|
|
end_time = timer()
|
|
|
|
if use_gpu < 0:
|
|
|
|
gpu_wps = None
|
|
|
|
cpu_wps = nwords/(end_time-start_time)
|
|
|
|
else:
|
|
|
|
gpu_wps = nwords/(end_time-start_time)
|
|
|
|
with Model.use_device('cpu'):
|
2017-10-10 17:51:20 +00:00
|
|
|
nlp_loaded = util.load_model_from_path(epoch_model_path)
|
2017-10-09 13:05:37 +00:00
|
|
|
dev_docs = list(corpus.dev_docs(
|
|
|
|
nlp_loaded, gold_preproc=gold_preproc))
|
|
|
|
start_time = timer()
|
|
|
|
scorer = nlp_loaded.evaluate(dev_docs)
|
|
|
|
end_time = timer()
|
|
|
|
cpu_wps = nwords/(end_time-start_time)
|
2017-10-27 12:38:39 +00:00
|
|
|
acc_loc = (output_path / ('model%d' % i) / 'accuracy.json')
|
2017-06-05 01:18:37 +00:00
|
|
|
with acc_loc.open('w') as file_:
|
|
|
|
file_.write(json_dumps(scorer.scores))
|
2017-09-25 17:00:47 +00:00
|
|
|
meta_loc = output_path / ('model%d' % i) / 'meta.json'
|
|
|
|
meta['accuracy'] = scorer.scores
|
2017-10-25 14:03:05 +00:00
|
|
|
meta['speed'] = {'nwords': nwords, 'cpu': cpu_wps,
|
|
|
|
'gpu': gpu_wps}
|
2017-10-25 14:08:26 +00:00
|
|
|
meta['vectors'] = {'width': nlp.vocab.vectors_length,
|
2017-11-01 00:25:09 +00:00
|
|
|
'vectors': len(nlp.vocab.vectors),
|
|
|
|
'keys': nlp.vocab.vectors.n_keys}
|
2017-09-25 17:00:47 +00:00
|
|
|
meta['lang'] = nlp.lang
|
|
|
|
meta['pipeline'] = pipeline
|
|
|
|
meta['spacy_version'] = '>=%s' % about.__version__
|
|
|
|
meta.setdefault('name', 'model%d' % i)
|
2017-09-26 15:34:52 +00:00
|
|
|
meta.setdefault('version', version)
|
2017-09-25 17:00:47 +00:00
|
|
|
|
|
|
|
with meta_loc.open('w') as file_:
|
|
|
|
file_.write(json_dumps(meta))
|
2017-06-03 18:28:20 +00:00
|
|
|
util.set_env_log(True)
|
2017-10-27 12:38:39 +00:00
|
|
|
print_progress(i, losses, scorer.scores, cpu_wps=cpu_wps,
|
|
|
|
gpu_wps=gpu_wps)
|
2017-05-26 10:52:09 +00:00
|
|
|
finally:
|
|
|
|
print("Saving model...")
|
2017-12-10 20:29:50 +00:00
|
|
|
with nlp.use_params(optimizer.averages):
|
|
|
|
final_model_path = output_path / 'model-final'
|
|
|
|
nlp.to_disk(final_model_path)
|
2017-05-19 23:15:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _render_parses(i, to_render):
|
|
|
|
to_render[0].user_data['title'] = "Batch %d" % i
|
|
|
|
with Path('/tmp/entities.html').open('w') as file_:
|
|
|
|
html = displacy.render(to_render[:5], style='ent', page=True)
|
|
|
|
file_.write(html)
|
|
|
|
with Path('/tmp/parses.html').open('w') as file_:
|
|
|
|
html = displacy.render(to_render[:5], style='dep', page=True)
|
|
|
|
file_.write(html)
|
2017-03-23 10:08:41 +00:00
|
|
|
|
|
|
|
|
2017-10-09 13:05:37 +00:00
|
|
|
def print_progress(itn, losses, dev_scores, cpu_wps=0.0, gpu_wps=0.0):
|
2017-05-16 14:17:30 +00:00
|
|
|
scores = {}
|
2017-05-22 15:41:39 +00:00
|
|
|
for col in ['dep_loss', 'tag_loss', 'uas', 'tags_acc', 'token_acc',
|
2017-10-09 13:05:37 +00:00
|
|
|
'ents_p', 'ents_r', 'ents_f', 'cpu_wps', 'gpu_wps']:
|
2017-05-16 14:17:30 +00:00
|
|
|
scores[col] = 0.0
|
2017-05-25 01:10:20 +00:00
|
|
|
scores['dep_loss'] = losses.get('parser', 0.0)
|
2017-09-28 13:05:31 +00:00
|
|
|
scores['ner_loss'] = losses.get('ner', 0.0)
|
2017-05-25 01:10:20 +00:00
|
|
|
scores['tag_loss'] = losses.get('tagger', 0.0)
|
2017-05-16 14:17:30 +00:00
|
|
|
scores.update(dev_scores)
|
2017-10-09 13:05:37 +00:00
|
|
|
scores['cpu_wps'] = cpu_wps
|
|
|
|
scores['gpu_wps'] = gpu_wps or 0.0
|
2017-05-22 15:41:39 +00:00
|
|
|
tpl = '\t'.join((
|
|
|
|
'{:d}',
|
|
|
|
'{dep_loss:.3f}',
|
2017-09-28 13:05:31 +00:00
|
|
|
'{ner_loss:.3f}',
|
2017-05-22 15:41:39 +00:00
|
|
|
'{uas:.3f}',
|
|
|
|
'{ents_p:.3f}',
|
|
|
|
'{ents_r:.3f}',
|
|
|
|
'{ents_f:.3f}',
|
|
|
|
'{tags_acc:.3f}',
|
2017-05-23 08:06:53 +00:00
|
|
|
'{token_acc:.3f}',
|
2017-10-09 13:05:37 +00:00
|
|
|
'{cpu_wps:.1f}',
|
|
|
|
'{gpu_wps:.1f}',
|
|
|
|
))
|
2017-05-16 14:17:30 +00:00
|
|
|
print(tpl.format(itn, **scores))
|
2017-03-23 10:08:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_results(scorer):
|
2017-03-26 12:16:52 +00:00
|
|
|
results = {
|
|
|
|
'TOK': '%.2f' % scorer.token_acc,
|
|
|
|
'POS': '%.2f' % scorer.tags_acc,
|
|
|
|
'UAS': '%.2f' % scorer.uas,
|
|
|
|
'LAS': '%.2f' % scorer.las,
|
|
|
|
'NER P': '%.2f' % scorer.ents_p,
|
|
|
|
'NER R': '%.2f' % scorer.ents_r,
|
|
|
|
'NER F': '%.2f' % scorer.ents_f}
|
2017-03-23 10:08:41 +00:00
|
|
|
util.print_table(results, title="Results")
|