[WIP] Test custom obj function#21
Conversation
| custom_obj <- make_obj_mse_cov(rho = rho_val, y_mean = mean(y)) | ||
| # When `obj` is a custom callback we must NOT also set `objective` in | ||
| # params, otherwise lgb.train will reject the unknown name. | ||
| others$objective <- NULL |
There was a problem hiding this comment.
LightGBM takes either objective or obj args, code here, and ultimately collects one of the two as objective
More context on the comment here from the lightbgm source code. Shown here:
# extract any function objects passed for objective or metric
fobj <- NULL
if (is.function(params$objective)) {
fobj <- params$objective
params$objective <- "none"
}Checks to see if if there is a custom function supplied, saves it as fobj to be passed to C later using gradient and hessian, and the "none" value lets lightgbm know that the custom math will be supplied later, rather than it using one of its built in C objective functions.
| custom_obj <- NULL | ||
| if (!is.null(others$objective) && identical(others$objective, "mse_cov")) { | ||
| rho_val <- if (is.null(mse_cov_rho)) 1e-3 else as.numeric(mse_cov_rho) | ||
| custom_obj <- make_obj_mse_cov(rho = rho_val, y_mean = mean(y)) |
There was a problem hiding this comment.
This function ultimately ends up in lightbgm source code here
Where it gets passed to a C program for compuation, expecting a gradient and a hessian, supplied by R/objectives.R
| small_h <- hess < zero_grad_tol | ||
| if (any(small_h)) hess[small_h] <- zero_grad_tol | ||
|
|
||
| list(grad = grad, hess = hess) |
There was a problem hiding this comment.
This list is passed and parsed as gpair in lightbm source code
| stats::predict( | ||
| object$fit, | ||
| as.matrix(new_data), | ||
| type = "raw", |
There was a problem hiding this comment.
This stops the script of dropping a bunch of warnings
| #' passing as the `obj` argument of [lightgbm::lgb.train]. | ||
| #' | ||
| #' @export | ||
| make_obj_mse_cov <- function(rho, y_mean, zero_grad_tol = 1e-6) { |
There was a problem hiding this comment.
The math here is completely borrowed from the source implementation
This PR builds a custom objective function closure to be passed to lightgbm.
Supports model PR: ccao-data/model-res-avm#475