From 97bcf2ae3a03cf97fd473062c1793e3d18ef2820 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Sat, 6 Mar 2021 08:42:14 +0100 Subject: [PATCH] Fix patience for identical scores (#7250) * Fix patience for identical scores Fix training patience so that the earliest best step is chosen for identical max scores. * Restore break, remove print * Explicitly define best_step for clarity --- spacy/training/loop.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spacy/training/loop.py b/spacy/training/loop.py index dacd2dba4..55919014b 100644 --- a/spacy/training/loop.py +++ b/spacy/training/loop.py @@ -230,7 +230,10 @@ def train_while_improving( if is_best_checkpoint is not None: losses = {} # Stop if no improvement in `patience` updates (if specified) - best_score, best_step = max(results) + # Negate step value so that the earliest best step is chosen for the + # same score, i.e. (1.0, 100) is chosen over (1.0, 200) + best_result = max((r_score, -r_step) for r_score, r_step in results) + best_step = -best_result[1] if patience and (step - best_step) >= patience: break # Stop if we've exhausted our max steps (if specified)