Skip to content

MLPEstimator do not report the proper n_iter_ after successive call of fit #24764

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
glemaitre opened this issue Oct 26, 2022 · 5 comments · Fixed by #25443
Closed

MLPEstimator do not report the proper n_iter_ after successive call of fit #24764

glemaitre opened this issue Oct 26, 2022 · 5 comments · Fixed by #25443

Comments

@glemaitre
Copy link
Member

glemaitre commented Oct 26, 2022

Describe the bug

It seems that MLPRegressor and MLPClassifier does not take into account max_iter with warm_start.

Steps/Code to Reproduce

model = MLPRegressor(warm_start=True, early_stopping=True, max_iter=10)
model.fit(X_iris, y_iris)
assert model.n_iter_ <= 10
model.set_params(max_iter=20)
model.fit(X_iris, y_iris)
assert model.n_iter_ <= 20

You can also try MLPClassifier which leads to over iterating.

Expected Results

n_iter_ <= 20

Actual Results

    def test_xxx():
        model = MLPRegressor(warm_start=True, early_stopping=False, max_iter=10)
        model.fit(X_iris, y_iris)
        assert model.n_iter_ <= 10
        model.set_params(max_iter=20)
        model.fit(X_iris, y_iris)
>       assert model.n_iter_ <= 20
E       assert 30 <= 20
E        +  where 30 = MLPRegressor(max_iter=20, warm_start=True).n_iter_

Versions

System:
    python: 3.8.12 | packaged by conda-forge | (default, Sep 16 2021, 01:38:21)  [Clang 11.1.0 ]
executable: /Users/glemaitre/mambaforge/envs/dev/bin/python
   machine: macOS-12.6-arm64-arm-64bit

Python dependencies:
      sklearn: 1.2.dev0
          pip: 21.3
   setuptools: 58.2.0
        numpy: 1.21.6
        scipy: 1.8.0
       Cython: 0.29.24
       pandas: 1.5.0
   matplotlib: 3.4.3
       joblib: 1.2.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /Users/glemaitre/mambaforge/envs/dev/lib/libopenblas_vortexp-r0.3.18.dylib
        version: 0.3.18
threading_layer: openmp
   architecture: VORTEX
    num_threads: 8

       user_api: openmp
   internal_api: openmp
         prefix: libomp
       filepath: /Users/glemaitre/mambaforge/envs/dev/lib/libomp.dylib
        version: None
    num_threads: 8
@glemaitre glemaitre added Bug Needs Triage Issue requires triage and removed Needs Triage Issue requires triage labels Oct 26, 2022
@glemaitre glemaitre changed the title MLPRegressor does not respect max_iter MLPRegressor and MLPClassifier does not respect max_iter with warm_start=True Oct 26, 2022
@glemaitre
Copy link
Member Author

Note that LBFGS solver works as expected.

@glemaitre
Copy link
Member Author

OK, the reason is that the n_iter_ is not reset to 0 in the non-incremental case (calling fit instead of partial_fit.

@glemaitre glemaitre changed the title MLPRegressor and MLPClassifier does not respect max_iter with warm_start=True MLPEstimator do not report the proper n_iter_ after successive call of fit Oct 26, 2022
@glemaitre
Copy link
Member Author

Need to check if this is a bug or a feature cf. #16812

@jeremiedbb
Copy link
Member

jeremiedbb commented Oct 28, 2022

But with warm start, aren't you asking for 20 more iterations ? (leading to 30 in total)

EDIT: actually, thinking more about that I agree that it should be 20 in total (we are not doing incremental learning here)

@glemaitre
Copy link
Member Author

Just to confirmed that this is a bug because the above test would work with RandomForest.

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.

3 participants