Skip to content

ENH more stable gradient of CrossEntropy - #6327

Merged
shiyu1994 merged 9 commits into
lightgbm-org:masterfrom
lorentzenchr:stable_cross_entropy
Feb 22, 2024
Merged

shiyu1994 merged 9 commits into
lightgbm-org:masterfrom
lorentzenchr:stable_cross_entropy

Conversation

@lorentzenchr

@lorentzenchr lorentzenchr commented Feb 18, 2024 •

Copy link
Copy Markdown
Contributor

Similar to scikit-learn/scikit-learn#28048.

There is a small runtime cost to pay, but gradient computation is not the main bottleneck of histogram based gradient boosting.

@jameslamb

Copy link
Copy Markdown
Member

Thanks for this! I'll defer to @shiyu1994 and @guolinke to review.

Until then, can you please update this to the latest master?

@shiyu1994 shiyu1994 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the contribution. I just left a few comments about the correction of hessian computation.

Comment thread src/objective/xentropy_objective.hpp Outdated
if (score[i] > -37.0) {
const double exp_tmp = std::exp(-score[i]);
gradients[i] = static_cast<score_t>(((1.0f - label_[i]) - label_[i] * exp_tmp) / (1.0f + exp_tmp));
hessians[i] = static_cast<score_t>(exp_tmp / (1 + exp_tmp) * (1 + exp_tmp));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The / followed by a * simply returns exp_tmp, which is not the expected hessian.

Suggested change
hessians[i] = static_cast<score_t>(exp_tmp / (1 + exp_tmp) * (1 + exp_tmp));
hessians[i] = static_cast<score_t>(exp_tmp / ((1 + exp_tmp) * (1 + exp_tmp)));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it possible that hessians[i] = static_cast<score_t>(exp_tmp / (1 + exp_tmp) / (1 + exp_tmp)); could be more numerically stable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

First for all, yes I forgot the parenthesis. Thanks for spotting it. It is surprising that still all the tests pass (with this bug).

Then (exp_tmp / (1 + exp_tmp) / (1 + exp_tmp)) is more numerical stable in the sense that it could prevent overflow. But exp_tmp > exp(37) = 1e16 and squaring that is within even single precision (3e38), and note that exp_tmp is even double precision.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. Could you also fix the hessian calculation in the else branch?

} else {
const double exp_tmp = std::exp(score[i]);
gradients[i] = static_cast<score_t>(exp_tmp - label_[i]);
hessians[i] = static_cast<score_t>(exp_tmp);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
hessians[i] = static_cast<score_t>(exp_tmp);
hessians[i] = static_cast<score_t>(exp_tmp / ((1 + exp_tmp) * (1 + exp_tmp)));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is not needed as exp_tmp < 1e-16 is tiny and (1 + exp_tmp) is just 1. Otherwise stated, the implemented formula is the 1st order Taylor series in exp_tmp.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see. That makes sense.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But maybe it would still be better to write the original calculation formula explicitly to avoid ambiguity?

@lorentzenchr lorentzenchr Feb 20, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What do you mean with "ambiguity"?
It would not avoid the branch and is a tiny bit more efficient.

Comment thread src/objective/xentropy_objective.hpp Resolved
Comment thread src/objective/xentropy_objective.hpp Resolved
@shiyu1994
shiyu1994 merged commit 894066d into lightgbm-org:master Feb 22, 2024
@lorentzenchr
lorentzenchr deleted the stable_cross_entropy branch February 24, 2024 12:15
@jameslamb jameslamb mentioned this pull request May 1, 2024
33 tasks done
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity since it was closed.
To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants