Skip to content

Commit

Permalink
[R-package] Merge iterations and early stopping with params (#829)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Laurae2 authored and guolinke committed Aug 18, 2017
1 parent 36732f2 commit 256547f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
23 changes: 17 additions & 6 deletions R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ lgb.cv <- function(params = list(),
if (!is.null(predictor)) {
begin_iteration <- predictor$current_iter() + 1
}
end_iteration <- begin_iteration + nrounds - 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)) {
Expand All @@ -146,7 +151,7 @@ lgb.cv <- function(params = list(),
}
data <- lgb.Dataset(data, label = label)
}

# Check for weights
if (!is.null(weight)) {
data$set_info("weight", weight)
Expand Down Expand Up @@ -209,10 +214,16 @@ lgb.cv <- function(params = list(),
callbacks <- add.cb(callbacks, cb.record.evaluation())
}

# Add early stopping callback
if (!is.null(early_stopping_rounds)) {
if (early_stopping_rounds > 0) {
callbacks <- add.cb(callbacks, cb.early.stop(early_stopping_rounds, verbose = verbose))
# 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))
}
}
}

Expand Down
22 changes: 17 additions & 5 deletions R-package/R/lgb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ lgb.train <- function(params = list(),
if (!is.null(predictor)) {
begin_iteration <- predictor$current_iter() + 1
}
end_iteration <- begin_iteration + nrounds - 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)) {
Expand Down Expand Up @@ -195,10 +201,16 @@ lgb.train <- function(params = list(),
callbacks <- add.cb(callbacks, cb.record.evaluation())
}

# Add early stopping callback
if (!is.null(early_stopping_rounds)) {
if (early_stopping_rounds > 0) {
callbacks <- add.cb(callbacks, cb.early.stop(early_stopping_rounds, verbose = verbose))
# 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))
}
}
}

Expand Down

0 comments on commit 256547f

Please sign in to comment.