spaCy/spacy/cli/train.py

162 lines
6.4 KiB
Python
Raw Normal View History

2017-03-23 10:08:41 +00:00
# coding: utf8
from __future__ import unicode_literals, division, print_function
import plac
2017-03-23 10:08:41 +00:00
import json
from collections import defaultdict
import cytoolz
from pathlib import Path
import dill
2017-05-21 14:07:06 +00:00
import tqdm
from thinc.neural.optimizers import linear_decay
from timeit import default_timer as timer
2017-03-23 10:08:41 +00:00
from ..tokens.doc import Doc
2017-03-23 10:08:41 +00:00
from ..scorer import Scorer
from ..gold import GoldParse, merge_sents
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
from .. import displacy
2017-06-05 01:18:37 +00:00
from ..compat import json_dumps
2017-03-23 10:08:41 +00:00
@plac.annotations(
lang=("model language", "positional", None, str),
output_dir=("output directory to store model in", "positional", None, str),
train_data=("location of JSON-formatted training data", "positional", None, str),
dev_data=("location of JSON-formatted development data (optional)", "positional", None, str),
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-06-03 18:28:20 +00:00
resume=("Whether to resume training", "flag", "R", bool),
no_tagger=("Don't train tagger", "flag", "T", bool),
no_parser=("Don't train parser", "flag", "P", bool),
no_entities=("Don't train NER", "flag", "N", bool)
)
def train(cmd, lang, output_dir, train_data, dev_data, n_iter=20, n_sents=0,
2017-06-03 21:10:23 +00:00
use_gpu=-1, resume=False, no_tagger=False, no_parser=False, no_entities=False):
"""
Train a model. Expects data in spaCy's JSON format.
"""
2017-06-03 18:28:20 +00:00
util.set_env_log(True)
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)
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():
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():
prints(dev_path, title="Development data not found", exits=1)
2017-03-23 10:08:41 +00:00
lang_class = util.get_lang_class(lang)
2017-03-23 10:08:41 +00:00
2017-05-21 14:07:06 +00:00
pipeline = ['token_vectors', 'tags', 'dependencies', 'entities']
if no_tagger and 'tags' in pipeline: pipeline.remove('tags')
if no_parser and 'dependencies' in pipeline: pipeline.remove('dependencies')
if no_entities and 'entities' in pipeline: pipeline.remove('entities')
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-05-26 10:52:09 +00:00
dropout_rates = util.decaying(util.env_opt('dropout_from', 0.2),
2017-05-25 22:15:24 +00:00
util.env_opt('dropout_to', 0.2),
2017-05-26 10:52:09 +00:00
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),
util.env_opt('batch_to', 64),
util.env_opt('batch_compound', 1.001))
2017-06-03 18:28:20 +00:00
if resume:
prints(output_path / 'model19.pickle', title="Resuming training")
nlp = dill.load((output_path / 'model19.pickle').open('rb'))
else:
nlp = lang_class(pipeline=pipeline)
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-06-03 21:10:23 +00:00
optimizer = nlp.begin_training(lambda: corpus.train_tuples, device=use_gpu)
2017-05-25 21:16:30 +00:00
2017-05-27 13:20:32 +00:00
print("Itn.\tLoss\tUAS\tNER P.\tNER R.\tNER F.\tTag %\tToken %")
2017-05-26 10:52:09 +00:00
try:
for i in range(n_iter):
2017-06-03 18:28:20 +00:00
if resume:
i += 20
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
train_docs = corpus.train_docs(nlp, projectivize=True,
2017-05-27 13:20:32 +00:00
gold_preproc=False, max_length=0)
2017-05-26 10:52:09 +00:00
losses = {}
for batch in minibatch(train_docs, size=batch_sizes):
docs, golds = zip(*batch)
nlp.update(docs, golds, sgd=optimizer,
drop=next(dropout_rates), losses=losses,
update_tensors=True)
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)
nlp_loaded = lang_class(pipeline=pipeline)
nlp_loaded = nlp_loaded.from_disk(epoch_model_path)
scorer = nlp_loaded.evaluate(
corpus.dev_docs(
nlp_loaded,
gold_preproc=False))
2017-06-05 01:18:37 +00:00
acc_loc =(output_path / ('model%d' % i) / 'accuracy.json')
with acc_loc.open('w') as file_:
file_.write(json_dumps(scorer.scores))
2017-06-03 18:28:20 +00:00
util.set_env_log(True)
2017-05-26 10:52:09 +00:00
print_progress(i, losses, scorer.scores)
finally:
print("Saving model...")
with (output_path / 'model-final.pickle').open('wb') as file_:
with nlp.use_params(optimizer.averages):
dill.dump(nlp, file_, -1)
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
def print_progress(itn, losses, dev_scores, wps=0.0):
scores = {}
2017-05-22 15:41:39 +00:00
for col in ['dep_loss', 'tag_loss', 'uas', 'tags_acc', 'token_acc',
'ents_p', 'ents_r', 'ents_f', 'wps']:
scores[col] = 0.0
2017-05-25 01:10:20 +00:00
scores['dep_loss'] = losses.get('parser', 0.0)
scores['tag_loss'] = losses.get('tagger', 0.0)
scores.update(dev_scores)
2017-05-25 01:10:20 +00:00
scores['wps'] = wps
2017-05-22 15:41:39 +00:00
tpl = '\t'.join((
'{:d}',
'{dep_loss:.3f}',
'{uas:.3f}',
'{ents_p:.3f}',
'{ents_r:.3f}',
'{ents_f:.3f}',
'{tags_acc:.3f}',
'{token_acc:.3f}',
'{wps:.1f}'))
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")