Skip to content

Working ParameterScheduler #1949

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

Merged
merged 17 commits into from
Apr 7, 2021
Merged

Working ParameterScheduler #1949

merged 17 commits into from
Apr 7, 2021

Conversation

danieltudosiu
Copy link
Contributor

@danieltudosiu danieltudosiu commented Apr 5, 2021

fixes #1552

Description

Added a new ParameterScheduler handler and the required tests.

Status

Ready

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.
  • In-line docstrings updated.

Added a new ParameterScheduler handler and the required tests.

Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
@danieltudosiu
Copy link
Contributor Author

/black

@wyli
Copy link
Contributor

wyli commented Apr 5, 2021

thanks @danieltudosiu, could you follow this PR #1940 (it's also about new handler) to update the docs/ and min_tests.py

@danieltudosiu
Copy link
Contributor Author

@wyli Just to be sure that I do what you want me to do.

You want me to reference the inline documentation that is written in the class to create the docs by adding it to the docs/source/handlers.rst?

And you want to exclude the tests that I written from being run by min_tests.py?

Cheers!

@wyli
Copy link
Contributor

wyli commented Apr 5, 2021

exactly, thanks!

danieltudosiu and others added 8 commits April 5, 2021 20:05
Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
@danieltudosiu
Copy link
Contributor Author

danieltudosiu commented Apr 6, 2021

@wyli could you please help me with this:

In the flake8 test I get the following error:

monai/handlers/parameter_scheduler.py:52:43: error: Argument 2 to "isinstance" has incompatible type "_SpecialForm"; expected "Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]"  [arg-type]

Which is referring to the following lines:

49 def _get_value_calculator(self, value_calculator: Union[str, Callable]):
50    if isinstance(value_calculator, str):
51        return self._calculators[value_calculator]
52    elif isinstance(value_calculator, Callable):
53        return value_calculator
54    else:
55        raise ValueError(
56            f"value_calculator must be either a string from {list(self._calculators.keys())} or a Callable."
57        )

Thanks!

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Contributor

wyli commented Apr 6, 2021

I removed a few typing then it works fine...I think we can keep it simple for now

@danieltudosiu
Copy link
Contributor Author

Thanks, I will need to understand later what the error really was.

Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
@danieltudosiu
Copy link
Contributor Author

@wyli I have a precision problem with torch.testing.assert_allclose.

If I manually check the get_value at this location:
https://github.com/Project-MONAI/MONAI/pull/1949/files#diff-3875231bae9af2fe3c36a3f2765da9ed1b129142fb62f15be036e2f1085e35cdR51

I get 0.333..... but somehow the assert_allclose does not pass because of the following error:

AssertionError: With rtol=0.0 and atol=0.001, found 1 element(s) (out of 1) whose difference(s) exceeded the margin of error (including 0 nan comparisons). The greatest difference was 0.0033333301544189453 (3.3333332538604736 vs. 3.3299999237060547), which occurred at index 0.

Which tells me that there is a precision issue.

Could you please tell me what I do wrong? Because not even the actual value that the assert_allclose get seems to be wrong even if I use the same exact method to get it.

@wyli
Copy link
Contributor

wyli commented Apr 7, 2021

@wyli I have a precision problem with torch.testing.assert_allclose.

If I manually check the get_value at this location:
https://github.com/Project-MONAI/MONAI/pull/1949/files#diff-3875231bae9af2fe3c36a3f2765da9ed1b129142fb62f15be036e2f1085e35cdR51

I get 0.333..... but somehow the assert_allclose does not pass because of the following error:

AssertionError: With rtol=0.0 and atol=0.001, found 1 element(s) (out of 1) whose difference(s) exceeded the margin of error (including 0 nan comparisons). The greatest difference was 0.0033333301544189453 (3.3333332538604736 vs. 3.3299999237060547), which occurred at index 0.

Which tells me that there is a precision issue.

Could you please tell me what I do wrong? Because not even the actual value that the assert_allclose get seems to be wrong even if I use the same exact method to get it.

changed the expected to 3.333333 looks like it works...

@wyli wyli enabled auto-merge (squash) April 7, 2021 09:52
Copy link
Contributor

@wyli wyli left a comment

Choose a reason for hiding this comment

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

thanks

