Skip to content

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

@ilya-causalens

Description

@ilya-causalens

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions