-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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] Provide more informative error when custom objective function returns data with incorrect shape #5323
Comments
Thanks for using LightGBM and for your report! In the future, please try to provide a minimal, reproducible example if possible. Many issues using Happy to say, though, that for this I was able to create such an example that reproduces this error. Ran the following code today on my Mac, using R 4.1.1, compiling with library(lightgbm)
data(EuStockMarkets)
stockDF <- as.data.frame(EuStockMarkets)
# create a Dataset
feature_names <- c("SMI", "CAC", "FTSE")
target_name <- "DAX"
X_train <- data.matrix(stockDF[, feature_names])
y_train <- stockDF[[target_name]]
# colnames are stored in private$colnames when initializing Dataset
dtrain <- lightgbm::lgb.Dataset(
data = X_train
, label = y_train
, colnames = feature_names
)
MAELoss <- function(preds, dtrain) {
labels <- attr(dtrain, 'label')
grad <- abs(preds - labels)
hess <- rep(0, length(preds))
return(list(hess = hess,grad = grad))
}
model <- lightgbm::lgb.train(
params = list()
, data = dtrain
, verbose= 1
, nrounds = 10
, obj = MAELoss
) On latest
And on
It's pretty surprising, since this project has unit tests on the use of custom objective functions passed to LightGBM/R-package/tests/testthat/test_custom_objective.R Lines 50 to 63 in 6b56a90
Those tests are passing for me locally, and in this project's many CI jobs. |
Isn't |
ahhhh RIGHT AFTER I posted that, I think I found the root cause. You cannot use library(lightgbm)
data(EuStockMarkets)
stockDF <- as.data.frame(EuStockMarkets)
feature_names <- c("SMI", "CAC", "FTSE")
target_name <- "DAX"
X_train <- data.matrix(stockDF[, feature_names])
y_train <- stockDF[[target_name]]
dtrain <- lightgbm::lgb.Dataset(
data = X_train
, label = y_train
, colnames = feature_names
, free_raw_data = FALSE
)
dtrain$construct() attr(dtrain, 'label')
# NULL Instead, please use dtrain$get_field("label")
# [1] 1628.75 1613.63 1606.51 1621.04 1618.16 1610.61 ... When I change your custom objective function to access the label that way, training proceeds successfully. I'm going to leave this issue open as a bug, since there should be an opportunity for |
ha @jmoralez you were faster than me. Yeah it's exactly that. |
Thank you guys!!! |
This issue 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. |
Description
I try to run a custom loss function but get the error:
booster$update(fobj = fobj) : No object function provided
Reproducible example
not completely reproducible in terms of entire data, but the code looks like this
Environment info
lgbm 3.3.2
install.packages("lightgbm")
Windows 10
R 4.2
if i change the 'MAELoss' to "regression"
it works ok, it just won't accept my function.
Thanks!
The text was updated successfully, but these errors were encountered: