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

Support ReduceLROnPlateau scheduler #1910

Merged
merged 4 commits into from
Jun 15, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pyro/optim/lr_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

class PyroLRScheduler(PyroOptim):
"""
A wrapper for torch.optim.lr_scheduler objects that adjust learning rates
A wrapper for :class:`~torch.optim.lr_scheduler` objects that adjusts learning rates
for dynamically generated parameters.

:param optim_constructor: a torch.optim.lr_scheduler
:param scheduler_constructor: a :class:`~torch.optim.lr_scheduler`
:param optim_args: a dictionary of learning arguments for the optimizer or a callable that returns
such dictionaries. must contain the key 'optimizer' with pytorch optimizer value

Example::

optimizer = torch.optim.SGD
pyro_scheduler = pyro.optim.ExponentialLR({'optimizer': optimizer, 'optim_args': {'lr': 0.01}, 'gamma': 0.1})
scheduler = pyro.optim.ExponentialLR({'optimizer': optimizer, 'optim_args': {'lr': 0.01}, 'gamma': 0.1})
svi = SVI(model, guide, pyro_scheduler, loss=TraceGraph_ELBO())
svi.step()
pyro_scheduler.step(epoch=epoch)
for i in range(epochs):
jpchen marked this conversation as resolved.
Show resolved Hide resolved
for minibatch in DataLoader(dataset, batch_size):
svi.step(minibatch)
scheduler.step(epoch=epoch)
"""
def __init__(self, scheduler_constructor, optim_args):
# pytorch scheduler
Expand All @@ -38,5 +40,9 @@ def _get_optim(self, params):
return self.pt_scheduler_constructor(optim, **self.kwargs)

def step(self, *args, **kwargs):
"""
Takes the same arguments as the PyTorch scheduler
(optional ``epoch`` kwarg or ``loss`` in for ``ReduceLROnPlateau``)
"""
for scheduler in self.optim_objs.values():
scheduler.step(*args, **kwargs)