Skip to content

Commit

Permalink
rename inner to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
sebffischer committed May 7, 2024
1 parent 5556ec2 commit 6d851c6
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ S3method(as_pipeop,Filter)
S3method(as_pipeop,Learner)
S3method(as_pipeop,PipeOp)
S3method(as_pipeop,default)
S3method(disable_inner_tuning,GraphLearner)
S3method(disable_internal_tuning,GraphLearner)
S3method(po,"NULL")
S3method(po,Filter)
S3method(po,Learner)
Expand Down
50 changes: 25 additions & 25 deletions R/GraphLearner.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
#' contain the model. Use `graph_model` to access the trained [`Graph`] after `$train()`. Read-only.
#' * `graph_model` :: [`Learner`][mlr3::Learner]\cr
#' [`Graph`] that is being wrapped. This [`Graph`] contains a trained state after `$train()`. Read-only.
#' * `inner_tuned_values` :: named `list()` or `NULL`\cr
#' The inner tuned parameter values.
#' `NULL` is returned if the learner is not trained or none of the wrapped learners supports inner tuning.
#' * `inner_valid_scores` :: named `list()` or `NULL`\cr
#' The inner tuned parameter values.
#' * `internal_tuned_values` :: named `list()` or `NULL`\cr
#' The internal tuned parameter values.
#' `NULL` is returned if the learner is not trained or none of the wrapped learners supports internal tuning.
#' * `internal_valid_scores` :: named `list()` or `NULL`\cr
#' The internal tuned parameter values.
#' `NULL` is returned if the learner is not trained or none of the wrapped learners supports internal validation.
#' * `validate` :: `numeric(1)`, `"inner_valid"`, `"test"` or `NULL`\cr
#' * `validate` :: `numeric(1)`, `"predefined"`, `"test"` or `NULL`\cr
#' How to construct the validation data. This also has to be configured in the individual learners wrapped by
#' `PipeOpLearner`, see [`set_validate.GraphLearner`] on how to configure this.
#'
Expand Down Expand Up @@ -111,10 +111,10 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner,
assert_subset(task_type, mlr_reflections$task_types$type)

private$.can_validate = some(learner_wrapping_pipeops(graph), function(po) "validation" %in% po$learner$properties)
private$.can_inner_tuning = some(learner_wrapping_pipeops(graph), function(po) "inner_tuning" %in% po$learner$properties)
private$.can_internal_tuning = some(learner_wrapping_pipeops(graph), function(po) "internal_tuning" %in% po$learner$properties)

properties = setdiff(mlr_reflections$learner_properties[[task_type]],
c("validation", "inner_tuning")[!c(private$.can_validate, private$.can_inner_tuning)])
c("validation", "internal_tuning")[!c(private$.can_validate, private$.can_internal_tuning)])

super$initialize(id = id, task_type = task_type,
feature_types = mlr_reflections$task_feature_types,
Expand Down Expand Up @@ -153,13 +153,13 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner,
}
),
active = list(
inner_valid_scores = function(rhs) {
internal_valid_scores = function(rhs) {
assert_ro_binding(rhs)
self$state$inner_valid_scores
self$state$internal_valid_scores
},
inner_tuned_values = function(rhs) {
internal_tuned_values = function(rhs) {
assert_ro_binding(rhs)
self$state$inner_tuned_values
self$state$internal_tuned_values
},
validate = function(rhs) {
if (!missing(rhs)) {
Expand Down Expand Up @@ -212,25 +212,25 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner,
.graph = NULL,
.validate = NULL,
.can_validate = NULL,
.can_inner_tuning = NULL,
.extract_inner_tuned_values = function() {
.can_internal_tuning = NULL,
.extract_internal_tuned_values = function() {
if (!private$.can_validate) return(NULL)
itvs = unlist(map(
learner_wrapping_pipeops(self$graph_model), function(po) {
if (exists("inner_tuned_values", po$learner)) {
po$learner_model$inner_tuned_values
if (exists("internal_tuned_values", po$learner)) {
po$learner_model$internal_tuned_values
}
}
), recursive = FALSE)
if (is.null(itvs) || !length(itvs)) return(named_list())
itvs
},
.extract_inner_valid_scores = function() {
if (!private$.can_inner_tuning) return(NULL)
.extract_internal_valid_scores = function() {
if (!private$.can_internal_tuning) return(NULL)
ivs = unlist(map(
learner_wrapping_pipeops(self$graph_model), function(po) {
if (exists("inner_valid_scores", po$learner)) {
po$learner_model$inner_valid_scores
if (exists("internal_valid_scores", po$learner)) {
po$learner_model$internal_valid_scores
}
}
), recursive = FALSE)
Expand Down Expand Up @@ -314,11 +314,11 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner,
#' 2. On the level of the [`Learner`]s that are wrapped by [`PipeOpLearner`] and [`PipeOpLearnerCV`], which specifies
#' which pipeops actually make use of the validation data.
#' All learners wrapped by [`PipeOpLearner`] and [`PipeOpLearnerCV`] should in almost all cases either set it
#' to `NULL` (disable) or `"inner_valid"` (enable).
#' to `NULL` (disable) or `"predefined"` (enable).
#'
#' @param learner ([`GraphLearner`])\cr
#' The graph learner to configure.
#' @param validate (`numeric(1)`, `"inner_valid"`, `"test"`, or `NULL`)\cr
#' @param validate (`numeric(1)`, `"predefined"`, `"test"`, or `NULL`)\cr
#' How to set the `$validate` field of the learner.
#' If set to `NULL` all validation is disabled, both on the graph learner level, but also for all pipeops.
#' @param ids (`NULL` or `character()`)\cr
Expand Down Expand Up @@ -384,7 +384,7 @@ set_validate.GraphLearner = function(learner, validate, ids = NULL, args = list(
walk(ids, function(poid) {
# learner might be another GraphLearner / AutoTuner so we call into set_validate() again
withCallingHandlers({
invoke(set_validate, learner = learner$graph$pipeops[[poid]]$learner, .args = insert_named(list(validate = "inner_valid"), args[[poid]]))
invoke(set_validate, learner = learner$graph$pipeops[[poid]]$learner, .args = insert_named(list(validate = "predefined"), args[[poid]]))
}, error = function(e) {
e$message = sprintf("Failed to set validate for PipeOp '%s':\n%s", poid, e$message)
stop(e)
Expand All @@ -401,12 +401,12 @@ set_validate.GraphLearner = function(learner, validate, ids = NULL, args = list(


#' @export
disable_inner_tuning.GraphLearner = function(learner, ids, ...) {
disable_internal_tuning.GraphLearner = function(learner, ids, ...) {
pvs = learner$param_set$values
on.exit({learner$param_set$values = pvs}, add = TRUE)
if (length(ids)) {
walk(learner_wrapping_pipeops(learner), function(po) {
disable_inner_tuning(po$learner, ids = po$param_set$ids()[sprintf("%s.%s", po$id, po$param_set$ids()) %in% ids])
disable_internal_tuning(po$learner, ids = po$param_set$ids()[sprintf("%s.%s", po$id, po$param_set$ids()) %in% ids])
})
}
on.exit()
Expand Down
4 changes: 2 additions & 2 deletions R/PipeOpImpute.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ PipeOpImpute = R6Class("PipeOpImpute",

self$state$outtasklayout = copy(intask$feature_types)

if (!is.null(intask$inner_valid_task)) {
intask$inner_valid_task = private$.predict(list(intask$inner_valid_task))[[1L]]
if (!is.null(intask$internal_valid_task)) {
intask$internal_valid_task = private$.predict(list(intask$internal_valid_task))[[1L]]
}

list(intask)
Expand Down
4 changes: 2 additions & 2 deletions R/PipeOpTaskPreproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ PipeOpTaskPreproc = R6Class("PipeOpTaskPreproc",
self$state$outtasklayout = copy(intask$feature_types)
self$state$outtaskshell = intask$data(rows = intask$row_ids[0])

if (!is.null(intask$inner_valid_task)) {
if (!is.null(intask$internal_valid_task)) {
# we call into .predict() and not .predict_task() to not put the burden
# of subsetting the features etc. on the PipeOp overwriting .predict_task
intask$inner_valid_task = private$.predict(list(intask$inner_valid_task))[[1L]]
intask$internal_valid_task = private$.predict(list(intask$internal_valid_task))[[1L]]
}

if (do_subset) {
Expand Down
6 changes: 3 additions & 3 deletions inst/testthat/helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ expect_datapreproc_pipeop_class = function(poclass, constargs = list(), task,
tasktrain$row_roles$use = tasktrain$row_roles$use[seq(1, n_use - 2)]

taskpredict = tasktrain$clone(deep = TRUE)
taskpredict$row_roles$use = taskpredict$inner_valid_task$row_roles$use
taskpredict$inner_valid_task = NULL
taskpredict$row_roles$use = taskpredict$internal_valid_task$row_roles$use
taskpredict$internal_valid_task = NULL

taskouttrain = po$train(list(tasktrain))[[1L]]
taskoutpredict = po$predict(list(taskpredict))[[1L]]

# other columns like weights are present during traing but not during predict
cols = unname(unlist(taskouttrain$col_roles[c("feature", "target")]))
dtrain = taskouttrain$inner_valid_task$data(cols = cols)
dtrain = taskouttrain$internal_valid_task$data(cols = cols)
dpredict = taskoutpredict$data(cols = cols)
expect_permutation(colnames(dtrain), colnames(dpredict))
expect_equal(nrow(dtrain), nrow(dpredict))
Expand Down
2 changes: 1 addition & 1 deletion man/mlr_learners_avg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/mlr_learners_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mlr_pipeops_tunethreshold.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/set_validate.GraphLearner.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 26 additions & 26 deletions tests/testthat/test_GraphLearner.R
Original file line number Diff line number Diff line change
Expand Up @@ -578,86 +578,86 @@ test_that("GraphLearner hashes", {

})

test_that("validation, inner_valid_scores", {
test_that("validation, internal_valid_scores", {
# None of the Learners can do validation -> NULL
glrn1 = as_learner(as_graph(lrn("classif.rpart")))$train(tsk("iris"))
expect_false("validation" %in% glrn1$properties)
expect_equal(glrn1$inner_valid_scores, NULL)
expect_equal(glrn1$internal_valid_scores, NULL)

glrn2 = as_learner(as_graph(lrn("classif.debug")))
expect_true("validation" %in% glrn2$properties)
set_validate(glrn2, 0.2)
expect_equal(glrn2$validate, 0.2)
expect_equal(glrn2$graph$pipeops$classif.debug$learner$validate, "inner_valid")
expect_equal(glrn2$graph$pipeops$classif.debug$learner$validate, "predefined")
glrn2$train(tsk("iris"))
expect_list(glrn2$inner_valid_scores, types = "numeric")
expect_equal(names(glrn2$inner_valid_scores), "classif.debug.acc")
expect_list(glrn2$internal_valid_scores, types = "numeric")
expect_equal(names(glrn2$internal_valid_scores), "classif.debug.acc")

set_validate(glrn2, NULL)
glrn2$train(tsk("iris"))
expect_true(is.null(glrn2$inner_valid_scores))
expect_true(is.null(glrn2$internal_valid_scores))

# No validation set specified --> No inner_valid_scores
# No validation set specified --> No internal_valid_scores
expect_equal(
as_learner(as_graph(lrn("classif.debug")))$train(tsk("iris"))$inner_valid_scores,
as_learner(as_graph(lrn("classif.debug")))$train(tsk("iris"))$internal_valid_scores,
NULL
)
glrn2 = as_learner(as_graph(lrn("classif.debug")))
})

test_that("inner_tuned_values", {
# no inner tuning support -> NULL
test_that("internal_tuned_values", {
# no internal tuning support -> NULL
task = tsk("iris")
glrn1 = as_learner(as_graph(lrn("classif.rpart")))$train(task)
expect_false("inner_tuning" %in% glrn1$properties)
expect_equal(glrn1$inner_tuned_values, NULL)
expect_false("internal_tuning" %in% glrn1$properties)
expect_equal(glrn1$internal_tuned_values, NULL)

# learner with inner tuning
# learner with internal tuning
glrn2 = as_learner(as_graph(lrn("classif.debug")))
expect_true("inner_tuning" %in% glrn2$properties)
expect_equal(glrn2$inner_tuned_values, NULL)
expect_true("internal_tuning" %in% glrn2$properties)
expect_equal(glrn2$internal_tuned_values, NULL)
glrn2$train(task)
expect_equal(glrn2$inner_tuned_values, named_list())
expect_equal(glrn2$internal_tuned_values, named_list())
glrn2$param_set$set_values(classif.debug.early_stopping = TRUE, classif.debug.iter = 1000)
set_validate(glrn2, 0.2)
glrn2$train(task)
expect_equal(names(glrn2$inner_tuned_values), "classif.debug.iter")
expect_equal(names(glrn2$internal_tuned_values), "classif.debug.iter")
})

test_that("disable_inner_tuning", {
test_that("disable_internal_tuning", {
glrn = as_learner(as_pipeop(lrn("classif.debug", iter = 100, early_stopping = TRUE)))
disable_inner_tuning(glrn, "classif.debug.iter")
disable_internal_tuning(glrn, "classif.debug.iter")
expect_false(glrn$graph$pipeops$classif.debug$param_set$values$early_stopping)
expect_error(disable_inner_tuning(glrn, "classif.debug.abc"), "subset of")
expect_error(disable_internal_tuning(glrn, "classif.debug.abc"), "subset of")
})

test_that("set_validate", {
glrn = as_learner(as_pipeop(lrn("classif.debug", validate = 0.3)))
set_validate(glrn, "test")
expect_equal(glrn$validate, "test")
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, "inner_valid")
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, "predefined")
set_validate(glrn, NULL)
expect_equal(glrn$validate, NULL)
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, NULL)
set_validate(glrn, 0.2, ids = "classif.debug")
expect_equal(glrn$validate, 0.2)
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, "inner_valid")
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, "predefined")


glrn = as_learner(ppl("stacking", list(lrn("classif.debug"), lrn("classif.featureless")),
lrn("classif.debug", id = "final")))
set_validate(glrn, 0.3, ids = c("classif.debug", "final"))
expect_equal(glrn$validate, 0.3)
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, "inner_valid")
expect_equal(glrn$graph$pipeops$final$learner$validate, "inner_valid")
expect_equal(glrn$graph$pipeops$classif.debug$learner$validate, "predefined")
expect_equal(glrn$graph$pipeops$final$learner$validate, "predefined")


glrn = as_learner(ppl("stacking", list(lrn("classif.debug"), lrn("classif.featureless")),
lrn("classif.debug", id = "final")))
glrn2 = as_learner(po("learner", glrn, id = "polearner"))
set_validate(glrn2, validate = 0.25, ids = "polearner", args = list(polearner = list(ids = "final")))
expect_equal(glrn2$validate, 0.25)
expect_equal(glrn2$graph$pipeops$polearner$learner$validate, "inner_valid")
expect_equal(glrn2$graph$pipeops$polearner$learner$graph$pipeops$final$learner$validate, "inner_valid")
expect_equal(glrn2$graph$pipeops$polearner$learner$validate, "predefined")
expect_equal(glrn2$graph$pipeops$polearner$learner$graph$pipeops$final$learner$validate, "predefined")
expect_equal(glrn2$graph$pipeops$polearner$learner$graph$pipeops$classif.debug$learner$validate, NULL)
})
10 changes: 5 additions & 5 deletions tests/testthat/test_pipeop_impute.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ test_that("PipeOpImpute", {
private = list(
.get_state = function(task) {
graph = self$build_graph()
inner_valid_task = task$inner_valid_task
on.exit({task$inner_valid_task = inner_valid_task})
task$inner_valid_task = NULL
internal_valid_task = task$internal_valid_task
on.exit({task$internal_valid_task = internal_valid_task})
task$internal_valid_task = NULL
graph$train(task)
list(gs = graph)
},
Expand Down Expand Up @@ -409,6 +409,6 @@ test_that("impute, test rows and affect_columns", {
task = tsk("pima")
task$divide(1:30)
outtrain = po_impute$train(list(task))[[1L]]
outpredict = po_impute$predict(list(task$inner_valid_task))[[1L]]
expect_true(isTRUE(all.equal(outtrain$inner_valid_task$data(), outpredict$data())))
outpredict = po_impute$predict(list(task$internal_valid_task))[[1L]]
expect_true(isTRUE(all.equal(outtrain$internal_valid_task$data(), outpredict$data())))
})

0 comments on commit 6d851c6

Please sign in to comment.