Skip to content

Refactor optimizer classes and add new scheduler - #551

Draft
felix-e-h-p wants to merge 1 commit into
mainfrom
optimiser-rewrite
Draft

felix-e-h-p wants to merge 1 commit into
mainfrom
optimiser-rewrite

Conversation

@felix-e-h-p

Copy link
Copy Markdown
Contributor

The optimisers file has a lot of copy paste in it, so I’ve cleaned it up a bit. Gone from about 210 lines to about 110. All the class names and arguments are identical, so existing configs and saved models load as before.

Refactor optimizer classes to improve parameter handling and add AdamWReduceLROnPlateau with enhanced learning rate management.
Comment thread pvnet/optimizers.py
params = []
for c in children:
params += find_other_than_submodule_parameters(c, ignore_modules)
return params

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced with a simple loop over model.modules() further down.

Comment thread pvnet/optimizers.py
def __init__(self, lr: float = 0.0005, **opt_kwargs):
"""Store the learning rate and any extra kwargs passed to the torch optimizer"""
self.lr = lr # overwritten by the LR finder via PVNetLightningModule
self.opt_kwargs = opt_kwargs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adam and AdamW had identical __init__s, so moved that up here. Added a comment on self.lr because the Lightning module overwrites it when the LR finder runs, which wasn’t mentioned anywhere.

Comment thread pvnet/optimizers.py
def __call__(self, model: nn.Module):
"""Return optimizer"""
return torch.optim.AdamW(model.parameters(), lr=self.lr, **self.kwargs)
return torch.optim.AdamW(model.parameters(), lr=self.lr, **self.opt_kwargs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same behaviour as before, they just inherit the init now.

Comment thread pvnet/optimizers.py
"optimizer": opt,
"lr_scheduler": {"scheduler": sch, "monitor": monitor},
}
return {"optimizer": opt, "lr_scheduler": {"scheduler": sch, "monitor": monitor}}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The AdamW plus plateau scheduler plus monitor setup used to be copied in three places: once in EmbAdamWReduceLROnPlateau, and twice in this class (single lr and dict of lrs). Now written once here and just uses whatever _param_groups returns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant