Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: added early stoping feature #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions best_checkpoint_copier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import tensorflow as tf


class Checkpoint(object):
dir = None
file = None
Expand All @@ -25,24 +26,36 @@ class BestCheckpointCopier(tf.estimator.Exporter):
sort_key_fn = None
sort_reverse = None

def __init__(self, name='best_checkpoints', checkpoints_to_keep=5, score_metric='Loss/total_loss', compare_fn=lambda x,y: x.score < y.score, sort_key_fn=lambda x: x.score, sort_reverse=False):
def __init__(self, name='best_checkpoints',
checkpoints_to_keep=5,
score_metric='Loss/total_loss',
compare_fn=lambda x,y: x.score < y.score,
sort_key_fn=lambda x: x.score,
sort_reverse=False,
patience=25):
self.checkpoints = []
self.checkpoints_to_keep = checkpoints_to_keep
self.compare_fn = compare_fn
self.name = name
self.score_metric = score_metric
self.sort_key_fn = sort_key_fn
self.sort_reverse = sort_reverse
self.early_stop_patient = 0
self.patience = patience
super(BestCheckpointCopier, self).__init__()

def _copyCheckpoint(self, checkpoint):
desination_dir = self._destinationDir(checkpoint)
os.makedirs(desination_dir, exist_ok=True)

for file in glob.glob(r'{}*'.format(checkpoint.path)):
if not self.name in desination_dir: desination_dir = os.path.join(desination_dir, self.name)
self._log('copying {} to {}'.format(file, desination_dir))
shutil.copy(file, desination_dir)

with open(desination_dir+"/meta.txt", "a") as f:
f.write(f"{file.split('/')[-1]} {round(checkpoint.score, 5)}\n")

def _destinationDir(self, checkpoint):
return os.path.join(checkpoint.dir, self.name)

Expand All @@ -53,6 +66,7 @@ def _keepCheckpoint(self, checkpoint):
self.checkpoints = sorted(self.checkpoints, key=self.sort_key_fn, reverse=self.sort_reverse)

self._copyCheckpoint(checkpoint)
self.early_stop_patient = 0

def _log(self, statement):
tf.logging.info('[{}] {}'.format(self.__class__.__name__, statement))
Expand Down Expand Up @@ -83,7 +97,10 @@ def export(self, estimator, export_path, checkpoint_path, eval_result, is_the_fi
checkpoint = Checkpoint(path=checkpoint_path, score=score)

if self._shouldKeep(checkpoint):
self._keepCheckpoint(checkpoint)
self._pruneCheckpoints(checkpoint)
self._keepCheckpoint(checkpoint)
else:
self._log('skipping checkpoint {}'.format(checkpoint.path))
self.early_stop_patient += 1
if self.early_stop_patient > self.patience:
raise ValueError (f"Stopping training. [email protected] didn't improve in last {self.patience} epochs.")