From d2bf1223129fae7efa8f18212f7ca139f820b404 Mon Sep 17 00:00:00 2001 From: fis Date: Wed, 22 Jul 2020 14:12:18 +0800 Subject: [PATCH 1/4] Specify ntreelimit. --- R-package/R/utils.R | 5 +++-- cmake/RPackageInstall.cmake.in | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/R-package/R/utils.R b/R-package/R/utils.R index 3c4e091834a9..894e220316cf 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -145,7 +145,8 @@ xgb.iter.update <- function(booster_handle, dtrain, iter, obj = NULL) { if (is.null(obj)) { .Call(XGBoosterUpdateOneIter_R, booster_handle, as.integer(iter), dtrain) } else { - pred <- predict(booster_handle, dtrain, outputmargin = TRUE, training = TRUE) + pred <- predict(booster_handle, dtrain, outputmargin = TRUE, training = TRUE, + ntreelimit = 0) gpair <- obj(pred, dtrain) .Call(XGBoosterBoostOneIter_R, booster_handle, dtrain, gpair$grad, gpair$hess) } @@ -172,7 +173,7 @@ xgb.iter.eval <- function(booster_handle, watchlist, iter, feval = NULL) { } else { res <- sapply(seq_along(watchlist), function(j) { w <- watchlist[[j]] - preds <- predict(booster_handle, w) # predict using all trees + preds <- predict(booster_handle, w, ntreelimit = 0) # predict using all trees eval_res <- feval(preds, w) out <- eval_res$value names(out) <- paste0(evnames[j], "-", eval_res$metric) diff --git a/cmake/RPackageInstall.cmake.in b/cmake/RPackageInstall.cmake.in index 3701518fb339..d9d783b41ab3 100644 --- a/cmake/RPackageInstall.cmake.in +++ b/cmake/RPackageInstall.cmake.in @@ -31,4 +31,4 @@ set(XGB_DEPS_SCRIPT check_call(COMMAND "${LIBR_EXECUTABLE}" -q -e "${XGB_DEPS_SCRIPT}") # Install the XGBoost R package -check_call(COMMAND "${LIBR_EXECUTABLE}" CMD INSTALL --no-multiarch --build "${build_dir}/R-package") \ No newline at end of file +check_call(COMMAND "${LIBR_EXECUTABLE}" CMD INSTALL --with-keep.source --no-multiarch --build "${build_dir}/R-package") From 7403a28c2f4979467b37bd2ea2cc86fb86200065 Mon Sep 17 00:00:00 2001 From: fis Date: Wed, 22 Jul 2020 14:37:12 +0800 Subject: [PATCH 2/4] Fix test. --- R-package/tests/testthat/test_custom_objective.R | 2 +- tests/python/test_basic_models.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R-package/tests/testthat/test_custom_objective.R b/R-package/tests/testthat/test_custom_objective.R index 8b504e7f52e0..1b74ac788161 100644 --- a/R-package/tests/testthat/test_custom_objective.R +++ b/R-package/tests/testthat/test_custom_objective.R @@ -20,7 +20,7 @@ logregobj <- function(preds, dtrain) { evalerror <- function(preds, dtrain) { labels <- getinfo(dtrain, "label") - err <- as.numeric(sum(labels != (preds > 0))) / length(labels) + err <- as.numeric(sum(labels != (preds > 0.5))) / length(labels) return(list(metric = "error", value = err)) } diff --git a/tests/python/test_basic_models.py b/tests/python/test_basic_models.py index 9b49c5360e0c..7a1bc8c1f8f3 100644 --- a/tests/python/test_basic_models.py +++ b/tests/python/test_basic_models.py @@ -210,7 +210,7 @@ def logregobj(preds, dtrain): def evalerror(preds, dtrain): labels = dtrain.get_label() - return 'error', float(sum(labels != (preds > 0.0))) / len(labels) + return 'error', float(sum(labels != (preds > 0.5))) / len(labels) # test custom_objective in training bst = xgb.train(param, dtrain, num_round, watchlist, logregobj, evalerror) From 0d1b0b3ad0dc8a34a2199484df391134762c5ab7 Mon Sep 17 00:00:00 2001 From: fis Date: Wed, 22 Jul 2020 15:00:18 +0800 Subject: [PATCH 3/4] Add a simple test. --- R-package/tests/testthat/test_custom_objective.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R-package/tests/testthat/test_custom_objective.R b/R-package/tests/testthat/test_custom_objective.R index 1b74ac788161..65bda250b0d9 100644 --- a/R-package/tests/testthat/test_custom_objective.R +++ b/R-package/tests/testthat/test_custom_objective.R @@ -43,6 +43,13 @@ test_that("custom objective in CV works", { expect_lt(cv$evaluation_log[num_round, test_error_mean], 0.03) }) +test_that("custom objective with early stop works", { + bst <- xgb.train(param, dtrain, 10, watchlist) + expect_equal(class(bst), "xgb.Booster") + train_log <- bst$evaluation_log$train_error + expect_true(all(diff(train_log)) <= 0) +}) + test_that("custom objective using DMatrix attr works", { attr(dtrain, 'label') <- getinfo(dtrain, 'label') From 4d38bf9aaa1a25413a115ef70ded00e70e178523 Mon Sep 17 00:00:00 2001 From: fis Date: Wed, 22 Jul 2020 15:03:08 +0800 Subject: [PATCH 4/4] Revert. --- cmake/RPackageInstall.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/RPackageInstall.cmake.in b/cmake/RPackageInstall.cmake.in index d9d783b41ab3..3701518fb339 100644 --- a/cmake/RPackageInstall.cmake.in +++ b/cmake/RPackageInstall.cmake.in @@ -31,4 +31,4 @@ set(XGB_DEPS_SCRIPT check_call(COMMAND "${LIBR_EXECUTABLE}" -q -e "${XGB_DEPS_SCRIPT}") # Install the XGBoost R package -check_call(COMMAND "${LIBR_EXECUTABLE}" CMD INSTALL --with-keep.source --no-multiarch --build "${build_dir}/R-package") +check_call(COMMAND "${LIBR_EXECUTABLE}" CMD INSTALL --no-multiarch --build "${build_dir}/R-package") \ No newline at end of file