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

SOLVER - add skglm L-BFGS #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions solvers/skglm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import warnings
from benchopt import BaseSolver, safe_import_context

with safe_import_context() as import_ctx:
from sklearn.exceptions import ConvergenceWarning

from skglm.penalties import L2
from skglm.solvers import LBFGS
from skglm.datafits import Logistic

from skglm.utils.jit_compilation import compiled_clone


class Solver(BaseSolver):
name = 'skglm'
stopping_strategy = "iteration"

install_cmd = 'conda'
requirements = [
'pip:git+https://github.com/scikit-learn-contrib/skglm.git@main'
]

references = [
'Q. Bertrand and Q. Klopfenstein and P.-A. Bannier and G. Gidel'
'and M. Massias'
'"Beyond L1: Faster and Better Sparse Models with skglm", '
'https://arxiv.org/abs/2204.07826'
]

def set_objective(self, X, y, lmbd):
self.X, self.y, self.lmbd = X, y, lmbd
n_samples = X.shape[0]

self.datafit = compiled_clone(Logistic())
self.penalty = compiled_clone(L2(lmbd / n_samples))

warnings.filterwarnings('ignore', category=ConvergenceWarning)
self.solver = LBFGS(tol=1e-20)

# cache Numba compilation
self.run(3)

def run(self, n_iter):
self.solver.max_iter = n_iter

self.coef_ = self.solver.solve(
self.X, self.y, self.datafit, self.penalty)[0]

@staticmethod
def get_next(stop_val):
return stop_val + 1

def get_result(self):
return self.coef_.flatten()