knakamura13 / mlrose-ky

A highly optimized fork of the popular mlrose-hiive package. For Machine Learning, Randomized Optimization and SEarch algorithms.
https://nkapila6.github.io/mlrose-ky/
BSD 3-Clause "New" or "Revised" License
16 stars 5 forks source link

RecursionError in SKMLPRunner due to infinite recursion in `_mlp_loss_grad_lbfgs_intercept` #17

Open knakamura13 opened 4 days ago

knakamura13 commented 4 days ago

Issue Summary A RecursionError Exception in nn_examples.ipynb causes the notebook to be unusable.

The error indicates an infinite recursion in SKMLPRunner, specifically within the _mlp_loss_grad_lbfgs_intercept method.

The issue requires debugging of SKMLPRunner and possibly its inherited classes to resolve the problematic implementation.

Steps to Reproduce

Expected Behavior This Exception should not occur. It indicates a problematic implementation in runners.SKMLPRunner, which requires further investigation.

Observed Behavior The full Traceback for this exception is shared below:

ValueError: 
All the 5 fits failed.
It is very likely that your model is misconfigured.
You can try to debug the error by setting error_score='raise'.

Below are more details about the failures:
--------------------------------------------------------------------------------
5 fits failed with the following error:
Traceback (most recent call last):
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/sklearn/model_selection/_validation.py", line 888, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "/Users/kylenakamura/documents-local/development-local/side-projects/mlrose-ky/src/mlrose_ky/runners/skmlp_runner.py", line 77, in fit
    return self.mlp.fit(x_train, y_train)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/sklearn/base.py", line 1473, in wrapper
    return fit_method(estimator, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 751, in fit
    return self._fit(X, y, incremental=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 488, in _fit
    self._fit_lbfgs(
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 532, in _fit_lbfgs
    opt_res = scipy.optimize.minimize(
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_minimize.py", line 713, in minimize
    res = _minimize_lbfgsb(fun, x0, args, jac, bounds,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_lbfgsb_py.py", line 347, in _minimize_lbfgsb
    sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_optimize.py", line 288, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py", line 166, in __init__
    self._update_fun()
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py", line 262, in _update_fun
    self._update_fun_impl()
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py", line 163, in update_fun
    self.f = fun_wrapped(self.x)
             ^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py", line 145, in fun_wrapped
    fx = fun(np.copy(x), *args)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_optimize.py", line 79, in __call__
    self._compute_if_needed(x, *args)
  File "/Users/kylenakamura/anaconda3/envs/machine-learning/lib/python3.11/site-packages/scipy/optimize/_optimize.py", line 73, in _compute_if_needed
    fg = self.fun(x, *args)
         ^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/documents-local/development-local/side-projects/mlrose-ky/src/mlrose_ky/runners/skmlp_runner.py", line 94, in _loss_grad_lbfgs_intercept
    f, g = self._mlp_loss_grad_lbfgs(packed_coef_inter, x, y, activations, deltas, coef_grads, intercept_grads)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/documents-local/development-local/side-projects/mlrose-ky/src/mlrose_ky/runners/skmlp_runner.py", line 94, in _loss_grad_lbfgs_intercept
    f, g = self._mlp_loss_grad_lbfgs(packed_coef_inter, x, y, activations, deltas, coef_grads, intercept_grads)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kylenakamura/documents-local/development-local/side-projects/mlrose-ky/src/mlrose_ky/runners/skmlp_runner.py", line 94, in _loss_grad_lbfgs_intercept
    f, g = self._mlp_loss_grad_lbfgs(packed_coef_inter, x, y, activations, deltas, coef_grads, intercept_grads)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  [Previous line repeated 966 more times]
RecursionError: maximum recursion depth exceeded

Additional Context

knakamura13 commented 2 days ago

Possibly relevant PR in hiive: https://github.com/hiive/mlrose/pull/32

Though, if that PR fixes it, then why is the master branch on Hiive still broken?