support both early stop name

This commit is contained in:
hetong007 2015-05-09 18:08:47 -07:00
parent f6fc38f7af
commit 3f9921762a
4 changed files with 30 additions and 18 deletions

View File

@ -66,10 +66,11 @@
#' prediction and dtrain, #' prediction and dtrain,
#' @param verbose If 0, xgboost will stay silent. If 1, xgboost will print #' @param verbose If 0, xgboost will stay silent. If 1, xgboost will print
#' information of performance. If 2, xgboost will print information of both #' 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 #' 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. #' 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. #' \code{maximize=TRUE} means the larger the evaluation score the better.
#' @param ... other parameters to pass to \code{params}. #' @param ... other parameters to pass to \code{params}.
#' #'
@ -119,7 +120,8 @@
#' #'
xgb.train <- function(params=list(), data, nrounds, watchlist = list(), xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
obj = NULL, feval = NULL, verbose = 1, obj = NULL, feval = NULL, verbose = 1,
earlyStopRound = NULL, maximize = NULL, ...) { early_stop_round = NULL, early.stop.round = NULL,
maximize = NULL, ...) {
dtrain <- data dtrain <- data
if (typeof(params) != "list") { if (typeof(params) != "list") {
stop("xgb.train: first argument params must be 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(...)) params = append(params, list(...))
# Early stopping # 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)) if (!is.null(feval) && is.null(maximize))
stop('Please set maximize to note whether the model is maximizing the evaluation or not.') stop('Please set maximize to note whether the model is maximizing the evaluation or not.')
if (length(watchlist) == 0) if (length(watchlist) == 0)
@ -175,7 +179,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
if (length(watchlist) != 0) { if (length(watchlist) != 0) {
msg <- xgb.iter.eval(bst$handle, watchlist, i - 1, feval) msg <- xgb.iter.eval(bst$handle, watchlist, i - 1, feval)
cat(paste(msg, "\n", sep="")) cat(paste(msg, "\n", sep=""))
if (!is.null(earlyStopRound)) if (!is.null(early_stop_round))
{ {
score = strsplit(msg,':|\\s+')[[1]][3] score = strsplit(msg,':|\\s+')[[1]][3]
score = as.numeric(score) score = as.numeric(score)
@ -183,7 +187,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
bestScore = score bestScore = score
bestInd = i bestInd = i
} else { } else {
if (i-bestInd>earlyStopRound) { if (i-bestInd>early_stop_round) {
earlyStopflag = TRUE earlyStopflag = TRUE
cat('Stopping. Best iteration:',bestInd) cat('Stopping. Best iteration:',bestInd)
break break
@ -193,7 +197,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
} }
} }
bst <- xgb.Booster.check(bst) bst <- xgb.Booster.check(bst)
if (!is.null(earlyStopRound)) { if (!is.null(early_stop_round)) {
bst$bestScore = bestScore bst$bestScore = bestScore
bst$bestInd = bestInd bst$bestInd = bestInd
} }

View File

@ -30,10 +30,11 @@
#' performance and construction progress information #' performance and construction progress information
#' @param missing Missing is only used when input is dense matrix, pick a float #' @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. #' 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 #' 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. #' 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. #' \code{maximize=TRUE} means the larger the evaluation score the better.
#' @param ... other parameters to pass to \code{params}. #' @param ... other parameters to pass to \code{params}.
#' #'
@ -56,7 +57,8 @@
#' @export #' @export
#' #'
xgboost <- function(data = NULL, label = NULL, missing = NULL, params = list(), nrounds, 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)) { if (is.null(missing)) {
dtrain <- xgb.get.DMatrix(data, label) dtrain <- xgb.get.DMatrix(data, label)
} else { } else {
@ -72,7 +74,8 @@ xgboost <- function(data = NULL, label = NULL, missing = NULL, params = list(),
} }
bst <- xgb.train(params, dtrain, nrounds, watchlist, verbose = verbose, 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) return(bst)
} }

View File

@ -5,8 +5,8 @@
\title{eXtreme Gradient Boosting Training} \title{eXtreme Gradient Boosting Training}
\usage{ \usage{
xgb.train(params = list(), data, nrounds, watchlist = list(), obj = NULL, 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{ \arguments{
\item{params}{the list of parameters. \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 \item{verbose}{If 0, xgboost will stay silent. If 1, xgboost will print
information of performance. If 2, xgboost will print information of both} 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 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.} 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.} \code{maximize=TRUE} means the larger the evaluation score the better.}
\item{...}{other parameters to pass to \code{params}.} \item{...}{other parameters to pass to \code{params}.}

View File

@ -5,7 +5,8 @@
\title{eXtreme Gradient Boosting (Tree) library} \title{eXtreme Gradient Boosting (Tree) library}
\usage{ \usage{
xgboost(data = NULL, label = NULL, missing = NULL, params = list(), 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{ \arguments{
\item{data}{takes \code{matrix}, \code{dgCMatrix}, local data file or \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 information of performance. If 2, xgboost will print information of both
performance and construction progress information} 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 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.} 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.} \code{maximize=TRUE} means the larger the evaluation score the better.}
\item{...}{other parameters to pass to \code{params}.} \item{...}{other parameters to pass to \code{params}.}