Abner Mácola
Abner Mácola

Reputation: 101

Save and load a lightGBM model in R

I am trying to save and later load a lightGBM model, but I cannot do it. I have already tried saveRDS() and readRDS functions, but when I predict, i get this error:

Error in predictor$predict(data = data, start_iteration = start_iteration, : Attempting to use a Booster which no longer exists. This can happen if you have called Booster$finalize() or if this Booster was saved with saveRDS(). To avoid this error in the future, use saveRDS.lgb.Booster() or Booster$save_model() to save lightgbm Boosters.

If I use the model right after I trained it, or in a session where it is loaded into the workspace, it works fine. The problem is when I try to use the model on another application. Here is how I coded it:

library(tidymodels)
library(lightgbm)
library(bonsai)

TMA_model <- data.frame(
  age = c(50, 45, 60, 55, 70, 34, 55, 48, 58, 42,
          52, 47, 62, 57, 72, 36, 53, 49, 59, 44,
          51, 46, 61, 56, 71, 35, 54, 50, 60, 43), 
  delta_creat = c(1.2, 1.0, 1.5, 1.3, 1.8, 1.2, 1.3, 1.4, 1.1, 1.6,
                  1.2, 1.0, 1.5, 1.3, 1.8, 1.2, 1.3, 1.4, 1.1, 1.6,
                  1.2, 1.0, 1.5, 1.3, 1.8, 1.2, 1.3, 1.4, 1.1, 1.6), 
  max_LDH = c(300, 280, 320, 310, 330, 295, 325, 290, 315, 305,
              305, 315, 290, 320, 300, 325, 280, 330, 310, 295,
              310, 295, 330, 280, 320, 305, 310, 325, 315, 290), 
  min_plat = c(150, 140, 160, 155, 170, 145, 165, 150, 135, 160,
               160, 155, 170, 145, 150, 160, 140, 170, 155, 145,
               155, 145, 170, 140, 160, 150, 155, 160, 165, 135),
  min_hb = c(12, 11.5, 13, 12.5, 14, 11.8, 13.2, 12.2, 12.6, 11.9,
             12.4, 11.7, 13.1, 12.3, 13.5, 11.6, 12.8, 12.1, 13.4, 12.0,
             12.9, 11.6, 13.2, 12.8, 13.0, 11.5, 12.7, 12.3, 12.5, 11.8), 
  max_ast = c(40, 38, 42, 41, 45, 37, 43, 39, 44, 40,
              42, 38, 44, 41, 40, 39, 43, 38, 45, 37,
              44, 37, 42, 38, 41, 40, 43, 39, 44, 38), 
  max_bt = c(37, 37.2, 37.5, 37.3, 37.8, 37.1, 37.4, 37.6, 37.0, 37.7,
             37.2, 37.1, 37.5, 37.4, 37.3, 37.0, 37.6, 37.8, 37.2, 37.4,
             37.7, 37.1, 37.3, 37.8, 37.0, 37.4, 37.2, 37.6, 37.5, 37.1), 
  max_ttap = c(100, 95, 105, 102, 110, 97, 108, 93, 103, 98,
               105, 100, 95, 110, 102, 108, 97, 93, 103, 95,
               100, 97, 105, 93, 108, 102, 110, 98, 103, 100), 
  max_tp = c(12, 12.2, 12.5, 12.3, 12.8, 12.1, 12.4, 12.6, 12.0, 12.7,
             12.3, 12.2, 12.1, 12.8, 12.4, 12.0, 12.6, 12.7, 12.5, 12.3,
             12.1, 12.4, 12.7, 12.8, 12.0, 12.2, 12.3, 12.6, 12.5, 12.4), 
  hypertension = c(1, 0, 1, 1, 0, 0, 1, 0, 1, 0,
                   1, 0, 1, 0, 1, 0, 0, 1, 1, 0,
                   0, 1, 0, 1, 0, 0, 1, 1, 0, 1),  
  MAP = c(90, 92, 88, 91, 89, 93, 87, 94, 86, 90,
          92, 94, 88, 90, 91, 89, 87, 93, 86, 92,
          91, 90, 93, 94, 86, 88, 89, 87, 92, 91),  
  TMA_class = factor(c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
                       1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
                       1, 2, 3, 4, 5, 1, 2, 3, 4, 5))
)