@wyli wyli merged commit 7581255 into Project-MONAI:master Apr 7, 2021
@vfdev-5
Copy link
Member

vfdev-5 commented Apr 7, 2021

@wyli @danieltudosiu nice PR and it makes me think of other schedules like piecewise or concat multiple schedules. Would you, guys, be interested in that if we promote ignite's contrib param scheduler to the core part such that MONAI could also use them ? We were also thinking about that and maybe some of more or less stable parts could be merged on our side.

@danieltudosiu
Copy link
Contributor Author

Disclaimer: I am just a Ph.D. student being annoyed by tiny features that are missing from MONAI so I am by no means a developer with any voting right on this XD.

My two cents:

@vfdev-5 just to make sure we are on the same page, this ParameterScheduler is more of a general-purpose parameter scheduler for things like EMA decays in VQ-VAEs, temperatures for gumbel softmax, etc. Basically anything else BUT the learning rate.

For learning rate, MONAI has the LRScheduler handler which is less advanced than what you present in param scheduler.

From my point of view, having an Ignite handler should trump a MONAI handler since MONAI should aim to mainly have specialized code for Medical stuff. So you promoting the Learning Rate handlers to would fall in like with that.

Additionally moving the ParameterScheduler to ignite will also be a better place than keeping it in MONAI, and seems to fall in line with your ParamScheduler. Looking at your codebase it seems that you (as I did) use the closed forms of the schedulers, if you promote them then in the transition it would be a good idea to just generalize it to non-optimizer parameters so that all that logic can be reused in other settings.


class ParamSchedulerHandler:
"""
General purpose scheduler for parameters values. By default it can schedule in a linear, exponential, step or
Copy link
Member

Choose a reason for hiding this comment

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

I think providing an example of usage would be of a great help for the users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I completely forgot about the use-case example. I will do another pull request to amend this one.

@vfdev-5
Copy link
Member

vfdev-5 commented Apr 7, 2021

@danieltudosiu thanks for the clarification ! Actually, you are right, I missed that part about your parameter scheduler is not related to the optimizer as the module in ignite (btw, it is not only about LR, but momentum and other optimizer's params). I rechecked your code and also agree that this could be also a nice addition to ignite itself as well, as we also have use-cases like that to schedule some hyperparameters...

By promoting I was just mentioning the fact that currently MONAI does not use any ignite's contrib submodules, so experiment tracking and param scheduling, time profilers are out of the scope for MONAI. So, I was just saying that from ignite's side we were thinking to move some independent parts of contrib module to ignite's core module. This could also make it available for MONAI.

@danieltudosiu
Copy link
Contributor Author

danieltudosiu commented Apr 7, 2021

@vfdev-5 Sorry that I missed the extended usability of your LR schedulers, I just had a quick glance at them.

Regarding the contrib package, I think MONAI does not use them because they are in contrib so their future is uncertain. But @wyli can expand on this since he is one of the lead developers.

From my point of view I think having the ParamScheduler and its children moved of contrib and their logic merged with the ParameterScheduler to have a more general-purpose class would be a good move.

@vfdev-5 if you give the green light to the move I would be more than happy to lead the move since I want to start contributing to Ignite as well.

@vfdev-5
Copy link
Member

vfdev-5 commented Apr 7, 2021

Regarding the contrib package, I think MONAI does not use them because they are in contrib so their future is uncertain. But @wyli can expand on this since he is one of the lead developers.

Yes, I totally understand that and agree with that. We are planning to merge it inside the core part to avoid confusions etc.

@vfdev-5 if you give the green light to the move I would be more than happy to lead the move since I want to start contributing to Ignite as well.

@danieltudosiu yes, please go for it 👍 green light. I'll open an issue for that on ignite's side and can ping you there...

@danieltudosiu
Copy link
Contributor Author

@danieltudosiu yes, please go for it 👍 green light. I'll open an issue for that on ignite's side and can ping you there...

Perfect, thanks!

nsrivathsa pushed a commit to nsrivathsa/MONAI that referenced this pull request Apr 12, 2021
* Working ParameterScheduler 

Added a new ParameterScheduler handler and the required tests.

Signed-off-by: Petru-Daniel Tudosiu <[email protected]>
Signed-off-by: Neha Srivathsa <[email protected]>
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.

Parameter scheduler
4 participants