8000 MLPRegressor.partial_fit produces an error when early_stopping is True · Issue #25693 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MLPRegressor.partial_fit produces an error when early_stopping is True #25693

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

Closed
ilya-causalens opened this issue Feb 24, 2023 · 1 comment · Fixed by #25694
Closed

MLPRegressor.partial_fit produces an error when early_stopping is True #25693

ilya-causalens opened this issue Feb 24, 2023 · 1 comment · Fixed by #25694

Comments

@ilya-causalens
Copy link

Describe the bug

WIth sklearn = 1.2.1, when using early_stopping = True, fit works fine, but partial fit produces the following error:

I think this is related to this change: #24683.

Steps/Code to Reproduce

import numpy.random
from sklearn.neural_network import MLPRegressor

if __name__ == '__main__':
    x = numpy.random.normal(size=(200, 4))
    y = x[:, 0] * 2 + x[:, 1] * 3 + x[:, 3] + numpy.random.normal(size=(200,))

    algo = MLPRegressor(early_stopping=True).fit(x, y)
    algo.partial_fit(x, y)

Expected Results

If early stopping is not supported for partial fit, it should handle this gracefully. If this is a bug - it should be fixed.

Actual Results

Traceback (most recent call last):
  File "/Users/ilyastolyarov/Repos/new/clpu/examples/usecases/script.py", line 12, in <module>
    algo.partial_fit(x, y)
  File "/Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 1640, in partial_fit
    return self._fit(X, y, incremental=True)
  File "/Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 471, in _fit
    self._fit_stochastic(
  File "/Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 653, in _fit_stochastic
    self._update_no_improvement_count(early_stopping, X_val, y_val)
  File "/Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 721, in _update_no_improvement_count
    if self.loss_curve_[-1] > self.best_loss_ - self.tol:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

Versions

System:
    python: 3.8.12 (default, May 16 2022, 17:58:21)  [Clang 13.0.0 (clang-1300.0.29.30)]
executable: /Users/ilyastolyarov/Repos/new/clpu/.venv/bin/python
   machine: macOS-11.6-x86_64-i386-64bit
Python dependencies:
      sklearn: 1.2.1
          pip: 22.3.1
   setuptools: 64.0.2
        numpy: 1.23.5
        scipy: 1.9.3
       Cython: 0.29.33
       pandas: 1.5.3
   matplotlib: 3.7.0
       joblib: 1.2.0
threadpoolctl: 3.1.0
Built with OpenMP: True
threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/numpy/.dylibs/libopenblas64_.0.dylib
        version: 0.3.20
threading_layer: pthreads
   architecture: Nehalem
    num_threads: 8
       user_api: openmp
   internal_api: openmp
         prefix: libomp
       filepath: /Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/sklearn/.dylibs/libomp.dylib
        version: None
    num_threads: 8
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /Users/ilyastolyarov/Repos/new/clpu/.venv/lib/python3.8/site-packages/scipy/.dylibs/libopenblas.0.dylib
        version: 0.3.18
threading_layer: pthreads
   architecture: Nehalem
    num_threads: 8
@thomasjpfan
Copy link
Member

Thank you for opening this issue! I agree we should have a better error message. A code comment states that early_stopping does not work with partial_fit:

# early_stopping in partial_fit doesn't make sense
early_stopping = self.early_stopping and not incremental

I opened #25694 raise a more informative error.

@thomasjpfan thomasjpfan added module:neural_network and removed Needs Triage Issue requires triage labels Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0