Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-package] [ci] added tests on early_stopping #2848

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,141 @@ test_that("lgb.train() works as expected with sparse features", {
expected_error <- 0.6931268
expect_true(abs(bst$eval_train()[[1L]][["value"]] - expected_error) < TOLERANCE)
})

test_that("lgb.train() works with early stopping for classification", {
trainDF <- data.frame(
"feat1" = rep(c(5.0, 10.0), 500L)
, "target" = rep(c(0L, 1L), 500L)
)
validDF <- data.frame(
"feat1" = rep(c(5.0, 10.0), 50L)
, "target" = rep(c(0L, 1L), 50L)
)
dtrain <- lgb.Dataset(
data = as.matrix(trainDF[["feat1"]], drop = FALSE)
, label = trainDF[["target"]]
)
dvalid <- lgb.Dataset(
data = as.matrix(validDF[["feat1"]], drop = FALSE)
, label = validDF[["target"]]
)
nrounds <- 10L

################################
# train with no early stopping #
################################
bst <- lgb.train(
params = list(
objective = "binary"
, metric = "binary_error"
)
, data = dtrain
, nrounds = nrounds
, valids = list(
"valid1" = dvalid
)
)

# a perfect model should be trivial to obtain, but all 10 rounds
# should happen
expect_equal(bst$best_score, 0.0)
expect_equal(bst$best_iter, 1L)
expect_equal(length(bst$record_evals[["valid1"]][["binary_error"]][["eval"]]), nrounds)

#############################
# train with early stopping #
#############################
early_stopping_rounds <- 5L
bst <- lgb.train(
params = list(
objective = "binary"
, metric = "binary_error"
, early_stopping_rounds = early_stopping_rounds
)
, data = dtrain
, nrounds = nrounds
, valids = list(
"valid1" = dvalid
)
)

# a perfect model should be trivial to obtain, and only 6 rounds
# should have happen (1 with improvement, 5 consecutive with no improvement)
expect_equal(bst$best_score, 0.0)
expect_equal(bst$best_iter, 1L)
expect_equal(
length(bst$record_evals[["valid1"]][["binary_error"]][["eval"]])
, early_stopping_rounds + 1L
)

})

test_that("lgb.train() works with early stopping for regression", {
set.seed(708L)
trainDF <- data.frame(
"feat1" = rep(c(10.0, 100.0), 500L)
, "target" = rep(c(-50.0, 50.0), 500L)
)
validDF <- data.frame(
"feat1" = rep(50.0, 4L)
, "target" = rep(50.0, 4L)
)
dtrain <- lgb.Dataset(
data = as.matrix(trainDF[["feat1"]], drop = FALSE)
, label = trainDF[["target"]]
)
dvalid <- lgb.Dataset(
data = as.matrix(validDF[["feat1"]], drop = FALSE)
, label = validDF[["target"]]
)
nrounds <- 10L

################################
# train with no early stopping #
################################
bst <- lgb.train(
params = list(
objective = "regression"
, metric = "rmse"
, min_data_in_bin = 5L
)
, data = dtrain
, nrounds = nrounds
, valids = list(
"valid1" = dvalid
)
)

# the best possible model should come from the first iteration, but
# all 10 training iterations should happen
expect_equal(bst$best_score, 55.0)
expect_equal(bst$best_iter, 1L)
expect_equal(length(bst$record_evals[["valid1"]][["rmse"]][["eval"]]), nrounds)

#############################
# train with early stopping #
#############################
early_stopping_rounds <- 5L
bst <- lgb.train(
params = list(
objective = "regression"
, metric = "rmse"
, min_data_in_bin = 5L
, early_stopping_rounds = early_stopping_rounds
)
, data = dtrain
, nrounds = nrounds
, valids = list(
"valid1" = dvalid
)
)

# the best model should be from the first iteration, and only 6 rounds
# should have happen (1 with improvement, 5 consecutive with no improvement)
expect_equal(bst$best_score, 55.0)
expect_equal(bst$best_iter, 1L)
expect_equal(
length(bst$record_evals[["valid1"]][["rmse"]][["eval"]])
, early_stopping_rounds + 1L
)
})