Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
fix behavior when num_serialized_models_to_keep is 0 (#2880)
Browse files Browse the repository at this point in the history
* fix behavior when num_serialized_models_to_keep is 0

* pylint
  • Loading branch information
joelgrus committed Jun 27, 2019
1 parent 2cce412 commit c85dcfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions allennlp/tests/training/checkpointer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def test_default(self):
models, training = self.retrieve_and_delete_saved()
assert models == training == target

def test_keep_zero(self):
checkpointer = Checkpointer(serialization_dir=self.TEST_DIR,
num_serialized_models_to_keep=0)
for e in range(10):
checkpointer.save_checkpoint(epoch=e,
model_state={"epoch": e},
training_states={"epoch": e},
is_best_so_far=True)
files = os.listdir(self.TEST_DIR)
assert 'model_state_epoch_1.th' not in files
assert 'training_state_epoch_1.th' not in files

def test_with_time(self):
"""
Tests that keep_serialized_model_every_num_seconds parameter causes a checkpoint to be saved
Expand Down
2 changes: 1 addition & 1 deletion allennlp/training/checkpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def save_checkpoint(self,
"Copying weights to '%s/best.th'.", self._serialization_dir)
shutil.copyfile(model_path, os.path.join(self._serialization_dir, "best.th"))

if self._num_serialized_models_to_keep and self._num_serialized_models_to_keep >= 0:
if self._num_serialized_models_to_keep is not None and self._num_serialized_models_to_keep >= 0:
self._serialized_paths.append((time.time(), model_path, training_path))
if len(self._serialized_paths) > self._num_serialized_models_to_keep:
paths_to_remove = self._serialized_paths.pop(0)
Expand Down

0 comments on commit c85dcfc

Please sign in to comment.