Fix r early stop with custom objective. (#5923)

* Specify `ntreelimit`.
This commit is contained in:
Jiaming Yuan
2020-07-23 03:28:17 +08:00
committed by GitHub
parent 30363d9c35
commit bc1d3ee230
3 changed files with 12 additions and 4 deletions

View File

@@ -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))
}
@@ -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')