From 6d08b9f47b74b03369bd34adbf749a538c10478b Mon Sep 17 00:00:00 2001 From: Guolin Ke Date: Sun, 25 Nov 2018 11:20:43 +0800 Subject: [PATCH 1/2] add free booster in lgb.cv --- R-package/R/lgb.cv.R | 6 +++++- R-package/R/lgb.train.R | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/R-package/R/lgb.cv.R b/R-package/R/lgb.cv.R index ddaf5c281998..35a1e76d5c01 100644 --- a/R-package/R/lgb.cv.R +++ b/R-package/R/lgb.cv.R @@ -41,6 +41,7 @@ CVBooster <- R6::R6Class( #' type str represents feature names #' @param callbacks list of callback functions #' List of callback functions that are applied at each iteration. +#' @param free_boosters Boolean, setting it to TRUE (the default value) will free the Booster objects in cv-folds #' @param ... other parameters, see Parameters.rst for more information. A few key parameters: #' \itemize{ #' \item{boosting}{Boosting type. \code{"gbdt"} or \code{"dart"}} @@ -87,6 +88,7 @@ lgb.cv <- function(params = list(), categorical_feature = NULL, early_stopping_rounds = NULL, callbacks = list(), + free_boosters = TRUE, ...) { # Setup temporary variables @@ -303,7 +305,9 @@ lgb.cv <- function(params = list(), if (env$met_early_stop) break } - + if (free_boosters) { + cv_booster$boosters = NULL + } # Return booster return(cv_booster) diff --git a/R-package/R/lgb.train.R b/R-package/R/lgb.train.R index 29a37853e899..f9744e8d3ce1 100644 --- a/R-package/R/lgb.train.R +++ b/R-package/R/lgb.train.R @@ -12,6 +12,8 @@ #' @param categorical_feature list of str or int #' type int represents index, #' type str represents feature names +#' @param callbacks list of callback functions +#' List of callback functions that are applied at each iteration. #' @param reset_data Boolean, setting it to TRUE (not the default value) will transform the booster model into a predictor model which frees up memory and the original datasets #' @param ... other parameters, see Parameters.rst for more information. A few key parameters: #' \itemize{ From 9b88655aef76995269bd5eff442a79a59f069d73 Mon Sep 17 00:00:00 2001 From: Guolin Ke Date: Mon, 26 Nov 2018 04:48:17 +0800 Subject: [PATCH 2/2] change to reset_data --- R-package/R/lgb.cv.R | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/R-package/R/lgb.cv.R b/R-package/R/lgb.cv.R index 35a1e76d5c01..c0d4bc2e6335 100644 --- a/R-package/R/lgb.cv.R +++ b/R-package/R/lgb.cv.R @@ -41,7 +41,7 @@ CVBooster <- R6::R6Class( #' type str represents feature names #' @param callbacks list of callback functions #' List of callback functions that are applied at each iteration. -#' @param free_boosters Boolean, setting it to TRUE (the default value) will free the Booster objects in cv-folds +#' @param reset_data Boolean, setting it to TRUE (not the default value) will transform the booster model into a predictor model which frees up memory and the original datasets #' @param ... other parameters, see Parameters.rst for more information. A few key parameters: #' \itemize{ #' \item{boosting}{Boosting type. \code{"gbdt"} or \code{"dart"}} @@ -88,7 +88,7 @@ lgb.cv <- function(params = list(), categorical_feature = NULL, early_stopping_rounds = NULL, callbacks = list(), - free_boosters = TRUE, + reset_data = FALSE, ...) { # Setup temporary variables @@ -305,8 +305,18 @@ lgb.cv <- function(params = list(), if (env$met_early_stop) break } - if (free_boosters) { - cv_booster$boosters = NULL + if (reset_data) { + lapply(cv_booster$boosters, function(fd) { + # Store temporarily model data elsewhere + booster_old <- list(best_iter = fd$booster$best_iter, + best_score = fd$booster$best_score, + record_evals = fd$booster$record_evals) + # Reload model + fd$booster <- lgb.load(model_str = fd$booster$save_model_to_string()) + fd$booster$best_iter <- booster_old$best_iter + fd$booster$best_score <- booster_old$best_score + fd$booster$record_evals <- booster_old$record_evals + }) } # Return booster return(cv_booster)