-
-
Notifications
You must be signed in to change notification settings - Fork 26k
ENH add exponential loss #25965
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
ENH add exponential loss #25965
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,12 +28,14 @@ | |
CyHalfTweedieLossIdentity, | ||
CyHalfBinomialLoss, | ||
CyHalfMultinomialLoss, | ||
CyExponentialLoss, | ||
) | ||
from .link import ( | ||
Interval, | ||
IdentityLink, | ||
LogLink, | ||
LogitLink, | ||
HalfLogitLink, | ||
MultinomialLogit, | ||
) | ||
from ..utils import check_scalar | ||
|
@@ -817,6 +819,11 @@ class HalfBinomialLoss(BaseLoss): | |
logistic regression, y = [0, 1]. | ||
If you add `constant_to_optimal_zero` to the loss, you get half the | ||
Bernoulli/binomial deviance. | ||
|
||
More details: Inserting the predicted probability y_pred = expit(raw_prediction) | ||
in the loss gives the well known:: | ||
|
||
loss(x_i) = - y_true_i * log(y_pred_i) - (1 - y_true_i) * log(1 - y_pred_i) | ||
""" | ||
|
||
def __init__(self, sample_weight=None): | ||
|
@@ -994,6 +1001,79 @@ def gradient_proba( | |
) | ||
|
||
|
||
class ExponentialLoss(BaseLoss): | ||
"""Exponential loss with (half) logit link, for binary classification. | ||
|
||
This is also know as boosting loss. | ||
|
||
Domain: | ||
y_true in [0, 1], i.e. regression on the unit interval | ||
y_pred in (0, 1), i.e. boundaries excluded | ||
betatim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Link: | ||
y_pred = expit(2 * raw_prediction) | ||
|
||
For a given sample x_i, the exponential loss is defined as:: | ||
|
||
loss(x_i) = y_true_i * exp(-raw_pred_i)) + (1 - y_true_i) * exp(raw_pred_i) | ||
|
||
See: | ||
- J. Friedman, T. Hastie, R. Tibshirani. | ||
"Additive logistic regression: a statistical view of boosting (With discussion | ||
and a rejoinder by the authors)." Ann. Statist. 28 (2) 337 - 407, April 2000. | ||
https://doi.org/10.1214/aos/1016218223 | ||
- A. Buja, W. Stuetzle, Y. Shen. (2005). | ||
"Loss Functions for Binary Class Probability Estimation and Classification: | ||
Structure and Applications." | ||
|
||
Note that the formulation works for classification, y = {0, 1}, as well as | ||
"exponential logistic" regression, y = [0, 1]. | ||
Note that this is a proper scoring rule, but without it's canonical link. | ||
|
||
More details: Inserting the predicted probability | ||
y_pred = expit(2 * raw_prediction) in the loss gives:: | ||
|
||
loss(x_i) = y_true_i * sqrt((1 - y_pred_i) / y_pred_i) | ||
+ (1 - y_true_i) * sqrt(y_pred_i / (1 - y_pred_i)) | ||
""" | ||
|
||
def __init__(self, sample_weight=None): | ||
super().__init__( | ||
closs=CyExponentialLoss(), | ||
link=HalfLogitLink(), | ||
n_classes=2, | ||
) | ||
self.interval_y_true = Interval(0, 1, True, True) | ||
|
||
def constant_to_optimal_zero(self, y_true, sample_weight=None): | ||
# This is non-zero only if y_true is neither 0 nor 1. | ||
term = -2 * np.sqrt(y_true * (1 - y_true)) | ||
if sample_weight is not None: | ||
term *= sample_weight | ||
return term | ||
|
||
def predict_proba(self, raw_prediction): | ||
"""Predict probabilities. | ||
|
||
Parameters | ||
---------- | ||
raw_prediction : array of shape (n_samples,) or (n_samples, 1) | ||
Raw prediction values (in link space). | ||
|
||
Returns | ||
------- | ||
proba : array of shape (n_samples, 2) | ||
Element-wise class probabilities. | ||
""" | ||
# Be graceful to shape (n_samples, 1) -> (n_samples,) | ||
if raw_prediction.ndim == 2 and raw_prediction.shape[1] == 1: | ||
raw_prediction = raw_prediction.squeeze(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the coverage warning from Codecov will disappear once the support for this loss is added to GLM and HGBT. Is that right? |
||
proba = np.empty((raw_prediction.shape[0], 2), dtype=raw_prediction.dtype) | ||
proba[:, 1] = self.link.inverse(raw_prediction) | ||
proba[:, 0] = 1 - proba[:, 1] | ||
return proba | ||
|
||
|
||
_LOSSES = { | ||
"squared_error": HalfSquaredError, | ||
"absolute_error": AbsoluteError, | ||
|
@@ -1003,4 +1083,5 @@ def gradient_proba( | |
"tweedie_loss": HalfTweedieLoss, | ||
"binomial_loss": HalfBinomialLoss, | ||
"multinomial_loss": HalfMultinomialLoss, | ||
"exponential_loss": ExponentialLoss, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.