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

improved hotstart stack #719

Merged
merged 7 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ docs/

# Local History for Visual Studio Code
.history/

.Rprofile
39 changes: 27 additions & 12 deletions R/HotstartStack.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ HotstartStack = R6Class("HotstartStack",
#' Creates a new instance of this [R6][R6::R6Class] class.
#'
#' @param learners (List of [Learner]s).
initialize = function(learners) {
learners = assert_learners(as_learners(learners))
self$stack = data.table(
start_learner = learners,
task_hash = map_chr(learners, function(l) l$state$task_hash),
learner_hash = map_chr(learners, learner_hotstart_hash),
key = c("task_hash", "learner_hash"))
initialize = function(learners = NULL) {
self$stack = data.table()

# add learners to stack
if (!is.null(learners)) self$add(learners)
},

#' @description
Expand All @@ -67,13 +65,13 @@ HotstartStack = R6Class("HotstartStack",
add = function(learners) {
learners = assert_learners(as_learners(learners))

rows = data.table(
start_learner = learners,
task_hash = map_chr(learners, function(l) l$state$task_hash),
learner_hash = map_chr(learners, learner_hotstart_hash))
# hashes
task_hash = map_chr(learners, function(learner) learner$state$task_hash)
learner_hash = map_chr(learners, learner_hotstart_hash)

self$stack = rbindlist(list(self$stack, rows))
self$stack = rbindlist(list(self$stack, data.table(start_learner = learners, task_hash, learner_hash)))
setkeyv(self$stack, c("task_hash", "learner_hash"))

invisible(self)
},

Expand All @@ -92,6 +90,7 @@ HotstartStack = R6Class("HotstartStack",
#'
# @return `numeric()`.
start_cost = function(learner, task_hash) {
if(!nrow(self$stack)) return(numeric(0))
.learner_hash = learner_hotstart_hash(assert_learner(learner))
.task_hash = assert_string(task_hash)
hotstart_id = learner$param_set$ids(tags = "hotstart")
Expand All @@ -101,6 +100,21 @@ HotstartStack = R6Class("HotstartStack",
][, get("cost")]
self$stack[, "cost" := NULL]
cost
},

#' @description
#' Helper for print outputs.
format = function() {
sprintf("<%s>", class(self)[1L])
},

#' @description
#' Printer.
#'
#' @param ... (ignored).
print = function() {
catf(format(self))
print(self$stack, digits = 2)
}
),

Expand All @@ -112,6 +126,7 @@ HotstartStack = R6Class("HotstartStack",
# `resample()` and `benchmark()` which call `learner_train(learner, task,
# row_ids, mode = 'retrain')` with the returned learner.
.start_learner = function(learner, task_hash) {
if(!nrow(self$stack)) return(NULL)
.learner_hash = learner_hotstart_hash(assert_learner(learner))
.task_hash = assert_character(task_hash, len = 1)
hotstart_id = learner$param_set$ids(tags = "hotstart")
Expand Down
2 changes: 1 addition & 1 deletion R/Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ Learner = R6Class("Learner",
if (missing(rhs)) {
return(private$.hotstart_stack)
}
assert_r6(rhs, "HotstartStack")
assert_r6(rhs, "HotstartStack", null.ok = TRUE)
private$.hotstart_stack = rhs
}
),
Expand Down
9 changes: 7 additions & 2 deletions R/benchmark.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ benchmark = function(design, store_models = FALSE, store_backends = TRUE, encaps
}
data.table(learner = list(learner), mode = mode)
})
# null hotstart stack to reduce overhead in parallelization
map(hotstart_grid$learner, function(learner) {
learner$hotstart_stack = NULL
learner
})
set(grid, j = "learner", value = hotstart_grid$learner)
set(grid, j = "mode", value = hotstart_grid$mode)
}
Expand All @@ -157,8 +162,8 @@ benchmark = function(design, store_models = FALSE, store_backends = TRUE, encaps
task = grid$task, learner = grid$learner, resampling = grid$resampling, iteration = grid$iteration,
mode = grid$mode,
MoreArgs = list(store_models = store_models, lgr_threshold = lg$threshold, pb = pb),
SIMPLIFY = FALSE, USE.NAMES = FALSE, future.globals = FALSE,
future.scheduling = structure(TRUE, ordering = "random"), future.packages = "mlr3", future.seed = TRUE,
SIMPLIFY = FALSE, USE.NAMES = FALSE, future.globals = FALSE,
future.scheduling = structure(TRUE, ordering = "random"), future.packages = "mlr3", future.seed = TRUE,
future.stdout = future_stdout()
)
}
Expand Down
12 changes: 9 additions & 3 deletions R/resample.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ resample = function(task, learner, resampling, store_models = FALSE, store_backe
}

grid = if (allow_hotstart) {
map_dtr(seq_len(n), function(iteration) {
hotstart_grid = map_dtr(seq_len(n), function(iteration) {
if (!is.null(learner$hotstart_stack)) {
# search for hotstart learner
task_hashes = task_hashes(task, resampling)
Expand All @@ -91,13 +91,19 @@ resample = function(task, learner, resampling, store_models = FALSE, store_backe
}
data.table(learner = list(learner), mode = mode)
})
# null hotstart stack to reduce overhead in parallelization
map(hotstart_grid$learner, function(learner) {
learner$hotstart_stack = NULL
learner
})
hotstart_grid
} else {
data.table(learner = replicate(n, learner), mode = "train")
}

if (getOption("mlr3.debug", FALSE)) {
lg$info("Running resample() sequentially in debug mode with %i iterations", n)
res = mapply(workhorse,
res = mapply(workhorse,
iteration = seq_len(n), learner = grid$learner, mode = grid$mode,
MoreArgs = list(task = task, resampling = instance, store_models = store_models, lgr_threshold = lg$threshold,
pb = pb), SIMPLIFY = FALSE
Expand All @@ -116,7 +122,7 @@ resample = function(task, learner, resampling, store_models = FALSE, store_backe

data = data.table(
task = list(task),
learner = list(learner),
learner = grid$learner,
learner_state = map(res, "learner_state"),
resampling = list(instance),
iteration = seq_len(n),
Expand Down
Loading