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

Skip to content

MAINT validate parameters in TSNE - #23845

Merged
jeremiedbb merged 10 commits into
scikit-learn:mainfrom
chalulu:tsne_validate_params#23462
Jul 20, 2022
Merged

MAINT validate parameters in TSNE#23845
jeremiedbb merged 10 commits into
scikit-learn:mainfrom
chalulu:tsne_validate_params#23462

Conversation

@chalulu

@chalulu chalulu commented Jul 6, 2022

Copy link
Copy Markdown
Contributor

Reference Issues/PRs
Updated TSNE to use parameter constraints as part of #23462.

What does this implement/fix? Explain your changes.

  • TSNE now has parameter constraints added.
  • _validate_parameters is now the first step in fit and fit_transform call.
  • simple params validations are removed.

Any other comments?

  • First contribution!
  • "n_iter_without_progress" parameter constraint is set to [Integral], but it seems unlikely to set it to a negative value.
  • In the doc, the "metric" parameter can be set to 'one of the options allowed by scipy.spatial.distance.pdist', but some metrics available in the scipy doc seems not available in scikit (jensenshannon, kulczynski1). For the constraint of this parameter I used metrics.pairwise._VALID_METRICS.
  • In test_t_tsne.py it is written that the 'n_iter' parameter should be "at least 200". In _t_sne.py it is written that it should be at least 250. For the constraint of this parameter I used the value 250.

@jeremiedbb jeremiedbb added No Changelog Needed Validation related to input validation labels Jul 6, 2022
@glemaitre glemaitre changed the title Tsne validate params#23462 MAINT validate parameters in TSNE Jul 6, 2022

@Micky774 Micky774 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey there @chalulu, thank you for the PR! So far most of it looks good, just some details regarding domains and hidden constraints. Please let me know if you have any questions or concerns :)

Comment thread sklearn/manifold/_t_sne.py Outdated
Comment thread sklearn/manifold/_t_sne.py
Comment thread sklearn/manifold/_t_sne.py Outdated
Comment thread sklearn/manifold/_t_sne.py Outdated
Comment thread sklearn/manifold/_t_sne.py Outdated
"min_grad_norm": [Real],
"metric": [StrOptions(set(_VALID_METRICS) | {"precomputed"}), callable],
"metric_params": [dict, None],
"init": [StrOptions({"pca", "random", "warn"}), np.ndarray],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here about hiding "warn". Note you'll have to format this properly.

Suggested change
"init": [StrOptions({"pca", "random", "warn"}), np.ndarray],
"init": [StrOptions({"pca", "random"}), Hidden(StrOptions({"warn"})), np.ndarray],

Comment thread sklearn/manifold/_t_sne.py Outdated
Comment thread sklearn/manifold/_t_sne.py Outdated
Comment thread sklearn/manifold/_t_sne.py Outdated
@chalulu

chalulu commented Jul 7, 2022

Copy link
Copy Markdown
Contributor Author

@Micky774 Thank you for your comments and suggestions :)

@Micky774

Micky774 commented Jul 7, 2022

Copy link
Copy Markdown
Contributor

Currently failing the test_estimators[TSNE()-check_fit2d_1sample] test, which makes sense because: perplexity should be strictly less than the number of samples (one), but also no less than one. In this case there's no valid value of perplexity to satisfy a one-sample fit. Should we consider excusing TSNE from this check? @jeremiedbb @glemaitre

@jeremiedbb

Copy link
Copy Markdown
Member

Alternatively, we could allow perplexity < 1 to not break backward compat and reconsider this issue when changing the lower bound in a separate PR

@Micky774

Micky774 commented Jul 7, 2022

Copy link
Copy Markdown
Contributor

Alternatively, we could allow perplexity < 1 to not break backward compat and reconsider this issue when changing the lower bound in a separate PR

Sounds good, no need to hold up this PR

@chalulu

chalulu commented Jul 8, 2022

Copy link
Copy Markdown
Contributor Author

I changed the interval of the perplexity to allow perplexity < 1.

@Micky774
When running utils.estimator_checks I noticed that the current version throws an error during the test "check_estimators_dtypes". As far as I understood it is related to this PR . The test uses X_train of size 20, which is lower than the default perplexity (=30), hence the function "_check_params_vs_input" raises an error. Is it the expected behavior of the test?

@Micky774

Micky774 commented Jul 8, 2022

Copy link
Copy Markdown
Contributor

When running utils.estimator_checks I noticed that the current version throws an error during the test "check_estimators_dtypes". As far as I understood it is related to this PR . The test uses X_train of size 20, which is lower than the default perplexity (=30), hence the function "_check_params_vs_input" raises an error. Is it the expected behavior of the test?

I'm not sure what you're referring to here -- the tests pass on both main and this PR. Under what circumstances are you getting this error?

@chalulu

chalulu commented Jul 9, 2022

Copy link
Copy Markdown
Contributor Author

I'm not sure what you're referring to here -- the tests pass on both main and this PR. Under what circumstances are you getting this error?

I think it is a misunderstanding from my side.
I was checking this doc (under "Rolling your own estimator") and tried the following code:

from sklearn.utils.estimator_checks import check_estimator
from sklearn.manifold import TSNE
check_estimator(TSNE())

which throws the error below:
ValueError: perplexity must be less than n_samples

But when running the tests with pytest:
pytest -vl scikit-learn/sklearn/tests/test_common.py
the tests pass.

@Micky774

Micky774 commented Jul 9, 2022

Copy link
Copy Markdown
Contributor

Actually that's a good catch! We should fix that typo in its own PR (would you be interested 👀).

Regarding why our common tests pass: before running that check we set the parameters of the estimators to values that won't raise errors for it, e.g. in this case we actually explicity set the perplexity to something lower for the reasons you observed!

Edit: for clarification, by "fix that typo" I mean replace TSNE with another estimator that doesn't need custom-set parameters for the check.

@chalulu

chalulu commented Jul 9, 2022

Copy link
Copy Markdown
Contributor Author

Edit: for clarification, by "fix that typo" I mean replace TSNE with another estimator that doesn't need custom-set parameters for the check

I am not sure to understand this. In the doc "LinearSVC" estimator is used, which does not throw any error. I just tried the same piece of code with TSNE estimator and noticed the error. Where should I replace TSNE with another estimator?

I also noticed that in earlier versions of sklearn (before the implementation of the _check_params_vs_input function), the code check_estimator(TSNE()) did not throw any error.

@Micky774

Micky774 commented Jul 9, 2022

Copy link
Copy Markdown
Contributor

I am not sure to understand this. In the doc "LinearSVC" estimator is used, which does not throw any error. I just tried the same piece of code with TSNE estimator and noticed the error. Where should I replace TSNE with another estimator?

Ah ignore me I was hasty and falsely thought that TSNE was used in the docs. All is good.

Also, the current failing CI is unrelated to this PR

@chalulu

chalulu commented Jul 9, 2022

Copy link
Copy Markdown
Contributor Author

Got it!

@Micky774

Copy link
Copy Markdown
Contributor

Hey there @chalulu, could you merge w/ main and push to prompt the CI/CD once more? I think the current error is not related to your PR.

@chalulu

chalulu commented Jul 15, 2022

Copy link
Copy Markdown
Contributor Author

@Micky774 I merged with main and pushed again.
Thank you for your review/feedback, I'm looking forward to trying to contribute on more issues.

@glemaitre
glemaitre self-requested a review July 20, 2022 14:30

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks @chalulu

@jeremiedbb
jeremiedbb merged commit 5053802 into scikit-learn:main Jul 20, 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.

3 participants