From 3f9921762ac1527627d319a46100850aa011ce74 Mon Sep 17 00:00:00 2001 From: hetong007 Date: Sat, 9 May 2015 18:08:47 -0700 Subject: [PATCH] support both early stop name --- R-package/R/xgb.train.R | 18 +++++++++++------- R-package/R/xgboost.R | 11 +++++++---- R-package/man/xgb.train.Rd | 10 ++++++---- R-package/man/xgboost.Rd | 9 ++++++--- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/R-package/R/xgb.train.R b/R-package/R/xgb.train.R index 286b16987..f6e60e23d 100644 --- a/R-package/R/xgb.train.R +++ b/R-package/R/xgb.train.R @@ -66,10 +66,11 @@ #' prediction and dtrain, #' @param verbose If 0, xgboost will stay silent. If 1, xgboost will print #' information of performance. If 2, xgboost will print information of both -#' @param earlyStopRound If \code{NULL}, the early stopping function is not triggered. +#' @param early_stop_round If \code{NULL}, the early stopping function is not triggered. #' If set to an integer \code{k}, training with a validation set will stop if the performance #' keeps getting worse consecutively for \code{k} rounds. -#' @param maximize If \code{feval} and \code{earlyStopRound} are set, then \code{maximize} must be set as well. +#' @param early.stop.round An alternative of \code{early_stop_round}. +#' @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 ... other parameters to pass to \code{params}. #' @@ -119,7 +120,8 @@ #' xgb.train <- function(params=list(), data, nrounds, watchlist = list(), obj = NULL, feval = NULL, verbose = 1, - earlyStopRound = NULL, maximize = NULL, ...) { + early_stop_round = NULL, early.stop.round = NULL, + maximize = NULL, ...) { dtrain <- data if (typeof(params) != "list") { stop("xgb.train: first argument params must be list") @@ -139,7 +141,9 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), params = append(params, list(...)) # Early stopping - if (!is.null(earlyStopRound)){ + if (is.null(early_stop_round) && !is.null(early.stop.round)) + early_stop_round = early.stop.round + if (!is.null(early_stop_round)){ if (!is.null(feval) && is.null(maximize)) stop('Please set maximize to note whether the model is maximizing the evaluation or not.') if (length(watchlist) == 0) @@ -175,7 +179,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), if (length(watchlist) != 0) { msg <- xgb.iter.eval(bst$handle, watchlist, i - 1, feval) cat(paste(msg, "\n", sep="")) - if (!is.null(earlyStopRound)) + if (!is.null(early_stop_round)) { score = strsplit(msg,':|\\s+')[[1]][3] score = as.numeric(score) @@ -183,7 +187,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), bestScore = score bestInd = i } else { - if (i-bestInd>earlyStopRound) { + if (i-bestInd>early_stop_round) { earlyStopflag = TRUE cat('Stopping. Best iteration:',bestInd) break @@ -193,7 +197,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), } } bst <- xgb.Booster.check(bst) - if (!is.null(earlyStopRound)) { + if (!is.null(early_stop_round)) { bst$bestScore = bestScore bst$bestInd = bestInd } diff --git a/R-package/R/xgboost.R b/R-package/R/xgboost.R index 367a149e7..4e4fbaa2c 100644 --- a/R-package/R/xgboost.R +++ b/R-package/R/xgboost.R @@ -30,10 +30,11 @@ #' performance and construction progress information #' @param missing Missing is only used when input is dense matrix, pick a float #' value that represents missing value. Sometimes a data use 0 or other extreme value to represents missing values. -#' @param earlyStopRound If \code{NULL}, the early stopping function is not triggered. +#' @param early_stop_round If \code{NULL}, the early stopping function is not triggered. #' If set to an integer \code{k}, training with a validation set will stop if the performance #' keeps getting worse consecutively for \code{k} rounds. -#' @param maximize If \code{feval} and \code{earlyStopRound} are set, then \code{maximize} must be set as well. +#' @param early.stop.round An alternative of \code{early_stop_round}. +#' @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 ... other parameters to pass to \code{params}. #' @@ -56,7 +57,8 @@ #' @export #' xgboost <- function(data = NULL, label = NULL, missing = NULL, params = list(), nrounds, - verbose = 1, earlyStopRound = NULL, maximize = NULL, ...) { + verbose = 1, early_stop_round = NULL, early.stop.round = NULL, + maximize = NULL, ...) { if (is.null(missing)) { dtrain <- xgb.get.DMatrix(data, label) } else { @@ -72,7 +74,8 @@ xgboost <- function(data = NULL, label = NULL, missing = NULL, params = list(), } bst <- xgb.train(params, dtrain, nrounds, watchlist, verbose = verbose, - earlyStopRound = earlyStopRound) + early_stop_round = early_stop_round, + early.stop.round = early.stop.round) return(bst) } diff --git a/R-package/man/xgb.train.Rd b/R-package/man/xgb.train.Rd index fa5ede220..a3317f1ab 100644 --- a/R-package/man/xgb.train.Rd +++ b/R-package/man/xgb.train.Rd @@ -5,8 +5,8 @@ \title{eXtreme Gradient Boosting Training} \usage{ xgb.train(params = list(), data, nrounds, watchlist = list(), obj = NULL, - feval = NULL, verbose = 1, earlyStopRound = NULL, maximize = NULL, - ...) + feval = NULL, verbose = 1, early_stop_round = NULL, + early.stop.round = NULL, maximize = NULL, ...) } \arguments{ \item{params}{the list of parameters. @@ -78,11 +78,13 @@ prediction and dtrain,} \item{verbose}{If 0, xgboost will stay silent. If 1, xgboost will print information of performance. If 2, xgboost will print information of both} -\item{earlyStopRound}{If \code{NULL}, the early stopping function is not triggered. +\item{early_stop_round}{If \code{NULL}, the early stopping function is not triggered. If set to an integer \code{k}, training with a validation set will stop if the performance keeps getting worse consecutively for \code{k} rounds.} -\item{maximize}{If \code{feval} and \code{earlyStopRound} are set, then \code{maximize} must be set as well. +\item{early.stop.round}{An alternative of \code{early_stop_round}.} + +\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{...}{other parameters to pass to \code{params}.} diff --git a/R-package/man/xgboost.Rd b/R-package/man/xgboost.Rd index 9509dbd39..01c519e2e 100644 --- a/R-package/man/xgboost.Rd +++ b/R-package/man/xgboost.Rd @@ -5,7 +5,8 @@ \title{eXtreme Gradient Boosting (Tree) library} \usage{ xgboost(data = NULL, label = NULL, missing = NULL, params = list(), - nrounds, verbose = 1, earlyStopRound = NULL, maximize = NULL, ...) + nrounds, verbose = 1, early_stop_round = NULL, early.stop.round = NULL, + maximize = NULL, ...) } \arguments{ \item{data}{takes \code{matrix}, \code{dgCMatrix}, local data file or @@ -41,11 +42,13 @@ Commonly used ones are: information of performance. If 2, xgboost will print information of both performance and construction progress information} -\item{earlyStopRound}{If \code{NULL}, the early stopping function is not triggered. +\item{early_stop_round}{If \code{NULL}, the early stopping function is not triggered. If set to an integer \code{k}, training with a validation set will stop if the performance keeps getting worse consecutively for \code{k} rounds.} -\item{maximize}{If \code{feval} and \code{earlyStopRound} are set, then \code{maximize} must be set as well. +\item{early.stop.round}{An alternative of \code{early_stop_round}.} + +\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{...}{other parameters to pass to \code{params}.}