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

Adding a warmup period to EarlyStopping and ModelCheckpoint #2644

Closed
rgmyr opened this issue Jul 18, 2020 · 6 comments
Closed

Adding a warmup period to EarlyStopping and ModelCheckpoint #2644

rgmyr opened this issue Jul 18, 2020 · 6 comments
Labels
feature Is an improvement or enhancement help wanted Open to be worked on won't fix This will not be worked on

Comments

@rgmyr
Copy link

rgmyr commented Jul 18, 2020

🚀 Feature

Add an optional warmup period for EarlyStopping and ModelCheckpoint callbacks.

Motivation

Sometimes the metric you want to monitor can take a number of epochs to stabilize and become meaningful.

For example: with GANs, you might want to monitor and minimize G's loss, but usually it starts out unreasonably low because it's based on the output of D, which hasn't learned anything about discriminating yet.

Pitch

I'd like to have this result in the callbacks having no effect for the first 10 epochs:

early_stop = EarlyStopping(..., warmup=10)
model_checkpoint = ModelCheckpoint(..., warmup=10)

Alternatives

I added this option through inheritance:

from pytorch_lightning.callbacks import EarlyStopping

class EarlyStoppingWithWarmup(EarlyStopping):
    """
    EarlyStopping, except don't watch the first `warmup` epochs.
    """
    def __init__(self, warmup=10, **kwargs):
        super().__init__(**kwargs)
        self.warmup = warmup

    def on_validation_end(self, trainer, pl_module):
        pass

    def on_epoch_end(self, trainer, pl_module):
        if trainer.current_epoch < self.warmup:
            return
        else:
            super()._run_early_stopping_check(trainer, pl_module)

Additional context

Edit: couldn't get this working at first, but got it figured out after upgrading my pl version. I would be happy to PR something like this if anyone can provide guidance on where to add it.

@rgmyr rgmyr added feature Is an improvement or enhancement help wanted Open to be worked on labels Jul 18, 2020
@rohitgr7
Copy link
Contributor

Your code is working for me. Also make it trainer.current_epoch < self.warmup: since current_epoch starts from 0.

@rgmyr
Copy link
Author

rgmyr commented Jul 18, 2020

@rohitgr7 Thanks, was just editing my post as you said that. I've got it working now.

@s-rog
Copy link
Contributor

s-rog commented Jul 20, 2020

for early stopping you can set min_epochs in trainer
https://pytorch-lightning.readthedocs.io/en/stable/trainer.html#min-epochs

for checkpoints, if you set save_top_k in ModelCheckpoint then the initial "bad" checkpoints will be overwritten

@rohitgr7
Copy link
Contributor

yeah min_epochs will do the trick here but with val_check_interval != 1.0 it might not. Let's say I have a very big dataset and want to check with val_check_interval=0.5 with a warmup=7, then min_epochs won't work here. I think warmup should be specific to number of times early_stopping is called.

@stale
Copy link

stale bot commented Sep 18, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the won't fix This will not be worked on label Sep 18, 2020
@stale stale bot closed this as completed Sep 25, 2020
@thesofakillers
Copy link

@rohitgr7 I know this is 3 years late, but you can use min_steps instead of min_epochs in that case probably

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement help wanted Open to be worked on won't fix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants