diff --git a/R-package/R/xgb.train.R b/R-package/R/xgb.train.R index fb403143a..b1d79d866 100644 --- a/R-package/R/xgb.train.R +++ b/R-package/R/xgb.train.R @@ -72,6 +72,8 @@ #' keeps getting worse consecutively for \code{k} rounds. #' @param maximize If \code{feval} and \code{early.stop.round} are set, then \code{maximize} must be set as well. #' \code{maximize=TRUE} means the larger the evaluation score the better. +#' @param save_period save the model to the disk in every \code{save_period} rounds, 0 means no such action. +#' @param save_name the name or path for periodically saved model file. #' @param ... other parameters to pass to \code{params}. #' #' @details @@ -120,7 +122,8 @@ #' xgb.train <- function(params=list(), data, nrounds, watchlist = list(), obj = NULL, feval = NULL, verbose = 1, print.every.n=1L, - early.stop.round = NULL, maximize = NULL, ...) { + early.stop.round = NULL, maximize = NULL, + save_period = 0, save_name = "xgboost.model", ...) { dtrain <- data if (typeof(params) != "list") { stop("xgb.train: first argument params must be list") @@ -215,6 +218,11 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), } } } + if (save_period > 0) { + if (i %% save_period == 0) { + xgb.save(bst, save_name) + } + } } bst <- xgb.Booster.check(bst) if (!is.null(early.stop.round)) { diff --git a/R-package/R/xgboost.R b/R-package/R/xgboost.R index 63077f866..c96a7d89e 100644 --- a/R-package/R/xgboost.R +++ b/R-package/R/xgboost.R @@ -36,6 +36,8 @@ #' keeps getting worse consecutively for \code{k} rounds. #' @param maximize If \code{feval} and \code{early.stop.round} are set, then \code{maximize} must be set as well. #' \code{maximize=TRUE} means the larger the evaluation score the better. +#' @param save_period save the model to the disk in every \code{save_period} rounds, 0 means no such action. +#' @param save_name the name or path for periodically saved model file. #' @param ... other parameters to pass to \code{params}. #' #' @details @@ -58,7 +60,7 @@ #' xgboost <- function(data = NULL, label = NULL, missing = NULL, params = list(), nrounds, verbose = 1, print.every.n = 1L, early.stop.round = NULL, - maximize = NULL, ...) { + maximize = NULL, save_period = 0, save_name = "xgboost.model", ...) { if (is.null(missing)) { dtrain <- xgb.get.DMatrix(data, label) } else { @@ -74,7 +76,8 @@ xgboost <- function(data = NULL, label = NULL, missing = NULL, params = list(), } bst <- xgb.train(params, dtrain, nrounds, watchlist, verbose = verbose, print.every.n=print.every.n, - early.stop.round = early.stop.round) + early.stop.round = early.stop.round, maximize = maximize, + save_period = save_period, save_name = save_name) return(bst) } diff --git a/R-package/man/xgb.train.Rd b/R-package/man/xgb.train.Rd index 7b1893ba7..15a0b0ba7 100644 --- a/R-package/man/xgb.train.Rd +++ b/R-package/man/xgb.train.Rd @@ -6,7 +6,8 @@ \usage{ xgb.train(params = list(), data, nrounds, watchlist = list(), obj = NULL, feval = NULL, verbose = 1, print.every.n = 1L, - early.stop.round = NULL, maximize = NULL, ...) + early.stop.round = NULL, maximize = NULL, save_period = 0, + save_name = "xgboost.model", ...) } \arguments{ \item{params}{the list of parameters. @@ -87,6 +88,10 @@ keeps getting worse consecutively for \code{k} rounds.} \item{maximize}{If \code{feval} and \code{early.stop.round} are set, then \code{maximize} must be set as well. \code{maximize=TRUE} means the larger the evaluation score the better.} +\item{save_period}{save the model to the disk in every \code{save_period} rounds, 0 means no such action.} + +\item{save_name}{the name or path for periodically saved model file.} + \item{...}{other parameters to pass to \code{params}.} } \description{ diff --git a/R-package/man/xgboost.Rd b/R-package/man/xgboost.Rd index 64bd00369..5dfeeacee 100644 --- a/R-package/man/xgboost.Rd +++ b/R-package/man/xgboost.Rd @@ -6,7 +6,7 @@ \usage{ xgboost(data = NULL, label = NULL, missing = NULL, params = list(), nrounds, verbose = 1, print.every.n = 1L, early.stop.round = NULL, - maximize = NULL, ...) + maximize = NULL, save_period = 0, save_name = "xgboost.model", ...) } \arguments{ \item{data}{takes \code{matrix}, \code{dgCMatrix}, local data file or @@ -51,6 +51,10 @@ keeps getting worse consecutively for \code{k} rounds.} \item{maximize}{If \code{feval} and \code{early.stop.round} are set, then \code{maximize} must be set as well. \code{maximize=TRUE} means the larger the evaluation score the better.} +\item{save_period}{save the model to the disk in every \code{save_period} rounds, 0 means no such action.} + +\item{save_name}{the name or path for periodically saved model file.} + \item{...}{other parameters to pass to \code{params}.} } \description{