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

SWA implementation #3249

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Empty file added pyro/contrib/optim/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions pyro/contrib/optim/swa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import torch
from pyro.optim import PyroOptim
from torchcontrib.optim import SWA as _SWA

def _swa_constructor(
param: torch.nn.Parameter,
base: type,
base_args: dict,
swa_args: dict,
) -> torch.optim.Optimizer:
base = base(param, **base_args)
optimizer = _SWA(base, **swa_args)
return optimizer

def SWA(args: dict) -> PyroOptim:
"""
Stochastic Weight Averaging (SWA) optimizer. [1]

References:
[1] 'Averaging Weights Leads to Wider Optima and Better Generalization',
Pavel Izmailov, Dmitry Podoprikhin, Timur Garipov, Dmitry Vetrov,
Andrew Gordon Wilson
Uncertainty in Artificial Intelligence (UAI), 2018

Arguments:
:param args: arguments for SWA optimizer

"""
return PyroOptim(_swa_constructor, args)

def swap_swa_sgd(optimizer: PyroOptim) -> None:
"""
Swap the SWA optimized parameters with samples.

Arguments:
:param optimizer: SWA optimizer
"""
for key, value in optimizer.optim_objs.items():
value.swap_swa_sgd()