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

Paramscheduler emahandler #2326

Merged
merged 11 commits into from
Nov 21, 2021
Merged

Conversation

fco-dv
Copy link
Contributor

@fco-dv fco-dv commented Nov 14, 2021

Fixes #2295

Description:
Add create_new parameter to attach method . This flag is used to create param_name on engine.state and is taking into account whether param_name attribute already exists or not on engine.state. Overrides existing attribute by default.

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

@github-actions github-actions bot added the module: handlers Core Handlers module label Nov 14, 2021
Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @fco-dv ! I left few comments

@@ -13,6 +15,9 @@ class StateParamScheduler(BaseParamScheduler):
param_name: name of parameter to update.
save_history: whether to log the parameter values to
`engine.state.param_history`, (default=False).
create_new: in case `param_name` already exists in `engine.state`, whether to authorize `StateParamScheduler`
Copy link
Collaborator

Choose a reason for hiding this comment

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

@fco-dv please use double-ticks ```` instead of single-tick when addresses code. Here and above.

Suggested change
create_new: in case `param_name` already exists in `engine.state`, whether to authorize `StateParamScheduler`
create_new: in case ``param_name`` already exists in ``engine.state``, whether to authorize ``StateParamScheduler``

f"This may be a conflict between multiple StateParameterScheduler handlers."
f"Please choose another name."
)
if not self.create_new:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not quite understand this if/else clauses, especially "else" one.
I was thinking about the following logic:

  1. param_name NOT in engine.state AND create_new is True => simply create new attribute
  2. param_name NOT in engine.state AND create_new is False => warn that we will create new attribute, but to remove this warning, create_new should be True
  3. param_name in engine.state AND create_new is True => raise error as we can not create new attribute as it already exists in the state.
  4. param_name in engine.state AND create_new is False => silently override existing attribute

What do you think ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes , I've misunderstood the case 3, thought we were handling the creation of state parameter in that case ... So yes Indeed, it's better and simpler ! Thanks

fco-dv and others added 2 commits November 15, 2021 22:25
Co-authored-by: vfdev <vfdev.5@gmail.com>
Co-authored-by: vfdev <vfdev.5@gmail.com>
@fco-dv fco-dv marked this pull request as ready for review November 15, 2021 21:28
Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks for the update @fco-dv
Can you improve tests a bit. Thanks !

Comment on lines 394 to 413
def test_param_scheduler_with_ema_handler_attach_exception():
import torch.nn as nn

from ignite.handlers import EMAHandler

data = torch.rand(100, 2)
model = nn.Linear(2, 1)
trainer = Engine(lambda e, b: model(b))
param_name = "ema_decay"
save_history = True
create_new = True

ema_handler = EMAHandler(model)
ema_handler.attach(trainer, name=param_name, event=Events.ITERATION_COMPLETED)
ema_decay_scheduler = PiecewiseLinearStateScheduler(
param_name=param_name,
milestones_values=[(0, 0.0), (10 * len(data), 0.999)],
save_history=save_history,
create_new=create_new,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you do not need to add EMAHandler to check ValueError.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a working test with EMAHandler and PiecewiseLinearStateScheduler like in the issue ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes sure , thanks for your comments @vfdev-5 !

@vfdev-5 vfdev-5 enabled auto-merge (squash) November 21, 2021 21:09
@vfdev-5 vfdev-5 merged commit 41f33fc into pytorch:master Nov 21, 2021
fco-dv added a commit to fco-dv/ignite that referenced this pull request Nov 23, 2021
* pytorch#2090 make work EMAHandler with StateParamScheduler

* pytorch#2295 update docstring for parameter `create_new`

* pytorch#2295 fixing attach method logic and update associated tests

* pytorch#2295 fix unused import / remove useless test

* pytorch#2295 fix tests

* pytorch#2295 rm else statement

* Update ignite/handlers/state_param_scheduler.py

Co-authored-by: vfdev <vfdev.5@gmail.com>

* Update ignite/handlers/state_param_scheduler.py

Co-authored-by: vfdev <vfdev.5@gmail.com>

* pytorch#2295 update tests assert messages

* pytorch#2295 update tests, add working example with EMAHandler

Co-authored-by: vfdev <vfdev.5@gmail.com>
Ishan-Kumar2 pushed a commit to Ishan-Kumar2/ignite that referenced this pull request Dec 26, 2021
* pytorch#2090 make work EMAHandler with StateParamScheduler

* pytorch#2295 update docstring for parameter `create_new`

* pytorch#2295 fixing attach method logic and update associated tests

* pytorch#2295 fix unused import / remove useless test

* pytorch#2295 fix tests

* pytorch#2295 rm else statement

* Update ignite/handlers/state_param_scheduler.py

Co-authored-by: vfdev <vfdev.5@gmail.com>

* Update ignite/handlers/state_param_scheduler.py

Co-authored-by: vfdev <vfdev.5@gmail.com>

* pytorch#2295 update tests assert messages

* pytorch#2295 update tests, add working example with EMAHandler

Co-authored-by: vfdev <vfdev.5@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: handlers Core Handlers module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make work EMAHandler with state parameter scheduler
3 participants