Skip to content

[WIP] [POC] FEA public objective function methods in estimators - #28505

Draft
jeremiedbb wants to merge 7 commits into
scikit-learn:mainfrom
jeremiedbb:obj_func_nmf
Draft

jeremiedbb wants to merge 7 commits into
scikit-learn:mainfrom
jeremiedbb:obj_func_nmf

Conversation

@jeremiedbb

Copy link
Copy Markdown
Member

towards #28169

Not sure where to put the ObjectiveFunction dataclass. I put it in sklearn.base for now but open to suggestions.
The design is not final. This PR is there to help discussing the design in the linked issue #28169 where discussion regarding the general design of this API should be discussed.

@github-actions

github-actions Bot commented Feb 22, 2024 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: f40f5ad. Link to the linter CI: here

@ogrisel ogrisel 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.

I put it in sklearn.base for now but open to suggestions.

I think it's fine this way.

Overall, I think the design look good but I am not sure what to do of this PR in the short term. It's good to have a concrete proof of concept to help back the redaction of a SLEP on this extension to the public estimator API of scikit-learn.

I think however that it would be worth doing the same exercise for at least a few unrelated estimators (e.g. LogisticRegression, DecisionTreeClassifier, HistGradientBoostingClassifier) to check that the design hold in a generic enough way. I don't know if it's best to draft those as part of this PR or in dedicated draft PRs targeting this one (to reuse the base dataclass) or using a feature branch to host additional common elements shall the need arise?

Some more inline feedback below.

Comment thread sklearn/base.py Outdated

normalized : bool
Whether each term and the total value of the objective function is
per-sample or not.

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.

Are there cases where we want normalized=False? Maybe for backward compat with existing objective functions such as KMeans' inertia? I general I think we should always use per-sample normalization to make those values easier to interpret.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Personally I always normalize the objective function, and I would not even add this parameter here.
The reason I added it is that usually in the literature and even in our docstrings and user guides the formula of the objective function is not normalized, so I thought that it might be less confusing for some people. (although they just have to multiply by n_samples).

If we're all on the same page, I'd be in favor of not having the param and always normalize.

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.

I suppose that if the docstring is explicit enough, it's fine to always normalize by sample size.

Comment thread sklearn/decomposition/_nmf.py Resolved
Comment thread sklearn/decomposition/tests/test_nmf.py Resolved
Comment thread sklearn/decomposition/tests/test_nmf.py Outdated
+ alpha_H * l1_ratio * n_samples * H.sum()
+ 0.5 * alpha_W * (1 - l1_ratio) * n_features * (W**2).sum()
+ 0.5 * alpha_H * (1 - l1_ratio) * n_samples * (H**2).sum()
)

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.

Ideally, one might also want to check that the objective function is actually monotonically decreasing at each iteration but this kind of tests would only be easy to write once we have the callbacks API.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We'll be able to add such tests easily when we have the callbacks

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.

Maybe add a todo?

@jeremiedbb

Copy link
Copy Markdown
Member Author

I think however that it would be worth doing the same exercise for at least a few unrelated estimators (e.g. LogisticRegression, DecisionTreeClassifier, HistGradientBoostingClassifier) to check that the design hold in a generic enough way

I'm starting to work on that to have a better overview

@jeremiedbb jeremiedbb changed the title [WIP] FEA public objective function method for NMF [WIP] [POC] FEA public objective function methods in estimators Feb 29, 2024

This branch has not been deployed

No deployments
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.

2 participants