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(
# Save model to temporary file for in-memory saving
save = function() {
# Create temporary file
temp <- tempfile()
# Save model to file
lgb.save(self, temp)
# Overwrite model in object
self$raw <- readChar(temp, file.info(temp)$size)
# Remove temporary file
file.remove(temp)
self$raw <- self$save_model_to_string(NULL)
}
......@@ -680,7 +671,7 @@ predict.lgb.Booster <- function(object, data,
#' @param filename path of model file
#' @param model_str a str containing the model
#'
#' @return booster
#' @return lgb.Booster
#'
#' @examples
#' \dontrun{
......@@ -701,8 +692,9 @@ predict.lgb.Booster <- function(object, data,
#' learning_rate = 1,
#' early_stopping_rounds = 10)
#' lgb.save(model, "model.txt")
#' load_booster <- lgb.load("model.txt")
#' load_booster_from_str <- lgb.load(model$raw)
#' load_booster <- lgb.load(filename = "model.txt")
#' model_string <- model$save_model_to_string(NULL) # saves best iteration
#' load_booster_from_str <- lgb.load(model_str = model_string)
#' }
#'
#' @rdname lgb.load
......@@ -739,7 +731,7 @@ lgb.load <- function(filename = NULL, model_str = NULL){
#' @param filename saved filename
#' @param num_iteration number of iteration want to predict with, NULL or <= 0 means use best iteration
#'
#' @return booster
#' @return lgb.Booster
#'
#' @examples
#' \dontrun{
......
......@@ -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 refhook a hook function for handling reference objects.
#'
#' @return an R object.
#' @return lgb.Booster.
#'
#' @examples
#' \dontrun{
......@@ -38,11 +38,8 @@ readRDS.lgb.Booster <- function(file = "", refhook = NULL) {
# Check if object has the model stored
if (!is.na(object$raw)) {
# Create temporary file for the model loading
temp <- tempfile()
write(object$raw, temp)
object2 <- lgb.load(temp)
file.remove(temp)
# Create temporary model for the model loading
object2 <- lgb.load(model_str = object$raw)
# Restore best iteration and recorded evaluations
object2$best_iter <- object$best_iter
......
......@@ -12,7 +12,7 @@ lgb.load(filename = NULL, model_str = NULL)
\item{model_str}{a str containing the model}
}
\value{
booster
lgb.Booster
}
\description{
Load LightGBM model from saved model file or string
......@@ -38,8 +38,9 @@ model <- lgb.train(params,
learning_rate = 1,
early_stopping_rounds = 10)
lgb.save(model, "model.txt")
load_booster <- lgb.load("model.txt")
load_booster_from_str <- lgb.load(model$raw)
load_booster <- lgb.load(filename = "model.txt")
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)
\item{num_iteration}{number of iteration want to predict with, NULL or <= 0 means use best iteration}
}
\value{
booster
lgb.Booster
}
\description{
Save LightGBM model
......
......@@ -12,7 +12,7 @@ readRDS.lgb.Booster(file = "", refhook = NULL)
\item{refhook}{a hook function for handling reference objects.}
}
\value{
an R object.
lgb.Booster.
}
\description{
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