new_data <- data.frame(
  age = c(48, 55, 62, 54, 67),  
  delta_creat = c(1.2, 1.3, 1.1, 1.4, 1.6),  
  max_LDH = c(305, 290, 320, 310, 295),  
  min_plat = c(150, 160, 135, 155, 145),  
  min_hb = c(12.2, 12.6, 11.9, 12.4, 11.7),  
  max_ast = c(38, 43, 39, 44, 40),  
  max_bt = c(37.2, 37.4, 37.0, 37.6, 37.1),  
  max_ttap = c(95, 108, 93, 103, 98),  
  max_tp = c(12.2, 12.4, 12.0, 12.6, 12.7),  
  hypertension = c(1, 0, 1, 0, 1),  
  MAP = c(92, 87, 94, 86, 90)  
)

model_spec <- 
  boost_tree(mode = 'classification',
             mtry = 6,  
             trees = 50, 
             min_n = 50,  
             tree_depth = 5) %>%
  set_engine('lightgbm')

recipe <- recipe(TMA_class ~ ., data = TMA_model)

model_fit <- workflow() %>%
  add_recipe(recipe) %>%
  add_model(model_spec) %>%
  fit(data = TMA_model)

saveRDS(model_fit, "model_fit.rds")
model_b <- readRDS("model_fit.rds")

new_data_predictions <- predict(model_b, new_data)

print(new_data_predictions)

I got no error when saving or loading it, but I cannot predict with the loaded model. Also, I have no problem with other models, like lasso or xgboost, they work fine, but lightGBM does not.

Upvotes: 2

Views: 368

Answers (1)

UseR10085
UseR10085

Reputation: 8198

You have to use the development version of lightgbm to save and load the model with saveRDS()/readRDS() as normal as reported here. But installing the development version of lightgbm is not that straight forward. You have to follow the following steps in Windows to install the development version of lightgbm

Installing from Source with CMake

You need to install git and CMake first form https://git-scm.com/downloads and https://cmake.org/download/

Note: this method is only supported on 64-bit systems. If you need to run LightGBM on 32-bit Windows (i386), follow the instructions in "Installing the CRAN Package".

Windows Preparation

NOTE: Windows users may need to run with administrator rights (either R or the command prompt, depending on the way you are installing this package).

Installing a 64-bit version of Rtools is mandatory. After installing Rtools and CMake, be sure the following paths are added to the environment variable PATH.

• Rtools: If you have Rtools 4.3, example:

C:\rtools43\mingw64\bin

C:\rtools43\usr\bin

• CMake example:

C:\Program Files\CMake\bin

• R example:

C:\Program Files\R\R-4.3.1\bin

Install MSYS2 (R 4.x)

Install MSYS2 from https://www.msys2.org/

Install Pandoc

Download the Pandoc from https://pandoc.org/installing.html and install it

On Windows, you can use either the classic command prompt or the more modern PowerShell terminal. If you use Windows in desktop mode, run the cmd or powershell command from the Start menu and type chcp 65001 before using pandoc, to set the encoding to UTF-8. Let’s verify that pandoc is installed. Type

pandoc --version

and hit enter. You should see a message telling you which version of pandoc is installed, and giving you some additional information.

Install with CMake

After following the "preparation" steps above for your operating system, build and install the R-package with the following commands in the command prompt run with administrator rights:

git clone --recursive https://github.com/microsoft/LightGBM
cd LightGBM
Rscript build_r.R --use-msys2

After that your code runs without any error. It took me a whole day to figure it out how to install the development version of lightgbm.

Upvotes: 2

Related Questions