-
Notifications
You must be signed in to change notification settings - Fork 109
Fix switch_optimizer callback #608
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
GiovanniCanali
merged 1 commit into
mathLab:dev
from
GiovanniCanali:fix_switch_optimizer
Jul 25, 2025
+62
−37
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 |
|---|---|---|
| @@ -1,45 +1,63 @@ | ||
| from pina.callback import SwitchOptimizer | ||
| import torch | ||
| import pytest | ||
|
|
||
| from pina.solver import PINN | ||
| from pina.trainer import Trainer | ||
| from pina.model import FeedForward | ||
| from pina.problem.zoo import Poisson2DSquareProblem as Poisson | ||
| from pina.optim import TorchOptimizer | ||
| from pina.callback import SwitchOptimizer | ||
| 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 optimizer | ||
| optimizer = TorchOptimizer(torch.optim.Adam) | ||
|
|
||
| # make the problem | ||
| poisson_problem = Poisson() | ||
| boundaries = ["g1", "g2", "g3", "g4"] | ||
| n = 10 | ||
| poisson_problem.discretise_domain(n, "grid", domains=boundaries) | ||
| poisson_problem.discretise_domain(n, "grid", domains="D") | ||
| model = FeedForward( | ||
| len(poisson_problem.input_variables), len(poisson_problem.output_variables) | ||
| ) | ||
| # Initialize the solver | ||
| solver = PINN(problem=problem, model=model, optimizer=optimizer) | ||
|
|
||
| # make the solver | ||
| solver = PINN(problem=poisson_problem, model=model) | ||
| # Define new optimizers for testing | ||
| lbfgs = TorchOptimizer(torch.optim.LBFGS, lr=1.0) | ||
| adamW = TorchOptimizer(torch.optim.AdamW, lr=0.01) | ||
|
|
||
| adam = TorchOptimizer(torch.optim.Adam, lr=0.01) | ||
| lbfgs = TorchOptimizer(torch.optim.LBFGS, lr=0.001) | ||
|
|
||
| @pytest.mark.parametrize("epoch_switch", [5, 10]) | ||
| @pytest.mark.parametrize("new_opt", [lbfgs, adamW]) | ||
| def test_switch_optimizer_constructor(new_opt, epoch_switch): | ||
|
|
||
| def test_switch_optimizer_constructor(): | ||
| SwitchOptimizer(adam, epoch_switch=10) | ||
| # Constructor | ||
| SwitchOptimizer(new_optimizers=new_opt, epoch_switch=epoch_switch) | ||
|
|
||
| # Should fail if epoch_switch is less than 1 | ||
| with pytest.raises(ValueError): | ||
| SwitchOptimizer(new_optimizers=new_opt, epoch_switch=0) | ||
|
|
||
| def test_switch_optimizer_routine(): | ||
| # check initial optimizer | ||
|
|
||
| @pytest.mark.parametrize("epoch_switch", [5, 10]) | ||
| @pytest.mark.parametrize("new_opt", [lbfgs, adamW]) | ||
| def test_switch_optimizer_routine(new_opt, epoch_switch): | ||
|
|
||
| # Check if the optimizer is initialized correctly | ||
| solver.configure_optimizers() | ||
| assert solver.optimizer.instance.__class__ == torch.optim.Adam | ||
| # make the trainer | ||
| switch_opt_callback = SwitchOptimizer(lbfgs, epoch_switch=3) | ||
|
|
||
| # Initialize the trainer | ||
| switch_opt_callback = SwitchOptimizer( | ||
| new_optimizers=new_opt, epoch_switch=epoch_switch | ||
| ) | ||
| trainer = Trainer( | ||
| solver=solver, | ||
| callbacks=[switch_opt_callback], | ||
| callbacks=switch_opt_callback, | ||
| accelerator="cpu", | ||
| max_epochs=5, | ||
| max_epochs=epoch_switch + 2, | ||
| ) | ||
| trainer.train() | ||
| assert solver.optimizer.instance.__class__ == torch.optim.LBFGS | ||
|
|
||
| # Check that the trainer strategy optimizers have been updated | ||
| assert solver.optimizer.instance.__class__ == new_opt.instance.__class__ | ||
| assert ( | ||
| trainer.strategy.optimizers[0].__class__ == new_opt.instance.__class__ | ||
| ) |
Oops, something went wrong.
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.
Good catch! Maybe we could do a setter in the solver class? This way we will not forget in the future
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.
Hi @dario-coscia,
Adding a setter in the solver class feels a bit redundant with
configure_optimizers, in my opinion. It’s definitely a useful idea, but it might require more invasive changes.Since we’re already planning to refactor the solver logic, we could revisit this at that time instead.
If that sounds good to you, I’d go ahead and merge this PR for now.