Skip to content

FIX Improves error message in partial_fit when early_stopping=True #25694

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

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ Changelog
no longer raise warnings when fitting data with feature names.
:pr:`24873` by :user:`Tim Head <betatim>`.

- |Fix| Improves error message in :class:`neural_network.MLPClassifier` and
:class:`neural_network.MLPRegressor`, when `early_stopping=True` and
:meth:`partial_fit` is called. :pr:`25694` by `Thomas Fan`_.

:mod:`sklearn.preprocessing`
............................

Expand Down
4 changes: 3 additions & 1 deletion sklearn/neural_network/_multilayer_perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,9 @@ def _fit_stochastic(
)

# early_stopping in partial_fit doesn't make sense
early_stopping = self.early_stopping and not incremental
if self.early_stopping and incremental:
raise ValueError("partial_fit does not support early_stopping=True")
early_stopping = self.early_stopping
if early_stopping:
# don't stratify in multilabel classification
should_stratify = is_classifier(self) and self.n_outputs_ == 1
Expand Down
13 changes: 13 additions & 0 deletions sklearn/neural_network/tests/test_mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,16 @@ def test_mlp_warm_start_no_convergence(MLPEstimator, solver):
with pytest.warns(ConvergenceWarning):
model.fit(X_iris, y_iris)
assert model.n_iter_ == 20


@pytest.mark.parametrize("MLPEstimator", [MLPClassifier, MLPRegressor])
def test_mlp_partial_fit_after_fit(MLPEstimator):
"""Check partial fit does not fail after fit when early_stopping=True.

Non-regression test for gh-25693.
"""
mlp = MLPEstimator(early_stopping=True, random_state=0).fit(X_iris, y_iris)

msg = "partial_fit does not support early_stopping=True"
with pytest.raises(ValueError, match=msg):
mlp.partial_fit(X_iris, y_iris)