Commit 256547fa authored by Laurae's avatar Laurae Committed by Guolin Ke
Browse files

[R-package] Merge iterations and early stopping with params (#829)

* Prepare merging parameters

* Add parameters from params for lgb.train

* Usage of params in lgb.cv for rounds/early stop

* Double bracket (1)

* Double brackets (2)
parent 36732f23
......@@ -137,7 +137,12 @@ lgb.cv <- function(params = list(),
if (!is.null(predictor)) {
begin_iteration <- predictor$current_iter() + 1
}
# Check for number of rounds passed as parameter - in case there are multiple ones, take only the first one
if (sum(names(params) %in% c("num_iterations", "num_iteration", "num_tree", "num_trees", "num_round", "num_rounds")) > 0) {
end_iteration <- begin_iteration + params[[which(names(params) %in% c("num_iterations", "num_iteration", "num_tree", "num_trees", "num_round", "num_rounds"))[1]]] - 1
} else {
end_iteration <- begin_iteration + nrounds - 1
}
# Check for training dataset type correctness
if (!lgb.is.Dataset(data)) {
......@@ -209,12 +214,18 @@ lgb.cv <- function(params = list(),
callbacks <- add.cb(callbacks, cb.record.evaluation())
}
# Add early stopping callback
# Check for early stopping passed as parameter when adding early stopping callback
if (sum(names(params) %in% c("early_stopping_round", "early_stopping_rounds", "early_stopping")) > 0) {
if (params[[which(names(params) %in% c("early_stopping_round", "early_stopping_rounds", "early_stopping"))[1]]] > 0) {
callbacks <- add.cb(callbacks, cb.early.stop(params[[which(names(params) %in% c("early_stopping_round", "early_stopping_rounds", "early_stopping"))[1]]], verbose = verbose))
}
} else {
if (!is.null(early_stopping_rounds)) {
if (early_stopping_rounds > 0) {
callbacks <- add.cb(callbacks, cb.early.stop(early_stopping_rounds, verbose = verbose))
}
}
}
# Categorize callbacks
cb <- categorize.callbacks(callbacks)
......
......@@ -112,7 +112,13 @@ lgb.train <- function(params = list(),
if (!is.null(predictor)) {
begin_iteration <- predictor$current_iter() + 1
}
# Check for number of rounds passed as parameter - in case there are multiple ones, take only the first one
if (sum(names(params) %in% c("num_iterations", "num_iteration", "num_tree", "num_trees", "num_round", "num_rounds")) > 0) {
end_iteration <- begin_iteration + params[[which(names(params) %in% c("num_iterations", "num_iteration", "num_tree", "num_trees", "num_round", "num_rounds"))[1]]] - 1
} else {
end_iteration <- begin_iteration + nrounds - 1
}
# Check for training dataset type correctness
if (!lgb.is.Dataset(data)) {
......@@ -195,12 +201,18 @@ lgb.train <- function(params = list(),
callbacks <- add.cb(callbacks, cb.record.evaluation())
}
# Add early stopping callback
# Check for early stopping passed as parameter when adding early stopping callback
if (sum(names(params) %in% c("early_stopping_round", "early_stopping_rounds", "early_stopping")) > 0) {
if (params[[which(names(params) %in% c("early_stopping_round", "early_stopping_rounds", "early_stopping"))[1]]] > 0) {
callbacks <- add.cb(callbacks, cb.early.stop(params[[which(names(params) %in% c("early_stopping_round", "early_stopping_rounds", "early_stopping"))[1]]], verbose = verbose))
}
} else {
if (!is.null(early_stopping_rounds)) {
if (early_stopping_rounds > 0) {
callbacks <- add.cb(callbacks, cb.early.stop(early_stopping_rounds, verbose = verbose))
}
}
}
# "Categorize" callbacks
cb <- categorize.callbacks(callbacks)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment