Skip to content

Commit 34e297e

Browse files
committed
ENH fall back to lbfgs if line search did not converge
1 parent 7318a4f commit 34e297e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

sklearn/linear_model/_glm/glm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,13 @@ def line_search(self, X, y, sample_weight):
358358
warnings.warn(
359359
f"Line search of Newton solver {self.__class__.__name__} at iteration "
360360
f"#{self.iteration} did no converge after 21 line search refinement "
361-
"iterations.",
361+
"iterations. It will now resort to lbfgs instead.",
362362
ConvergenceWarning,
363363
)
364+
if self.verbose:
365+
print(" Lines search did not converge and resorts to lbfgs instead.")
366+
self.use_fallback_lbfgs_solve = True
367+
return
364368

365369
self.raw_prediction = raw
366370

@@ -467,6 +471,8 @@ def solve(self, X, y, sample_weight):
467471
# self.loss_value, self.gradient_old, self.gradient,
468472
# self.raw_prediction.
469473
self.line_search(X=X, y=y, sample_weight=sample_weight)
474+
if self.use_fallback_lbfgs_solve:
475+
break
470476

471477
# 4. Check convergence
472478
# Sets self.converged.
@@ -537,7 +543,7 @@ def inner_solve(self, X, y, sample_weight):
537543
except (np.linalg.LinAlgError, scipy.linalg.LinAlgWarning) as e:
538544
warnings.warn(
539545
f"The inner solver of {self.__class__.__name__} stumbled upon a "
540-
"singular or very ill-conditioned hessian matrix at iteration "
546+
"singular or very ill-conditioned Hessian matrix at iteration "
541547
f"#{self.iteration}. It will now resort to lbfgs instead.\n"
542548
"Further options are to use another solver or to avoid such situation "
543549
"in the first place. Possible remedies are removing collinearfeatures "

sklearn/linear_model/_glm/tests/test_glm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,8 +1021,8 @@ def test_linalg_warning_with_newton_solver(newton_solver, global_random_seed):
10211021
# without regularization should raise an informative warning and fallback
10221022
# to the LBFGS solver.
10231023
msg = (
1024-
"The inner solver of .*NewtonSolver stumbled upon a"
1025-
" singular or very ill-conditioned Hessian matrix"
1024+
"The inner solver of .*NewtonSolver stumbled upon a singular or very "
1025+
"ill-conditioned Hessian matrix"
10261026
)
10271027
with pytest.warns(scipy.linalg.LinAlgWarning, match=msg):
10281028
reg = PoissonRegressor(solver=newton_solver, alpha=0.0, tol=tol).fit(

0 commit comments

Comments
 (0)