Commit 97ca38d1 authored by Laurae's avatar Laurae Committed by Guolin Ke
Browse files

[R-package] Stop using temporary model files (#551)

* Not using temporary files anymore RDS

* Attempt to avoid using a temporary file
parent 8567248e
...@@ -416,17 +416,8 @@ Booster <- R6Class( ...@@ -416,17 +416,8 @@ Booster <- R6Class(
# Save model to temporary file for in-memory saving # Save model to temporary file for in-memory saving
save = function() { save = function() {
# Create temporary file
temp <- tempfile()
# Save model to file
lgb.save(self, temp)
# Overwrite model in object # Overwrite model in object
self$raw <- readChar(temp, file.info(temp)$size) self$raw <- self$save_model_to_string(NULL)
# Remove temporary file
file.remove(temp)
} }
...@@ -680,7 +671,7 @@ predict.lgb.Booster <- function(object, data, ...@@ -680,7 +671,7 @@ predict.lgb.Booster <- function(object, data,
#' @param filename path of model file #' @param filename path of model file
#' @param model_str a str containing the model #' @param model_str a str containing the model
#' #'
#' @return booster #' @return lgb.Booster
#' #'
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
...@@ -701,8 +692,9 @@ predict.lgb.Booster <- function(object, data, ...@@ -701,8 +692,9 @@ predict.lgb.Booster <- function(object, data,
#' learning_rate = 1, #' learning_rate = 1,
#' early_stopping_rounds = 10) #' early_stopping_rounds = 10)
#' lgb.save(model, "model.txt") #' lgb.save(model, "model.txt")
#' load_booster <- lgb.load("model.txt") #' load_booster <- lgb.load(filename = "model.txt")
#' load_booster_from_str <- lgb.load(model$raw) #' model_string <- model$save_model_to_string(NULL) # saves best iteration
#' load_booster_from_str <- lgb.load(model_str = model_string)
#' } #' }
#' #'
#' @rdname lgb.load #' @rdname lgb.load
...@@ -739,7 +731,7 @@ lgb.load <- function(filename = NULL, model_str = NULL){ ...@@ -739,7 +731,7 @@ lgb.load <- function(filename = NULL, model_str = NULL){
#' @param filename saved filename #' @param filename saved filename
#' @param num_iteration number of iteration want to predict with, NULL or <= 0 means use best iteration #' @param num_iteration number of iteration want to predict with, NULL or <= 0 means use best iteration
#' #'
#' @return booster #' @return lgb.Booster
#' #'
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#' @param file a connection or the name of the file where the R object is saved to or read from. #' @param file a connection or the name of the file where the R object is saved to or read from.
#' @param refhook a hook function for handling reference objects. #' @param refhook a hook function for handling reference objects.
#' #'
#' @return an R object. #' @return lgb.Booster.
#' #'
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
...@@ -38,11 +38,8 @@ readRDS.lgb.Booster <- function(file = "", refhook = NULL) { ...@@ -38,11 +38,8 @@ readRDS.lgb.Booster <- function(file = "", refhook = NULL) {
# Check if object has the model stored # Check if object has the model stored
if (!is.na(object$raw)) { if (!is.na(object$raw)) {
# Create temporary file for the model loading # Create temporary model for the model loading
temp <- tempfile() object2 <- lgb.load(model_str = object$raw)
write(object$raw, temp)
object2 <- lgb.load(temp)
file.remove(temp)
# Restore best iteration and recorded evaluations # Restore best iteration and recorded evaluations
object2$best_iter <- object$best_iter object2$best_iter <- object$best_iter
......
...@@ -12,7 +12,7 @@ lgb.load(filename = NULL, model_str = NULL) ...@@ -12,7 +12,7 @@ lgb.load(filename = NULL, model_str = NULL)
\item{model_str}{a str containing the model} \item{model_str}{a str containing the model}
} }
\value{ \value{
booster lgb.Booster
} }
\description{ \description{
Load LightGBM model from saved model file or string Load LightGBM model from saved model file or string
...@@ -38,8 +38,9 @@ model <- lgb.train(params, ...@@ -38,8 +38,9 @@ model <- lgb.train(params,
learning_rate = 1, learning_rate = 1,
early_stopping_rounds = 10) early_stopping_rounds = 10)
lgb.save(model, "model.txt") lgb.save(model, "model.txt")
load_booster <- lgb.load("model.txt") load_booster <- lgb.load(filename = "model.txt")
load_booster_from_str <- lgb.load(model$raw) model_string <- model$save_model_to_string(NULL) # saves best iteration
load_booster_from_str <- lgb.load(model_str = model_string)
} }
} }
......
...@@ -14,7 +14,7 @@ lgb.save(booster, filename, num_iteration = NULL) ...@@ -14,7 +14,7 @@ lgb.save(booster, filename, num_iteration = NULL)
\item{num_iteration}{number of iteration want to predict with, NULL or <= 0 means use best iteration} \item{num_iteration}{number of iteration want to predict with, NULL or <= 0 means use best iteration}
} }
\value{ \value{
booster lgb.Booster
} }
\description{ \description{
Save LightGBM model Save LightGBM model
......
...@@ -12,7 +12,7 @@ readRDS.lgb.Booster(file = "", refhook = NULL) ...@@ -12,7 +12,7 @@ readRDS.lgb.Booster(file = "", refhook = NULL)
\item{refhook}{a hook function for handling reference objects.} \item{refhook}{a hook function for handling reference objects.}
} }
\value{ \value{
an R object. lgb.Booster.
} }
\description{ \description{
Attemps to load a model using RDS. Attemps to load a model using RDS.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment