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

ENH - Run on grid stopping criterion #671

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

TheoGuyard
Copy link
Contributor

This PR adds a RunOnGridCriterion so that solvers can be run over a grid of parameters that can represent anything: a number of iterations, a target accuracy, an hyperparameter value, etc... The solver is stopped either when the end of the grid is met or when the default triggers in StoppingCriterion.should_stop() are activated. I've used such criterion for the sparse support recovery benchmark so I think this may be useful for other ones.

Example of usage

Below is an example were a one runs a solver over a grid of two parameters a and b, chosen within np.linspace(0, 0.1, 10) and np.linspace(1, 1000, 10), respectively.

import itertools
from benchopt import BaseSolver
from benchopt.stopping_criterion import RunOnGridCriterion

class MyNewSolver(BaseSolver):
    name = "my_new_solver"
    param_a_values = np.linspace(0, 0.1, 10)
    param_b_values = np.linspace(1, 1000, 10)
    stopping_criterion = RunOnGridCriterion(grid=list(itertools.product(a,b)))
    ...
    def run(self, grid_value):
        self.solver.solve(instance, param_a=grid_value[0], param_b=grid_value[1])
        ....

Caching and grid parameter

I faced a small issue while implementing my feature. The grid parameter in the __init__() function must have a default value otherwise benchopt is not able to run the benchmark. It turns out that the stopping criterion is cached by benchopt and the resulting effect is that the default grid value defined in the class RunOnGridCriterion is always the one used in the run method of the solvers, even when a new stopping_criterion with a different grid value is defined in the solver class. @tomMoral said that this could be solved by overloading the get_runner_instance() function but I'm not sure I have done that is the best way. Let me know if you think of a smarter way to do that !

Checks before merging PR

  • added documentation for any new feature
  • added unit test
  • edited the what's new (if applicable)

@TheoGuyard TheoGuyard changed the title Run on grid stopping criterion ENH - Run on grid stopping criterion Oct 10, 2023
@TheoGuyard TheoGuyard mentioned this pull request Oct 10, 2023
3 tasks
@codecov-commenter
Copy link

Codecov Report

Merging #671 (0848c31) into main (223bfad) will decrease coverage by 0.14%.
Report is 1 commits behind head on main.
The diff coverage is 33.33%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##             main     #671      +/-   ##
==========================================
- Coverage   54.88%   54.75%   -0.14%     
==========================================
  Files          45       45              
  Lines        2928     2946      +18     
  Branches      539      539              
==========================================
+ Hits         1607     1613       +6     
- Misses       1196     1208      +12     
  Partials      125      125              

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.

None yet

2 participants