Skip to content
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

[R-package] raise an informative error when custom objective produces incorrect output (fixes #5323) #5329

Merged
merged 3 commits into from
Jul 12, 2022

Conversation

jmoralez
Copy link
Collaborator

Adds a check to verify that the shapes of grad and hess are correct (same as the number of predictions) to raise an informative error when they're not. This is similar to the one added in #4815 for the python-package.

Fixes #5323.

@jmoralez jmoralez requested a review from jameslamb as a code owner June 27, 2022 17:04
@jmoralez jmoralez added the fix label Jun 27, 2022
Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

Very nice, thanks for this!

I'm not worried about the performance overhead of doing this on every iteration, since:

  • length() should be O(1)
  • users already should expect training with custom R code to be slower than using the built-in objectives

I just left one suggestion about the language in the error messages.

n_hess <- length(gpair$hess)
n_preds <- length(preds)
if (n_grad != n_preds) {
stop(sprintf("grad should have length %d, got %d", n_preds, n_grad))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
stop(sprintf("grad should have length %d, got %d", n_preds, n_grad))
stop(sprintf("Expected custom objective function to return grad with length %d, got %d.", n_preds, n_grad))

Can we please modify this error message (and the one about the hessian) to make it clearer to users that their custom objective function's implementation is the most likely cause?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 84e811e

Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

works for me! thanks

@jameslamb jameslamb merged commit 44fe591 into master Jul 12, 2022
@jameslamb jameslamb deleted the r-custom-obj-checks branch July 12, 2022 00:23
@jameslamb jameslamb mentioned this pull request Oct 7, 2022
40 tasks
@github-actions
Copy link

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 Aug 19, 2023
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.

[R-package] Provide more informative error when custom objective function returns data with incorrect shape
2 participants