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

Reload R model object imported from different environment without using a temp file #432

Closed
JesseLimtiaco opened this issue Apr 18, 2017 · 4 comments

Comments

@JesseLimtiaco
Copy link
Contributor

Hello,

My question is similar to the issue brought up in 296:

#296

The R6 objects are environment-type variables and my use case transfers these R6 objects between different environments/machines instead of RDS files. The solution to make the saveRDS.lgb.Booster and readRDS.lgb.Booster works when transferring the RDS files but I was hoping to avoid creating a temp file in the R session.

readRDS.lgb.Booster looks like this where it reads off the temp file that is written to file storage.

readRDS.lgb.Booster <- function (file = "", refhook = NULL)
{
object <- readRDS(file = file, refhook = refhook)
if (!is.na(object$raw)) {
temp <- tempfile()
write(object$raw, temp)
object2 <- lgb.load(temp)
file.remove(temp)
object2$best_iter <- object$best_iter
object2$record_evals <- object$record_evals
return(object2)
}
else {
return(object)
}
}

Can we make something that looks like this that passes in an R6 object that already has a saved $raw attribute?

reload.lgb.Booster <- function (object)
{
if (!is.na(object$raw)) {
object2 <- lgb.load(object$raw) # initializes new model from object$raw data instead of text file
object2$best_iter <- object$best_iter
object2$record_evals <- object$record_evals
return(object2)
}
else {
return(object)
}
}

Thanks!

@Laurae2
Copy link
Contributor

Laurae2 commented Apr 18, 2017

Can you save the model and the best iteration / metric log separately? If yes, then this will allow you to skip this issue. You can then write the metric log / best iteration directly on the newly created environment (model).

ping @guolinke if there is a way to save/load model from a variable containing the model string in memory instead of a file ?

@JesseLimtiaco
Copy link
Contributor Author

Yes this implementation would be very helpful. Can we add analogous __load_model_from_string and __save_model_to_string functions to the Booster class in the R package? Right now it seems the only way to initialize in R is to make a new model or read from model file:

Booster non existent

    stop("lgb.Booster: Need at least either training dataset or model file to create booster instance")

@JesseLimtiaco
Copy link
Contributor Author

Solved with pull request: Adding ability to load model from string in R #472

@lock lock bot locked as resolved and limited conversation to collaborators Mar 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants