diff --git a/R-package/NAMESPACE b/R-package/NAMESPACE index ab987d0593eb..718f0e55a0d7 100644 --- a/R-package/NAMESPACE +++ b/R-package/NAMESPACE @@ -34,8 +34,6 @@ export(lgb.restore_handle) export(lgb.save) export(lgb.train) export(lightgbm) -export(readRDS.lgb.Booster) -export(saveRDS.lgb.Booster) export(setLGBMthreads) export(set_field) export(slice) diff --git a/R-package/R/readRDS.lgb.Booster.R b/R-package/R/readRDS.lgb.Booster.R deleted file mode 100644 index 69e954fc75f1..000000000000 --- a/R-package/R/readRDS.lgb.Booster.R +++ /dev/null @@ -1,50 +0,0 @@ -#' @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) -} diff --git a/R-package/R/saveRDS.lgb.Booster.R b/R-package/R/saveRDS.lgb.Booster.R deleted file mode 100644 index d227d75eb90d..000000000000 --- a/R-package/R/saveRDS.lgb.Booster.R +++ /dev/null @@ -1,80 +0,0 @@ -#' @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)) -} diff --git a/R-package/man/readRDS.lgb.Booster.Rd b/R-package/man/readRDS.lgb.Booster.Rd deleted file mode 100644 index 0a144434cd36..000000000000 --- a/R-package/man/readRDS.lgb.Booster.Rd +++ /dev/null @@ -1,53 +0,0 @@ -% 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) -} -} diff --git a/R-package/man/saveRDS.lgb.Booster.Rd b/R-package/man/saveRDS.lgb.Booster.Rd deleted file mode 100644 index b9b34e1fd021..000000000000 --- a/R-package/man/saveRDS.lgb.Booster.Rd +++ /dev/null @@ -1,75 +0,0 @@ -% 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) -} -} diff --git a/R-package/pkgdown/_pkgdown.yml b/R-package/pkgdown/_pkgdown.yml index 233a31f0ead9..99a3b1010d41 100644 --- a/R-package/pkgdown/_pkgdown.yml +++ b/R-package/pkgdown/_pkgdown.yml @@ -85,8 +85,6 @@ reference: - '`lgb.save`' - '`lgb.load`' - '`lgb.model.dt.tree`' - - '`saveRDS.lgb.Booster`' - - '`readRDS.lgb.Booster`' - title: Model Interpretation desc: Analyze your models contents: diff --git a/R-package/tests/testthat/test_lgb.Booster.R b/R-package/tests/testthat/test_lgb.Booster.R index c1fc02630c13..e6b0e8abda64 100644 --- a/R-package/tests/testthat/test_lgb.Booster.R +++ b/R-package/tests/testthat/test_lgb.Booster.R @@ -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", { data(agaricus.train, package = "lightgbm") dtrain <- lgb.Dataset( @@ -1350,46 +1311,6 @@ test_that("Handle is automatically restored when calling predict", { 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", { X <- matrix(rnorm(100L), ncol = 1L) labels <- 2L * X + runif(nrow(X), 0L, 0.1)