-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added ReduceLROnPlateau callback for VI. #7011
Open
alvaropp
wants to merge
16
commits into
pymc-devs:main
Choose a base branch
from
alvaropp:lr_scheduling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
caf0f34
Added ReduceLROnPlateau and test.
alvaropp 0afd475
Pass optimiser and not shared variable to callback
alvaropp 9e27de5
American spelling.
alvaropp d66d523
Linting.
alvaropp a2c27bd
Removed unused variable.
alvaropp 74850bc
Updated test to use all optimizers.
alvaropp d2abbcd
Added baseclass and ExponentialDecay.
alvaropp 3068e0c
Added tests.
alvaropp 86f4097
Docstrings.
alvaropp ba3e3f2
Linting.
alvaropp 8bf6241
Added new callbacks to __all__.
alvaropp 2700c2e
Implement schedulers as function wrappers
jessegrabowski 56e7772
Implement schedulers as function wrappers
jessegrabowski 03953cd
Implement schedulers as function wrappers
jessegrabowski 79e1304
Run pre-commit hooks
jessegrabowski d375bf0
Change typehint from `Callable` to `partial` for mypy
jessegrabowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
|
||
import numpy as np | ||
|
||
__all__ = ["Callback", "CheckParametersConvergence", "Tracker"] | ||
__all__ = ["Callback", "CheckParametersConvergence", "ReduceLROnPlateau", "Tracker"] | ||
|
||
|
||
class Callback: | ||
|
@@ -93,6 +93,77 @@ | |
return np.concatenate([sh.get_value().flatten() for sh in shared_list]) | ||
|
||
|
||
class ReduceLROnPlateau(Callback): | ||
"""Reduce learning rate when the loss has stopped improving. | ||
|
||
This is inspired by Keras' homonymous callback: | ||
https://github.com/keras-team/keras/blob/v2.14.0/keras/callbacks.py | ||
|
||
Parameters | ||
---------- | ||
optimizer: callable | ||
PyMC optimizer | ||
factor: float | ||
factor by which the learning rate will be reduced: `new_lr = lr * factor` | ||
patience: int | ||
number of epochs with no improvement after which learning rate will be reduced | ||
min_lr: float | ||
lower bound on the learning rate | ||
cooldown: int | ||
number of iterations to wait before resuming normal operation after lr has been reduced | ||
verbose: bool | ||
false: quiet, true: update messages | ||
""" | ||
|
||
def __init__( | ||
self, | ||
optimizer, | ||
factor=0.1, | ||
patience=10, | ||
min_lr=1e-6, | ||
cooldown=0, | ||
): | ||
self.optimizer = optimizer | ||
self.factor = factor | ||
self.patience = patience | ||
self.min_lr = min_lr | ||
self.cooldown = cooldown | ||
|
||
self.cooldown_counter = 0 | ||
self.wait = 0 | ||
self.best = float("inf") | ||
|
||
def __call__(self, approx, loss_hist, i): | ||
current = loss_hist[-1] | ||
|
||
if np.isinf(current): | ||
return | ||
|
||
if self.in_cooldown(): | ||
self.cooldown_counter -= 1 | ||
self.wait = 0 | ||
return | ||
|
||
if current < self.best: | ||
self.best = current | ||
self.wait = 0 | ||
elif not np.isinf(self.best): | ||
self.wait += 1 | ||
if self.wait >= self.patience: | ||
self.reduce_lr() | ||
self.cooldown_counter = self.cooldown | ||
self.wait = 0 | ||
|
||
def reduce_lr(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still prefer that this was done symbolically with shared variables, because it will allow for composition between learning rate annealing strategies. |
||
old_lr = float(self.optimizer.keywords["learning_rate"]) | ||
if old_lr > self.min_lr: | ||
new_lr = max(old_lr * self.factor, self.min_lr) | ||
self.optimizer.keywords["learning_rate"] = new_lr | ||
|
||
def in_cooldown(self): | ||
return self.cooldown_counter > 0 | ||
|
||
|
||
class Tracker(Callback): | ||
""" | ||
Helper class to record arbitrary stats during VI | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the user have to provide this? Can it instead be inferred somehow from the host VI object? It's ugly to have to pass the optimizer twice (once for the VI itself, then again in the callback)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this would be great, but I haven't figured out whether it's possible. Probably one for someone more familiar with the codebase :)