Auto-format

This commit is contained in:
Ines Montani 2019-04-22 14:31:11 +02:00
parent 1d567913f9
commit 9767427669
1 changed files with 20 additions and 12 deletions

View File

@ -226,7 +226,7 @@ def train(
msg.row(["-" * width for width in row_settings["widths"]], **row_settings) msg.row(["-" * width for width in row_settings["widths"]], **row_settings)
try: try:
iter_since_best = 0 iter_since_best = 0
best_score = 0. best_score = 0.0
for i in range(n_iter): for i in range(n_iter):
train_docs = corpus.train_docs( train_docs = corpus.train_docs(
nlp, noise_level=noise_level, gold_preproc=gold_preproc, max_length=0 nlp, noise_level=noise_level, gold_preproc=gold_preproc, max_length=0
@ -335,8 +335,8 @@ def train(
gpu_wps=gpu_wps, gpu_wps=gpu_wps,
) )
msg.row(progress, **row_settings) msg.row(progress, **row_settings)
# early stopping
if early_stopping_iter is not None: if early_stopping_iter is not None:
# Early stopping
current_score = _score_for_model(meta) current_score = _score_for_model(meta)
if current_score < best_score: if current_score < best_score:
iter_since_best += 1 iter_since_best += 1
@ -344,8 +344,14 @@ def train(
iter_since_best = 0 iter_since_best = 0
best_score = current_score best_score = current_score
if iter_since_best >= early_stopping_iter: if iter_since_best >= early_stopping_iter:
msg.text("Early stopping, best iteration is: {}".format(i-iter_since_best)) msg.text(
msg.text("Best score = {}; Final iteration score = {}".format(best_score, current_score)) "Early stopping, best iteration "
"is: {}".format(i - iter_since_best)
)
msg.text(
"Best score = {}; Final iteration "
"score = {}".format(best_score, current_score)
)
break break
finally: finally:
with nlp.use_params(optimizer.averages): with nlp.use_params(optimizer.averages):
@ -356,19 +362,21 @@ def train(
best_model_path = _collate_best_model(meta, output_path, nlp.pipe_names) best_model_path = _collate_best_model(meta, output_path, nlp.pipe_names)
msg.good("Created best model", best_model_path) msg.good("Created best model", best_model_path)
def _score_for_model(meta): def _score_for_model(meta):
""" Returns mean score between tasks in pipeline that can be used for early stopping. """ """ Returns mean score between tasks in pipeline that can be used for early stopping. """
mean_acc = list() mean_acc = list()
pipes = meta['pipeline'] pipes = meta["pipeline"]
acc = meta['accuracy'] acc = meta["accuracy"]
if 'tagger' in pipes: if "tagger" in pipes:
mean_acc.append(acc['tags_acc']) mean_acc.append(acc["tags_acc"])
if 'parser' in pipes: if "parser" in pipes:
mean_acc.append((acc['uas']+acc['las']) / 2) mean_acc.append((acc["uas"] + acc["las"]) / 2)
if 'ner' in pipes: if "ner" in pipes:
mean_acc.append((acc['ents_p']+acc['ents_r']+acc['ents_f']) / 3) mean_acc.append((acc["ents_p"] + acc["ents_r"] + acc["ents_f"]) / 3)
return sum(mean_acc) / len(mean_acc) return sum(mean_acc) / len(mean_acc)
@contextlib.contextmanager @contextlib.contextmanager
def _create_progress_bar(total): def _create_progress_bar(total):
if int(os.environ.get("LOG_FRIENDLY", 0)): if int(os.environ.get("LOG_FRIENDLY", 0)):