Unverified Commit 9259a531 authored by david-cortes's avatar david-cortes Committed by GitHub
Browse files

[R-package] remove behavior where lightgbm() saves model to disk (#4974)

* do not produce model files on disk by default

* remove 'save_name' argument from 'lightgbm()'
parent 057ba078
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#' , agaricus.train$label #' , agaricus.train$label
#' , params = list(objective = "binary", nthreads = 1L) #' , params = list(objective = "binary", nthreads = 1L)
#' , nrounds = 5L #' , nrounds = 5L
#' , save_name = NULL
#' , verbose = 0) #' , verbose = 0)
#' fname <- tempfile(fileext="rds") #' fname <- tempfile(fileext="rds")
#' saveRDS(model, fname) #' saveRDS(model, fname)
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
#' the "Parameters" section of the documentation} for a list of parameters and valid values. #' the "Parameters" section of the documentation} for a list of parameters and valid values.
#' @param verbose verbosity for output, if <= 0, also will disable the print of evaluation during training #' @param verbose verbosity for output, if <= 0, also will disable the print of evaluation during training
#' @param serializable whether to make the resulting objects serializable through functions such as #' @param serializable whether to make the resulting objects serializable through functions such as
#' \code{save} or \code{saveRDS} (see section "Model serialization"). #' \code{save} or \code{saveRDS} (see section "Model serialization").
#' @section Early Stopping: #' @section Early Stopping:
#' #'
#' "early stopping" refers to stopping the training process if the model's performance on a given #' "early stopping" refers to stopping the training process if the model's performance on a given
...@@ -92,8 +92,6 @@ NULL ...@@ -92,8 +92,6 @@ NULL
#' @inheritParams lgb_shared_params #' @inheritParams lgb_shared_params
#' @param label Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}} #' @param label Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}}
#' @param weight vector of response values. If not NULL, will set to dataset #' @param weight vector of response values. If not NULL, will set to dataset
#' @param save_name File name to use when writing the trained model to disk. Should end in ".model".
#' If passing `NULL`, will not save the trained model to disk.
#' @param ... Additional arguments passed to \code{\link{lgb.train}}. For example #' @param ... Additional arguments passed to \code{\link{lgb.train}}. For example
#' \itemize{ #' \itemize{
#' \item{\code{valids}: a list of \code{lgb.Dataset} objects, used for validation} #' \item{\code{valids}: a list of \code{lgb.Dataset} objects, used for validation}
...@@ -120,7 +118,6 @@ lightgbm <- function(data, ...@@ -120,7 +118,6 @@ lightgbm <- function(data,
verbose = 1L, verbose = 1L,
eval_freq = 1L, eval_freq = 1L,
early_stopping_rounds = NULL, early_stopping_rounds = NULL,
save_name = "lightgbm.model",
init_model = NULL, init_model = NULL,
callbacks = list(), callbacks = list(),
serializable = TRUE, serializable = TRUE,
...@@ -167,11 +164,6 @@ lightgbm <- function(data, ...@@ -167,11 +164,6 @@ lightgbm <- function(data,
, args = train_args , args = train_args
) )
# Store model under a specific name
if (!is.null(save_name)) {
bst$save_model(filename = save_name)
}
return(bst) return(bst)
} }
......
...@@ -27,7 +27,6 @@ model <- lightgbm( ...@@ -27,7 +27,6 @@ model <- lightgbm(
, agaricus.train$label , agaricus.train$label
, params = list(objective = "binary", nthreads = 1L) , params = list(objective = "binary", nthreads = 1L)
, nrounds = 5L , nrounds = 5L
, save_name = NULL
, verbose = 0) , verbose = 0)
fname <- tempfile(fileext="rds") fname <- tempfile(fileext="rds")
saveRDS(model, fname) saveRDS(model, fname)
......
...@@ -13,7 +13,6 @@ lightgbm( ...@@ -13,7 +13,6 @@ lightgbm(
verbose = 1L, verbose = 1L,
eval_freq = 1L, eval_freq = 1L,
early_stopping_rounds = NULL, early_stopping_rounds = NULL,
save_name = "lightgbm.model",
init_model = NULL, init_model = NULL,
callbacks = list(), callbacks = list(),
serializable = TRUE, serializable = TRUE,
...@@ -44,9 +43,6 @@ fails to improve for \code{early_stopping_rounds} consecutive boosting rounds. ...@@ -44,9 +43,6 @@ fails to improve for \code{early_stopping_rounds} consecutive boosting rounds.
If training stops early, the returned model will have attribute \code{best_iter} If training stops early, the returned model will have attribute \code{best_iter}
set to the iteration number of the best iteration.} set to the iteration number of the best iteration.}
\item{save_name}{File name to use when writing the trained model to disk. Should end in ".model".
If passing `NULL`, will not save the trained model to disk.}
\item{init_model}{path of model file of \code{lgb.Booster} object, will continue training from this model} \item{init_model}{path of model file of \code{lgb.Booster} object, will continue training from this model}
\item{callbacks}{List of callback functions that are applied at each iteration.} \item{callbacks}{List of callback functions that are applied at each iteration.}
......
...@@ -80,7 +80,6 @@ test_that("train and predict binary classification", { ...@@ -80,7 +80,6 @@ test_that("train and predict binary classification", {
, metric = "binary_error" , metric = "binary_error"
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
expect_false(is.null(bst$record_evals)) expect_false(is.null(bst$record_evals))
record_results <- lgb.get.eval.result(bst, "train", "binary_error") record_results <- lgb.get.eval.result(bst, "train", "binary_error")
...@@ -114,7 +113,6 @@ test_that("train and predict softmax", { ...@@ -114,7 +113,6 @@ test_that("train and predict softmax", {
, num_class = 3L , num_class = 3L
) )
, nrounds = 20L , nrounds = 20L
, save_name = tempfile(fileext = ".model")
) )
expect_false(is.null(bst$record_evals)) expect_false(is.null(bst$record_evals))
...@@ -138,7 +136,6 @@ test_that("use of multiple eval metrics works", { ...@@ -138,7 +136,6 @@ test_that("use of multiple eval metrics works", {
, metric = metrics , metric = metrics
) )
, nrounds = 10L , nrounds = 10L
, save_name = tempfile(fileext = ".model")
) )
expect_false(is.null(bst$record_evals)) expect_false(is.null(bst$record_evals))
expect_named( expect_named(
...@@ -161,7 +158,6 @@ test_that("lgb.Booster.upper_bound() and lgb.Booster.lower_bound() work as expec ...@@ -161,7 +158,6 @@ test_that("lgb.Booster.upper_bound() and lgb.Booster.lower_bound() work as expec
, metric = "binary_error" , metric = "binary_error"
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
expect_true(abs(bst$lower_bound() - -1.590853) < TOLERANCE) expect_true(abs(bst$lower_bound() - -1.590853) < TOLERANCE)
expect_true(abs(bst$upper_bound() - 1.871015) < TOLERANCE) expect_true(abs(bst$upper_bound() - 1.871015) < TOLERANCE)
...@@ -179,7 +175,6 @@ test_that("lgb.Booster.upper_bound() and lgb.Booster.lower_bound() work as expec ...@@ -179,7 +175,6 @@ test_that("lgb.Booster.upper_bound() and lgb.Booster.lower_bound() work as expec
, metric = "l2" , metric = "l2"
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
expect_true(abs(bst$lower_bound() - 0.1513859) < TOLERANCE) expect_true(abs(bst$lower_bound() - 0.1513859) < TOLERANCE)
expect_true(abs(bst$upper_bound() - 0.9080349) < TOLERANCE) expect_true(abs(bst$upper_bound() - 0.9080349) < TOLERANCE)
...@@ -194,7 +189,6 @@ test_that("lightgbm() rejects negative or 0 value passed to nrounds", { ...@@ -194,7 +189,6 @@ test_that("lightgbm() rejects negative or 0 value passed to nrounds", {
data = dtrain data = dtrain
, params = params , params = params
, nrounds = nround_value , nrounds = nround_value
, save_name = tempfile(fileext = ".model")
) )
}, "nrounds should be greater than zero") }, "nrounds should be greater than zero")
} }
...@@ -213,7 +207,6 @@ test_that("lightgbm() accepts nrounds as either a top-level argument or paramete ...@@ -213,7 +207,6 @@ test_that("lightgbm() accepts nrounds as either a top-level argument or paramete
, metric = "l2" , metric = "l2"
, num_leaves = 5L , num_leaves = 5L
) )
, save_name = tempfile(fileext = ".model")
) )
set.seed(708L) set.seed(708L)
...@@ -226,7 +219,6 @@ test_that("lightgbm() accepts nrounds as either a top-level argument or paramete ...@@ -226,7 +219,6 @@ test_that("lightgbm() accepts nrounds as either a top-level argument or paramete
, num_leaves = 5L , num_leaves = 5L
, nrounds = nrounds , nrounds = nrounds
) )
, save_name = tempfile(fileext = ".model")
) )
set.seed(708L) set.seed(708L)
...@@ -240,7 +232,6 @@ test_that("lightgbm() accepts nrounds as either a top-level argument or paramete ...@@ -240,7 +232,6 @@ test_that("lightgbm() accepts nrounds as either a top-level argument or paramete
, num_leaves = 5L , num_leaves = 5L
, nrounds = nrounds , nrounds = nrounds
) )
, save_name = tempfile(fileext = ".model")
) )
top_level_l2 <- top_level_bst$eval_train()[[1L]][["value"]] top_level_l2 <- top_level_bst$eval_train()[[1L]][["value"]]
...@@ -289,7 +280,6 @@ test_that("lightgbm() performs evaluation on validation sets if they are provide ...@@ -289,7 +280,6 @@ test_that("lightgbm() performs evaluation on validation sets if they are provide
"valid1" = dvalid1 "valid1" = dvalid1
, "valid2" = dvalid2 , "valid2" = dvalid2
) )
, save_name = tempfile(fileext = ".model")
) )
expect_named( expect_named(
...@@ -307,23 +297,6 @@ test_that("lightgbm() performs evaluation on validation sets if they are provide ...@@ -307,23 +297,6 @@ test_that("lightgbm() performs evaluation on validation sets if they are provide
expect_true(abs(bst$record_evals[["valid2"]][["binary_error"]][["eval"]][[1L]] - 0.02226317) < TOLERANCE) expect_true(abs(bst$record_evals[["valid2"]][["binary_error"]][["eval"]][[1L]] - 0.02226317) < TOLERANCE)
}) })
test_that("lightgbm() does not write model to disk if save_name=NULL", {
files_before <- list.files(getwd())
model <- lightgbm(
data = train$data
, label = train$label
, nrounds = 5L
, params = list(objective = "binary")
, verbose = 0L
, save_name = NULL
)
files_after <- list.files(getwd())
expect_equal(files_before, files_after)
})
test_that("training continuation works", { test_that("training continuation works", {
dtrain <- lgb.Dataset( dtrain <- lgb.Dataset(
train$data train$data
...@@ -742,7 +715,6 @@ test_that("lgb.train() accepts nrounds as either a top-level argument or paramet ...@@ -742,7 +715,6 @@ test_that("lgb.train() accepts nrounds as either a top-level argument or paramet
objective = "regression" objective = "regression"
, metric = "l2" , metric = "l2"
, num_leaves = 5L , num_leaves = 5L
, save_name = tempfile(fileext = ".model")
, verbose = VERBOSITY , verbose = VERBOSITY
) )
) )
...@@ -758,7 +730,6 @@ test_that("lgb.train() accepts nrounds as either a top-level argument or paramet ...@@ -758,7 +730,6 @@ test_that("lgb.train() accepts nrounds as either a top-level argument or paramet
, metric = "l2" , metric = "l2"
, num_leaves = 5L , num_leaves = 5L
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
, verbose = VERBOSITY , verbose = VERBOSITY
) )
) )
...@@ -775,7 +746,6 @@ test_that("lgb.train() accepts nrounds as either a top-level argument or paramet ...@@ -775,7 +746,6 @@ test_that("lgb.train() accepts nrounds as either a top-level argument or paramet
, metric = "l2" , metric = "l2"
, num_leaves = 5L , num_leaves = 5L
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
, verbose = VERBOSITY , verbose = VERBOSITY
) )
) )
...@@ -1981,7 +1951,6 @@ test_that("using lightgbm() without early stopping, best_iter and best_score com ...@@ -1981,7 +1951,6 @@ test_that("using lightgbm() without early stopping, best_iter and best_score com
, num_leaves = 5L , num_leaves = 5L
) )
, verbose = -7L , verbose = -7L
, save_name = tempfile(fileext = ".model")
) )
# when verbose <= 0 is passed to lightgbm(), 'valids' is passed through to lgb.train() # when verbose <= 0 is passed to lightgbm(), 'valids' is passed through to lgb.train()
# untouched. If you set verbose to > 0, the training data will still be first but called "train" # untouched. If you set verbose to > 0, the training data will still be first but called "train"
......
...@@ -136,7 +136,6 @@ test_that("lgb.load() gives the expected error messages given different incorrec ...@@ -136,7 +136,6 @@ test_that("lgb.load() gives the expected error messages given different incorrec
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = 2L , nrounds = 2L
, save_name = tempfile(fileext = ".model")
) )
# you have to give model_str or filename # you have to give model_str or filename
...@@ -183,7 +182,6 @@ test_that("Loading a Booster from a text file works", { ...@@ -183,7 +182,6 @@ test_that("Loading a Booster from a text file works", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = 2L , nrounds = 2L
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
...@@ -260,7 +258,6 @@ test_that("Loading a Booster from a string works", { ...@@ -260,7 +258,6 @@ test_that("Loading a Booster from a string works", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = 2L , nrounds = 2L
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
...@@ -292,7 +289,6 @@ test_that("Saving a large model to string should work", { ...@@ -292,7 +289,6 @@ test_that("Saving a large model to string should work", {
, objective = "binary" , objective = "binary"
) )
, nrounds = 500L , nrounds = 500L
, save_name = tempfile(fileext = ".model")
, verbose = VERBOSITY , verbose = VERBOSITY
) )
...@@ -336,7 +332,6 @@ test_that("Saving a large model to JSON should work", { ...@@ -336,7 +332,6 @@ test_that("Saving a large model to JSON should work", {
, objective = "binary" , objective = "binary"
) )
, nrounds = 200L , nrounds = 200L
, save_name = tempfile(fileext = ".model")
, verbose = VERBOSITY , verbose = VERBOSITY
) )
...@@ -367,7 +362,6 @@ test_that("If a string and a file are both passed to lgb.load() the file is used ...@@ -367,7 +362,6 @@ test_that("If a string and a file are both passed to lgb.load() the file is used
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = 2L , nrounds = 2L
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
...@@ -423,7 +417,6 @@ test_that("Creating a Booster from a Dataset with an existing predictor should w ...@@ -423,7 +417,6 @@ test_that("Creating a Booster from a Dataset with an existing predictor should w
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
data(agaricus.test, package = "lightgbm") data(agaricus.test, package = "lightgbm")
dtest <- Dataset$new( dtest <- Dataset$new(
...@@ -517,7 +510,6 @@ test_that("Booster$rollback_one_iter() should work as expected", { ...@@ -517,7 +510,6 @@ test_that("Booster$rollback_one_iter() should work as expected", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
expect_equal(bst$current_iter(), nrounds) expect_equal(bst$current_iter(), nrounds)
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
...@@ -552,7 +544,6 @@ test_that("Booster$update() passing a train_set works as expected", { ...@@ -552,7 +544,6 @@ test_that("Booster$update() passing a train_set works as expected", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
expect_equal(bst$current_iter(), nrounds) expect_equal(bst$current_iter(), nrounds)
...@@ -576,7 +567,6 @@ test_that("Booster$update() passing a train_set works as expected", { ...@@ -576,7 +567,6 @@ test_that("Booster$update() passing a train_set works as expected", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = nrounds + 1L , nrounds = nrounds + 1L
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst2)) expect_true(lgb.is.Booster(bst2))
expect_equal(bst2$current_iter(), nrounds + 1L) expect_equal(bst2$current_iter(), nrounds + 1L)
...@@ -602,7 +592,6 @@ test_that("Booster$update() throws an informative error if you provide a non-Dat ...@@ -602,7 +592,6 @@ test_that("Booster$update() throws an informative error if you provide a non-Dat
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = nrounds , nrounds = nrounds
, save_name = tempfile(fileext = ".model")
) )
expect_error({ expect_error({
bst$update( bst$update(
...@@ -698,7 +687,6 @@ test_that("Saving a model with different feature importance types works", { ...@@ -698,7 +687,6 @@ test_that("Saving a model with different feature importance types works", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = 2L , nrounds = 2L
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
...@@ -754,7 +742,6 @@ test_that("Saving a model with unknown importance type fails", { ...@@ -754,7 +742,6 @@ test_that("Saving a model with unknown importance type fails", {
, verbose = VERBOSITY , verbose = VERBOSITY
) )
, nrounds = 2L , nrounds = 2L
, save_name = tempfile(fileext = ".model")
) )
expect_true(lgb.is.Booster(bst)) expect_true(lgb.is.Booster(bst))
......
...@@ -23,7 +23,6 @@ test_that("Feature penalties work properly", { ...@@ -23,7 +23,6 @@ test_that("Feature penalties work properly", {
) )
, nrounds = 5L , nrounds = 5L
, verbose = -1L , verbose = -1L
, save_name = tempfile(fileext = ".model")
) )
}) })
...@@ -75,7 +74,6 @@ test_that("training should warn if you use 'dart' boosting, specified with 'boos ...@@ -75,7 +74,6 @@ test_that("training should warn if you use 'dart' boosting, specified with 'boos
, params = params , params = params
, nrounds = 5L , nrounds = 5L
, verbose = -1L , verbose = -1L
, save_name = tempfile(fileext = ".model")
) )
}, regexp = "Early stopping is not available in 'dart' mode") }, regexp = "Early stopping is not available in 'dart' mode")
} }
......
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