-
Notifications
You must be signed in to change notification settings - Fork 91
Switch scheduler callback #735
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Switch Scheduler | ||
| ===================== | ||
|
|
||
| .. currentmodule:: pina.callback.switch_scheduler | ||
| .. autoclass:: SwitchScheduler | ||
| :members: | ||
| :show-inheritance: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Module for the SwitchScheduler callback.""" | ||
|
|
||
| from lightning.pytorch.callbacks import Callback | ||
| from ..optim import TorchScheduler | ||
| from ..utils import check_consistency, check_positive_integer | ||
|
|
||
|
|
||
| class SwitchScheduler(Callback): | ||
| """ | ||
| Callback to switch scheduler during training. | ||
| """ | ||
|
|
||
| def __init__(self, new_schedulers, epoch_switch): | ||
| """ | ||
| This callback allows switching between different schedulers during | ||
| training, enabling the exploration of multiple optimization strategies | ||
| without interrupting the training process. | ||
|
|
||
| :param new_schedulers: The scheduler or list of schedulers to switch to. | ||
| Use a single scheduler for single-model solvers, or a list of | ||
| schedulers when working with multiple models. | ||
| :type new_schedulers: pina.optim.TorchScheduler | | ||
| list[pina.optim.TorchScheduler] | ||
| :param int epoch_switch: The epoch at which the scheduler switch occurs. | ||
| :raise AssertionError: If epoch_switch is less than 1. | ||
| :raise ValueError: If each scheduler in ``new_schedulers`` is not an | ||
| instance of :class:`pina.optim.TorchScheduler`. | ||
|
|
||
| Example: | ||
| >>> scheduler = TorchScheduler( | ||
| >>> torch.optim.lr_scheduler.StepLR, step_size=5 | ||
| >>> ) | ||
| >>> switch_callback = SwitchScheduler( | ||
| >>> new_schedulers=scheduler, epoch_switch=10 | ||
| >>> ) | ||
| """ | ||
| super().__init__() | ||
|
|
||
| # Check if epoch_switch is greater than 1 | ||
| check_positive_integer(epoch_switch - 1, strict=True) | ||
|
|
||
| # If new_schedulers is not a list, convert it to a list | ||
| if not isinstance(new_schedulers, list): | ||
| new_schedulers = [new_schedulers] | ||
|
|
||
| # Check consistency | ||
| for scheduler in new_schedulers: | ||
| check_consistency(scheduler, TorchScheduler) | ||
|
|
||
| # Store the new schedulers and epoch switch | ||
| self._new_schedulers = new_schedulers | ||
| self._epoch_switch = epoch_switch | ||
|
|
||
| def on_train_epoch_start(self, trainer, __): | ||
| """ | ||
| Switch the scheduler at the start of the specified training epoch. | ||
|
|
||
| :param lightning.pytorch.Trainer trainer: The trainer object managing | ||
| the training process. | ||
| :param __: Placeholder argument (not used). | ||
| """ | ||
| # Check if the current epoch matches the switch epoch | ||
| if trainer.current_epoch == self._epoch_switch: | ||
| schedulers = [] | ||
|
|
||
| # Hook the new schedulers to the model parameters | ||
| for idx, scheduler in enumerate(self._new_schedulers): | ||
| scheduler.hook(trainer.solver._pina_optimizers[idx]) | ||
| schedulers.append(scheduler) | ||
|
|
||
| # Update the trainer's scheduler configs | ||
| trainer.lr_scheduler_configs[idx].scheduler = scheduler.instance | ||
|
|
||
| # Update the solver's schedulers | ||
| trainer.solver._pina_schedulers = schedulers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import torch | ||
| import pytest | ||
|
|
||
| from pina.solver import PINN | ||
| from pina.trainer import Trainer | ||
| from pina.model import FeedForward | ||
| from pina.optim import TorchScheduler | ||
| from pina.callback import SwitchScheduler | ||
| from pina.problem.zoo import Poisson2DSquareProblem as Poisson | ||
|
|
||
|
|
||
| # Define the problem | ||
| problem = Poisson() | ||
| problem.discretise_domain(10) | ||
| model = FeedForward(len(problem.input_variables), len(problem.output_variables)) | ||
|
|
||
| # Define the scheduler | ||
| scheduler = TorchScheduler(torch.optim.lr_scheduler.ConstantLR, factor=0.1) | ||
|
|
||
| # Initialize the solver | ||
| solver = PINN(problem=problem, model=model, scheduler=scheduler) | ||
|
|
||
| # Define new schedulers for testing | ||
| step = TorchScheduler(torch.optim.lr_scheduler.StepLR, step_size=10, gamma=0.1) | ||
| exp = TorchScheduler(torch.optim.lr_scheduler.ExponentialLR, gamma=0.9) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("epoch_switch", [5, 10]) | ||
| @pytest.mark.parametrize("new_sched", [step, exp]) | ||
| def test_switch_scheduler_constructor(new_sched, epoch_switch): | ||
|
|
||
| # Constructor | ||
| SwitchScheduler(new_schedulers=new_sched, epoch_switch=epoch_switch) | ||
|
|
||
| # Should fail if epoch_switch is less than 1 | ||
| with pytest.raises(AssertionError): | ||
| SwitchScheduler(new_schedulers=new_sched, epoch_switch=0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("epoch_switch", [5, 10]) | ||
| @pytest.mark.parametrize("new_sched", [step, exp]) | ||
| def test_switch_scheduler_routine(new_sched, epoch_switch): | ||
|
|
||
| # Initialize the trainer | ||
| switch_sched_callback = SwitchScheduler( | ||
| new_schedulers=new_sched, epoch_switch=epoch_switch | ||
| ) | ||
| trainer = Trainer( | ||
| solver=solver, | ||
| callbacks=switch_sched_callback, | ||
| accelerator="cpu", | ||
| max_epochs=epoch_switch + 2, | ||
| ) | ||
| trainer.train() | ||
|
|
||
| # Check that the solver and trainer strategy schedulers have been updated | ||
| assert solver.scheduler.instance.__class__ == new_sched.instance.__class__ | ||
| assert ( | ||
| trainer.lr_scheduler_configs[0].scheduler.__class__ | ||
| == new_sched.instance.__class__ | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep the Scheduler callback, so that in the future, we can add other callbacks there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This differs from #445, where we chose to name each file after the class it contains.
That said, your observation raises a valid point. My proposal is the following: within the callback directory, we create a dedicated
schedulersubdirectory that, for now, will include onlyswitch_scheduler.py, while leaving room for future additions. This prevents the file from becoming overly long as more scheduler-related callbacks are introduced.The same structure would apply to
optimizer_callbackand processing_callback. In particular, the layout I have in mind is the following:--
callback--------
scheduler_callback---------------
switch_scheduler.py--------
optimizer_callback---------------
switch_optimizer.py---------
refinement---------------
refinement_interface.py---------------
r3_refinement.py--------
processing_callback---------------
metric_tracker.py---------------
pina_progress_bar.py---------------
normalizer_data_callback.pyHappy to hear your thoughts on this, @dario-coscia @FilippoOlivo @ndem0.
All these changes would be implemented in a dedicated PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer something like this (just remove
callbackon the subfolders)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good! Maybe I would fuse scheduler and optimizer in optim directory (this is similar to from pina.optim import ...)