Skip to content

Commit

Permalink
ppdate tests for early stop (Lightning-AI#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshSharma12 committed Jan 22, 2020
1 parent 876775c commit f3f7f85
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tests/test_cpu_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_early_stopping_cpu_model(tmpdir):
stopping = EarlyStopping(monitor='val_loss', min_delta=0.1)
trainer_options = dict(
default_save_path=tmpdir,
min_epochs=2,
early_stop_callback=stopping,
gradient_clip_val=1.0,
overfit_pct=0.20,
Expand All @@ -33,7 +34,7 @@ def test_early_stopping_cpu_model(tmpdir):
)

model, hparams = tutils.get_model()
tutils.run_model_test(trainer_options, model, on_gpu=False)
tutils.run_model_test(trainer_options, model, on_gpu=False, early_stop=True)

# test freeze on cpu
model.freeze()
Expand Down
6 changes: 5 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_model_test_no_loggers(trainer_options, model, min_acc=0.50):
trainer.optimizers, trainer.lr_schedulers = pretrained_model.configure_optimizers()


def run_model_test(trainer_options, model, on_gpu=True):
def run_model_test(trainer_options, model, on_gpu=True, early_stop=False):
save_dir = trainer_options['default_save_path']

# logger file to get meta
Expand All @@ -65,6 +65,10 @@ def run_model_test(trainer_options, model, on_gpu=True):
# correct result and ok accuracy
assert result == 1, 'amp + ddp model failed to complete'

if (early_stop):
assert trainer.current_epoch >= trainer.min_epochs, 'amp + ddp model failed to complete'
assert trainer.current_epoch < trainer.max_epochs-1, 'amp + ddp model failed to stop early'

# test model loading
pretrained_model = load_model(logger, trainer.checkpoint_callback.filepath)

Expand Down

0 comments on commit f3f7f85

Please sign in to comment.