Unverified Commit 4588d648 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] remove readRDS.lgb.Booster() and saveRDS.lgb.Booster() (#6246)

parent 051eb22c
...@@ -34,8 +34,6 @@ export(lgb.restore_handle) ...@@ -34,8 +34,6 @@ export(lgb.restore_handle)
export(lgb.save) export(lgb.save)
export(lgb.train) export(lgb.train)
export(lightgbm) export(lightgbm)
export(readRDS.lgb.Booster)
export(saveRDS.lgb.Booster)
export(setLGBMthreads) export(setLGBMthreads)
export(set_field) export(set_field)
export(slice) export(slice)
......
#' @name readRDS.lgb.Booster
#' @title readRDS for \code{lgb.Booster} models (DEPRECATED)
#' @description Calls \code{readRDS} in what is expected to be a serialized \code{lgb.Booster} object,
#' and then restores its handle through \code{lgb.restore_handle}.
#'
#' \bold{This function throws a warning and will be removed in future versions.}
#' @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 \code{lgb.Booster}
#'
#' @examples
#' \donttest{
#' library(lightgbm)
#' \dontshow{setLGBMthreads(2L)}
#' \dontshow{data.table::setDTthreads(1L)}
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' data(agaricus.test, package = "lightgbm")
#' test <- agaricus.test
#' dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label)
#' params <- list(
#' objective = "regression"
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
#' params = params
#' , data = dtrain
#' , nrounds = 10L
#' , valids = valids
#' , early_stopping_rounds = 5L
#' )
#' model_file <- tempfile(fileext = ".rds")
#' saveRDS.lgb.Booster(model, model_file)
#' new_model <- readRDS.lgb.Booster(model_file)
#' }
#' @export
readRDS.lgb.Booster <- function(file, refhook = NULL) {
warning("'readRDS.lgb.Booster' is deprecated and will be removed in a future release. Use readRDS() instead.")
object <- readRDS(file = file, refhook = refhook)
lgb.restore_handle(object)
return(object)
}
#' @name saveRDS.lgb.Booster
#' @title saveRDS for \code{lgb.Booster} models (DEPRECATED)
#' @description Calls \code{saveRDS} on an \code{lgb.Booster} object, making it serializable before the call if
#' it isn't already.
#'
#' \bold{This function throws a warning and will be removed in future versions.}
#' @param object \code{lgb.Booster} object to serialize.
#' @param file a connection or the name of the file where the R object is saved to or read from.
#' @param ascii a logical. If TRUE or NA, an ASCII representation is written; otherwise (default),
#' a binary one is used. See the comments in the help for save.
#' @param version the workspace format version to use. \code{NULL} specifies the current default
#' version (2). Versions prior to 2 are not supported, so this will only be relevant
#' when there are later versions.
#' @param compress a logical specifying whether saving to a named file is to use "gzip" compression,
#' or one of \code{"gzip"}, \code{"bzip2"} or \code{"xz"} to indicate the type of
#' compression to be used. Ignored if file is a connection.
#' @param refhook a hook function for handling reference objects.
#' @param raw whether to save the model in a raw variable or not, recommended to leave it to \code{TRUE}.
#'
#' @return NULL invisibly.
#'
#' @examples
#' \donttest{
#' library(lightgbm)
#' \dontshow{setLGBMthreads(2L)}
#' \dontshow{data.table::setDTthreads(1L)}
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' data(agaricus.test, package = "lightgbm")
#' test <- agaricus.test
#' dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label)
#' params <- list(
#' objective = "regression"
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
#' params = params
#' , data = dtrain
#' , nrounds = 10L
#' , valids = valids
#' , early_stopping_rounds = 5L
#' )
#' model_file <- tempfile(fileext = ".rds")
#' saveRDS.lgb.Booster(model, model_file)
#' }
#' @export
saveRDS.lgb.Booster <- function(object,
file,
ascii = FALSE,
version = NULL,
compress = TRUE,
refhook = NULL,
raw = TRUE) {
warning("'saveRDS.lgb.Booster' is deprecated and will be removed in a future release. Use saveRDS() instead.")
if (!.is_Booster(x = object)) {
stop("saveRDS.lgb.Booster: object should be an ", sQuote("lgb.Booster"))
}
if (is.null(object$raw)) {
lgb.make_serializable(object)
}
saveRDS(
object
, file = file
, ascii = ascii
, version = version
, compress = compress
, refhook = refhook
)
return(invisible(NULL))
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/readRDS.lgb.Booster.R
\name{readRDS.lgb.Booster}
\alias{readRDS.lgb.Booster}
\title{readRDS for \code{lgb.Booster} models (DEPRECATED)}
\usage{
readRDS.lgb.Booster(file, refhook = NULL)
}
\arguments{
\item{file}{a connection or the name of the file where the R object is saved to or read from.}
\item{refhook}{a hook function for handling reference objects.}
}
\value{
\code{lgb.Booster}
}
\description{
Calls \code{readRDS} in what is expected to be a serialized \code{lgb.Booster} object,
and then restores its handle through \code{lgb.restore_handle}.
\bold{This function throws a warning and will be removed in future versions.}
}
\examples{
\donttest{
library(lightgbm)
\dontshow{setLGBMthreads(2L)}
\dontshow{data.table::setDTthreads(1L)}
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
data(agaricus.test, package = "lightgbm")
test <- agaricus.test
dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label)
params <- list(
objective = "regression"
, metric = "l2"
, min_data = 1L
, learning_rate = 1.0
, num_threads = 2L
)
valids <- list(test = dtest)
model <- lgb.train(
params = params
, data = dtrain
, nrounds = 10L
, valids = valids
, early_stopping_rounds = 5L
)
model_file <- tempfile(fileext = ".rds")
saveRDS.lgb.Booster(model, model_file)
new_model <- readRDS.lgb.Booster(model_file)
}
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/saveRDS.lgb.Booster.R
\name{saveRDS.lgb.Booster}
\alias{saveRDS.lgb.Booster}
\title{saveRDS for \code{lgb.Booster} models (DEPRECATED)}
\usage{
saveRDS.lgb.Booster(
object,
file,
ascii = FALSE,
version = NULL,
compress = TRUE,
refhook = NULL,
raw = TRUE
)
}
\arguments{
\item{object}{\code{lgb.Booster} object to serialize.}
\item{file}{a connection or the name of the file where the R object is saved to or read from.}
\item{ascii}{a logical. If TRUE or NA, an ASCII representation is written; otherwise (default),
a binary one is used. See the comments in the help for save.}
\item{version}{the workspace format version to use. \code{NULL} specifies the current default
version (2). Versions prior to 2 are not supported, so this will only be relevant
when there are later versions.}
\item{compress}{a logical specifying whether saving to a named file is to use "gzip" compression,
or one of \code{"gzip"}, \code{"bzip2"} or \code{"xz"} to indicate the type of
compression to be used. Ignored if file is a connection.}
\item{refhook}{a hook function for handling reference objects.}
\item{raw}{whether to save the model in a raw variable or not, recommended to leave it to \code{TRUE}.}
}
\value{
NULL invisibly.
}
\description{
Calls \code{saveRDS} on an \code{lgb.Booster} object, making it serializable before the call if
it isn't already.
\bold{This function throws a warning and will be removed in future versions.}
}
\examples{
\donttest{
library(lightgbm)
\dontshow{setLGBMthreads(2L)}
\dontshow{data.table::setDTthreads(1L)}
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
data(agaricus.test, package = "lightgbm")
test <- agaricus.test
dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label)
params <- list(
objective = "regression"
, metric = "l2"
, min_data = 1L
, learning_rate = 1.0
, num_threads = 2L
)
valids <- list(test = dtest)
model <- lgb.train(
params = params
, data = dtrain
, nrounds = 10L
, valids = valids
, early_stopping_rounds = 5L
)
model_file <- tempfile(fileext = ".rds")
saveRDS.lgb.Booster(model, model_file)
}
}
...@@ -85,8 +85,6 @@ reference: ...@@ -85,8 +85,6 @@ reference:
- '`lgb.save`' - '`lgb.save`'
- '`lgb.load`' - '`lgb.load`'
- '`lgb.model.dt.tree`' - '`lgb.model.dt.tree`'
- '`saveRDS.lgb.Booster`'
- '`readRDS.lgb.Booster`'
- title: Model Interpretation - title: Model Interpretation
desc: Analyze your models desc: Analyze your models
contents: contents:
......
...@@ -1252,45 +1252,6 @@ test_that("lgb.cv() correctly handles passing through params to the model file", ...@@ -1252,45 +1252,6 @@ test_that("lgb.cv() correctly handles passing through params to the model file",
}) })
test_that("params (including dataset params) should be stored in .rds file for Booster", {
data(agaricus.train, package = "lightgbm")
dtrain <- lgb.Dataset(
agaricus.train$data
, label = agaricus.train$label
, params = list(
max_bin = 17L
)
)
params <- list(
objective = "binary"
, max_depth = 4L
, bagging_fraction = 0.8
, verbose = .LGB_VERBOSITY
, num_threads = .LGB_MAX_THREADS
)
bst <- Booster$new(
params = params
, train_set = dtrain
)
bst_file <- tempfile(fileext = ".rds")
expect_warning(saveRDS.lgb.Booster(bst, file = bst_file))
expect_warning({
bst_from_file <- readRDS.lgb.Booster(file = bst_file)
})
expect_identical(
bst_from_file$params
, list(
objective = "binary"
, max_depth = 4L
, bagging_fraction = 0.8
, verbose = .LGB_VERBOSITY
, num_threads = .LGB_MAX_THREADS
, max_bin = 17L
)
)
})
test_that("params (including dataset params) should be stored in .rds file for Booster", { test_that("params (including dataset params) should be stored in .rds file for Booster", {
data(agaricus.train, package = "lightgbm") data(agaricus.train, package = "lightgbm")
dtrain <- lgb.Dataset( dtrain <- lgb.Dataset(
...@@ -1350,46 +1311,6 @@ test_that("Handle is automatically restored when calling predict", { ...@@ -1350,46 +1311,6 @@ test_that("Handle is automatically restored when calling predict", {
expect_equal(pred_before, pred_after) expect_equal(pred_before, pred_after)
}) })
test_that("boosters with linear models at leaves work with saveRDS.lgb.Booster and readRDS.lgb.Booster", {
X <- matrix(rnorm(100L), ncol = 1L)
labels <- 2L * X + runif(nrow(X), 0L, 0.1)
dtrain <- lgb.Dataset(
data = X
, label = labels
)
params <- list(
objective = "regression"
, verbose = .LGB_VERBOSITY
, metric = "mse"
, seed = 0L
, num_leaves = 2L
, num_threads = .LGB_MAX_THREADS
)
bst <- lgb.train(
data = dtrain
, nrounds = 10L
, params = params
)
expect_true(.is_Booster(bst))
# save predictions, then write the model to a file and destroy it in R
preds <- predict(bst, X)
model_file <- tempfile(fileext = ".rds")
expect_warning(saveRDS.lgb.Booster(bst, file = model_file))
bst$finalize()
expect_null(bst$.__enclos_env__$private$handle)
rm(bst)
# load the booster and make predictions...should be the same
expect_warning({
bst2 <- readRDS.lgb.Booster(file = model_file)
})
preds2 <- predict(bst2, X)
expect_identical(preds, preds2)
})
test_that("boosters with linear models at leaves can be written to RDS and re-loaded successfully", { test_that("boosters with linear models at leaves can be written to RDS and re-loaded successfully", {
X <- matrix(rnorm(100L), ncol = 1L) X <- matrix(rnorm(100L), ncol = 1L)
labels <- 2L * X + runif(nrow(X), 0L, 0.1) labels <- 2L * X + runif(nrow(X), 0L, 0.1)
......
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