You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RFC make response / inverse link / activation function official #29169
With questions like #29163 and with the private loss functions #15123 (almost everywhere) in place, I would like to discuss to make the inverse link function public.
Models like LogisticRegression or HistGradientBoostingRegressor(loss="poisson") have predictions like inverse_link(raw_prediction(X)) where raw_prediction(X) is the prediction in "link space", e.g. linear predictor ("eta") for linear models.
In line with the most recent nomenclature of HistGradientBoosting*, I propose the following public API for regressors and classifiers:
raw_predict(X)
response_function(y_raw) or activation_function(y_raw)
link_function(y_obs)
Alternatives:
estimator.link is a link object which has 2 methods named like above.
isn't raw_predictions the same as decision_function in effect?
activation_function sounds NN like, so maybe not appropriate.
Overall, I don't think I like the idea of adding these at the estimator level. I personally don't even understand what link_function or response_function would do. However, I'd be happy to have estimator.link documented properly, and then have methods of estimator.link as public methods for people who really want to dig in.
The link function is the inverse of what you usually apply in reality:
Binary Logistic: return of predict_proba = expit(raw) and link function is logit
(Log-)-Poisson regression: return of predict = exp(raw) and link function is log
isn't raw_predictions the same as decision_function in effect?
For classifiers, in effect, yes. But not API-wise. Also, decision_function is one of the greates misnomers of our API, IMO. The existance of decision_function is also not guaranteed from what I read in our docs:
Classification algorithms usually also offer a way to quantify certainty of a prediction, either using decision_function or predict_proba
Site note: I guess that for the softmax inverse link used for the multiclass log-loss, we would expose softmax as inverse link and log as (forward) link since softmax is not bijective.
Main comment to the core of the RFC:
I think would intuitively favor making estimator.loss.link public instead of introducing new methods on the estimator itself, but we would still need to expose a raw_predict method on the estimator to make that useful.
I agree that decision_function is a really bad name in retrospect, especially now that we started introducing cost-sensitive learning tools such as TunedThresholdClassifierCV where the "decision" would better represent the thresholded predict_proba or decision_function value of a binary classifier, for instance.
So we could introduce raw_predict (at least for classifiers, regressors, not sure about clustering algorithms).
Then we could soft deprecate (e.g. stop using and hide in the doc any occurrence of decision_function in favor of raw_predict for classifiers, but keep the decision_function as a pure backward compat alias to avoid breaking user code).
We would definitely a SLEP for such an impacting public API change though.
Note that the question in #29163 should first be addressed by updating our user guide (and maybe cross-reference it from the docstring for the predict (for regressors) and predict_proba (for classifiers) methods.
I'm unsure of adding expose the loss objects through estimator.loss. Can the link functions be a regular functions and in the docstring of raw_predict we state how to generate predict or predict_proba?
In principle, loss and link function are independent. It's only an efficient implementation that needs to merge them together.
As written in the RFC, having link and inverse link function available on the estimator would help several use cases and reduce confusion.
I think adding three new methods estimators is pretty big API update. I'm trying to find a path to get some momentum, like standardizing on raw_predict.
After raw_predict is out, then there is a standard way to do this:
log_reg=LogisticRegression().fit(X, y)
raw_pred=log_reg.raw_predict(X)
# If they want to get the actual predctions, they need to know the inverse link:y_pred=expit(raw_pred)
I think this is already a net win.
Afterwards, if we want to go further, we can extend the API:
But raw_predict without something like response_function/inverse is quite meaningless. Maybe, we don't need the link function itself, only it's inverse.
EDIT: Ok, starting with only raw_predict makes sense and already enable new things.
I think adding three new methods estimators is pretty big API update.
Yes, it is. Again "but" those are missing from the beginnings as they are quite fundamental (for any loss except the squared error and quantile loss).
In the short term, I'm thinking of having the inverse link function in the docstring, so the raw_predict is meaningful:
defraw_predict(self, X):
"""Return the raw prediction. `predict`'s output is the same as `inverse_link(raw_prediction)`, where the inverse link function is `scipy.special.expit`. """
For estimators that can adjust their loss, we'll list out the inverse link functions.
Reacted by Christian Lorentzen, Adrin Jalali and Olivier Grisel
if we want to expose raw_predict for all supervised estimators in scikit-learn or only for models with an explicit link function (GLMs, MLPs, Gradient Boosting...);
how it is defined for non-probabilistic classifiers such as SVC (and variants): is it always mapped to decision_function for a classifier?
And yes, for non-probabilistic classifiers, it returns the same as decision_function. We also discussed deprecating decision_function (with a long cycle). No hard objections were mentioned. I would be +1 for that.
Activity
adrinjalali commentedon Jun 4, 2024
isn't
raw_predictionsthe same asdecision_functionin effect?activation_functionsounds NN like, so maybe not appropriate.Overall, I don't think I like the idea of adding these at the estimator level. I personally don't even understand what
link_functionorresponse_functionwould do. However, I'd be happy to haveestimator.linkdocumented properly, and then have methods ofestimator.linkas public methods for people who really want to dig in.lorentzenchr commentedon Jun 4, 2024
The link function is the inverse of what you usually apply in reality:
return of predict_proba = expit(raw)and link function islogitreturn of predict = exp(raw)and link function islogFor classifiers, in effect, yes. But not API-wise. Also,
decision_functionis one of the greates misnomers of our API, IMO. The existance of decision_function is also not guaranteed from what I read in our docs:ogrisel commentedon Jun 11, 2024
Site note: I guess that for the softmax inverse link used for the multiclass log-loss, we would expose
softmaxas inverse link andlogas (forward) link since softmax is not bijective.Main comment to the core of the RFC:
I think would intuitively favor making
estimator.loss.linkpublic instead of introducing new methods on the estimator itself, but we would still need to expose araw_predictmethod on the estimator to make that useful.I agree that
decision_functionis a really bad name in retrospect, especially now that we started introducing cost-sensitive learning tools such asTunedThresholdClassifierCVwhere the "decision" would better represent the thresholdedpredict_probaordecision_functionvalue of a binary classifier, for instance.So we could introduce
raw_predict(at least for classifiers, regressors, not sure about clustering algorithms).Then we could soft deprecate (e.g. stop using and hide in the doc any occurrence of
decision_functionin favor ofraw_predictfor classifiers, but keep thedecision_functionas a pure backward compat alias to avoid breaking user code).We would definitely a SLEP for such an impacting public API change though.
ogrisel commentedon Jun 11, 2024
Note that the question in #29163 should first be addressed by updating our user guide (and maybe cross-reference it from the docstring for the
predict(for regressors) andpredict_proba(for classifiers) methods.thomasjpfan commentedon Jun 14, 2024
I think
raw_predictmakes sense.I'm unsure of adding expose the loss objects through
estimator.loss. Can the link functions be a regular functions and in the docstring ofraw_predictwe state how to generatepredictorpredict_proba?lorentzenchr commentedon Sep 28, 2024
In principle, loss and link function are independent. It's only an efficient implementation that needs to merge them together.
As written in the RFC, having link and inverse link function available on the estimator would help several use cases and reduce confusion.
thomasjpfan commentedon Sep 28, 2024
I think adding three new methods estimators is pretty big API update. I'm trying to find a path to get some momentum, like standardizing on
raw_predict.After
raw_predictis out, then there is a standard way to do this:I think this is already a net win.
Afterwards, if we want to go further, we can extend the API:
lorentzenchr commentedon Sep 28, 2024
But
raw_predictwithout something likeresponse_function/inverseis quite meaningless. Maybe, we don't need thelinkfunction itself, only it's inverse.EDIT: Ok, starting with only
raw_predictmakes sense and already enable new things.Yes, it is. Again "but" those are missing from the beginnings as they are quite fundamental (for any loss except the squared error and quantile loss).
thomasjpfan commentedon Oct 9, 2024
In the short term, I'm thinking of having the inverse link function in the docstring, so the
raw_predictis meaningful:For estimators that can adjust their
loss, we'll list out the inverse link functions.lorentzenchr commentedon Feb 18, 2025
Another use case for
raw_predictis #28574 which needs access to logits.ogrisel commentedon Mar 6, 2025
I think we would need to clarify:
raw_predictfor all supervised estimators in scikit-learn or only for models with an explicit link function (GLMs, MLPs, Gradient Boosting...);decision_functionfor a classifier?lorentzenchr commentedon Mar 6, 2025
We discussed it in the monthly meeting https://github.com/scikit-learn/administrative/blob/master/monthly_meetings/2025-02-24.md and agreed on having a
raw_predict, officially.And yes, for non-probabilistic classifiers, it returns the same as
decision_function. We also discussed deprecatingdecision_function(with a long cycle). No hard objections were mentioned. I would be +1 for that.