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] session crashes when serializing and deserializing model #4415

Closed
verajosemanuel opened this issue Jun 28, 2021 · 5 comments
Closed

Comments

@verajosemanuel
Copy link

verajosemanuel commented Jun 28, 2021

I trained a model in R using LightGBM (treesnip) and saved using common saveRDS command.

To my surprise, when trying to predict some data after reading the saved model, readRDS seems to work but script&predict is crashing.

Read this issue #4208 regarding some similar issue but I am not sure that is the cause.

is there any safe procedure to save my model fot later use in R?

regards

@jameslamb
Copy link
Collaborator

It is hard to say for sure since you haven't provided a reproducible example showing exactly what you tried, but based on the information you've provided, #4208 probably describe problems you're facing using saveRDS() / readRDS(). I recommend subscribing to #4208 to be sure you're notified once that issue is resolved.

Until then, you can use the functions lightgbm::saveRDS.lgb.Booster() and lightgbm::readRDS.lgb.Booster() to save a {lightgbm} model object.

You can try these by using the following code:

library(lightgbm)

data(agaricus.train, package = "lightgbm")

dtrain <- lightgbm::lgb.Dataset(
    agaricus.train$data
    , label = agaricus.train$label
)
bst <- lightgbm::lgb.train(
    params = list(
        "learning_rate" = 0.05
    )
    , data = dtrain
    , nrounds = 10L
    , obj = "binary"
)

model_file <- "test-model.rds"
saveRDS.lgb.Booster(
    object = bst
    , file = model_file
)

rm(list = ls())

After running that code, restart your R session and re-load the model. You should see that you're able to successfully predict on new data.

library(lightgbm)

model_file <- "test-model.rds"

data(agaricus.test, package = "lightgbm")
bst <- lightgbm::readRDS.lgb.Booster(
    file = model_file
)

preds <- predict(bst, data = agaricus.test$data)
print(preds)

For more, see:

NOTE: I'm not familiar with how {treesnip} works. If it returns its own object which wraps a {lightgbm} Booster, then until #4208 is resolved you will have to extract the Booster from that {treesnip}-specific object to use the example code above.

@jameslamb jameslamb changed the title [question] serialize and deserialize model [R-package] session crashes when serializing and deserializing model Jun 29, 2021
@verajosemanuel
Copy link
Author

verajosemanuel commented Jun 29, 2021

that's correct! just the issue I was having. Thanks for the workaround! Sadly, the code you provided crashed my R environment

@jameslamb
Copy link
Collaborator

Sadly, the code you provided crashed my R environment

How did you install {lightgbm} and what version of it are you using? As I mentioned in #4007 (comment), there have been several stability fixes to the R package that have not yet been released.

If you need further help, please provide a reproducible example.

@no-response
Copy link

no-response bot commented Jul 29, 2021

This issue has been automatically closed because it has been awaiting a response for too long. When you have time to to work with the maintainers to resolve this issue, please post a new comment and it will be re-opened. If the issue has been locked for editing by the time you return to it, please open a new issue and reference this one. Thank you for taking the time to improve LightGBM!

@no-response no-response bot closed this as completed Jul 29, 2021
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
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

3 participants