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

Log LR using LearningRateMonitor even when LR Scheduler is not defined. #9786

Conversation

VirajBagal
Copy link
Contributor

@VirajBagal VirajBagal commented Oct 1, 2021

What does this PR do?

In this PR, the LearningRateMonitor callback is extended to log learning rates of each optimizer even when LR schedulers are not specified by the user in training.

When LR schedulers are not specified, trainer.optimizers is used instead of trainer.lr_schedulers for getting names of the optimizers, and then we iterate over the optimizers to log their learning rates.

Extra tests are written to make sure that the learning rates are logged with correct names even when schedulers are not defined.

Motivation

Currently the LR monitor callback only takes effect if the user specifies LR schedulers for training. However, one could want to record what the initial learning rate used for their optimizer's parameter groups even without any LR schedulers. This PR tries to solve this issue.
Fixes #7468

Does your PR introduce any breaking changes?

No

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

@codecov
Copy link

codecov bot commented Oct 1, 2021

Codecov Report

Merging #9786 (801d928) into master (b530b7a) will decrease coverage by 4%.
The diff coverage is 100%.

❗ Current head 801d928 differs from pull request most recent head 5736d3a. Consider uploading reports for the commit 5736d3a to get more accurate results

@@           Coverage Diff           @@
##           master   #9786    +/-   ##
=======================================
- Coverage      93%     89%    -4%     
=======================================
  Files         178     179     +1     
  Lines       15652   15778   +126     
=======================================
- Hits        14508   14009   -499     
- Misses       1144    1769   +625     

pytorch_lightning/callbacks/lr_monitor.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/lr_monitor.py Outdated Show resolved Hide resolved
@ananthsub ananthsub added callback logging Related to the `LoggerConnector` and `log()` labels Oct 1, 2021
@ananthsub ananthsub added this to the v1.5 milestone Oct 1, 2021
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT !

pytorch_lightning/callbacks/lr_monitor.py Show resolved Hide resolved
tests/callbacks/test_lr_monitor.py Outdated Show resolved Hide resolved
tests/callbacks/test_lr_monitor.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
tests/callbacks/test_lr_monitor.py Outdated Show resolved Hide resolved
tests/callbacks/test_lr_monitor.py Outdated Show resolved Hide resolved
tests/callbacks/test_lr_monitor.py Outdated Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Oct 13, 2021
@SkafteNicki SkafteNicki enabled auto-merge (squash) October 14, 2021 11:02
@SkafteNicki SkafteNicki merged commit 1569869 into Lightning-AI:master Oct 14, 2021
rohitgr7 added a commit to Tshimanga/pytorch-lightning that referenced this pull request Oct 18, 2021
…d. (Lightning-AI#9786)

* LR logging works even with no lr scheduler, wrote few extra tests as well

* updated changelog

* modified code as suggested by DeepSource

* added helper functions

* opt with no scheduler

* rename

* chlog

* update test

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: rohitgr7 <rohitgr1998@gmail.com>
@rohitgr7 rohitgr7 mentioned this pull request Oct 18, 2021
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callback logging Related to the `LoggerConnector` and `log()` ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend the Learning Rate Monitor callback to log the initial lr for optimizers
6 participants