Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MAINT validate parameters for MLPRregressor and MLPClassifier #23789

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

Conversation

Jitensid
Copy link
Contributor

@Jitensid Jitensid commented Jun 29, 2022

Reference Issues/PRs

MLPRegressor and MLPClassifier uses _validate_parameters as part of #23462

What does this implement/fix? Explain your changes.

  1. MLPRegressorand MLPClassifier estimators base class BaseMultilayerPerceptron has a new class attribute _parameter_constraints that defines the valid types and values for the parameters.
  2. partial_fit and fit methods first call the self._validate_params() method.

Any other comments?

Some tests are failing and need some help in them.

@jeremiedbb jeremiedbb added No Changelog Needed Validation related to input validation labels Jun 29, 2022
@glemaitre glemaitre changed the title [WIP] MLPRregressor and MLPClassifier uses validate params [WIP] validate parameters for MLPRregressor and MLPClassifier Jun 29, 2022
@glemaitre
Copy link
Member

@Jitensid I let you check the failure of the CI before making a review such that you can correct the code accordingly.

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @Jitensid. Here's a first pass.

Some of the CI failures are more subtle. I'll take a look

@Jitensid
Copy link
Contributor Author

Jitensid commented Jul 1, 2022

Thank for the suggestions @jeremiedbb and @glemaitre

@Micky774
Copy link
Contributor

Micky774 commented Jul 1, 2022

The current error is due to the common test checking for MLPClassifier.partial_fit to validate the parameters, however because of how it is called in the tests partial_fit(X, y), it actually violates the partial fit API in that it doesn't provided classes on first call. Since you have the validation done in the BaseMultilayerPerceptron.partial_fit method, it isn't reached in the common test because _check_partial_fit_first_call raises a ValueError in MLPClassifier.partial_fit before it can call super().partial_fit.

If we don't care about potentially calling self._validate_params multiple times, this can be fixed by inserting a call right before _check_partial_fit_first_call in MLPClassifier.partial_fit.

@Jitensid
Copy link
Contributor Author

Jitensid commented Jul 2, 2022

The current error is due to the common test checking for MLPClassifier.partial_fit to validate the parameters, however because of how it is called in the tests partial_fit(X, y), it actually violates the partial fit API in that it doesn't provided classes on first call. Since you have the validation done in the BaseMultilayerPerceptron.partial_fit method, it isn't reached in the common test because _check_partial_fit_first_call raises a ValueError in MLPClassifier.partial_fit before it can call super().partial_fit.

If we don't care about potentially calling self._validate_params multiple times, this can be fixed by inserting a call right before _check_partial_fit_first_call in MLPClassifier.partial_fit.

Thank you @Micky774 I have found the cause of the failure of tests as pointed by you. As of right now I have added self._validate_params method in the partial_fit method of the MLPClassifier estimator.

@Jitensid Jitensid changed the title [WIP] validate parameters for MLPRregressor and MLPClassifier [MRG] validate parameters for MLPRregressor and MLPClassifier Jul 2, 2022
@Jitensid Jitensid changed the title [MRG] validate parameters for MLPRregressor and MLPClassifier [WIP] validate parameters for MLPRregressor and MLPClassifier Jul 2, 2022
…egressor_and_mlpclassifier_uses_validate_params
@Jitensid Jitensid changed the title [WIP] validate parameters for MLPRregressor and MLPClassifier [MRG] validate parameters for MLPRregressor and MLPClassifier Jul 6, 2022
@jeremiedbb
Copy link
Member

I pushed a small refactoring to avoid repeated validation in partial_fit, and fixes some small mistakes I found in the constraint definitions.

@Jitensid
Copy link
Contributor Author

Jitensid commented Jul 6, 2022

I pushed a small refactoring to avoid repeated validation in partial_fit, and fixes some small mistakes I found in the constraint definitions.

Thank you

@glemaitre glemaitre changed the title [MRG] validate parameters for MLPRregressor and MLPClassifier MAINT validate parameters for MLPRregressor and MLPClassifier Jul 6, 2022
@@ -1606,3 +1563,25 @@ def _validate_input(self, X, y, incremental, reset):
if y.ndim == 2 and y.shape[1] == 1:
y = column_or_1d(y, warn=True)
return X, y

@available_if(lambda est: est._check_solver)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid the lambda here. It will not be possible to pickle then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine after #23077

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@glemaitre glemaitre merged commit 0206d3e into scikit-learn:main Jul 6, 2022
@glemaitre
Copy link
Member

Thanks @Jitensid

Harsh14901 pushed a commit to Harsh14901/scikit-learn that referenced this pull request Jul 6, 2022
ogrisel pushed a commit to ogrisel/scikit-learn that referenced this pull request Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants