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

apply postprocessors even if they don't need training #940

Merged
merged 2 commits into from
Sep 18, 2024
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
10 changes: 7 additions & 3 deletions R/grid_code_paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ tune_grid_loop_iter <- function(split,
iter_grid_model
)

if (!workflows::.should_inner_split(workflow)) {
if (!has_postprocessor(workflow)) {
elt_extract <- .catch_and_log(
extract_details(workflow, control$extract),
control,
Expand Down Expand Up @@ -535,15 +535,19 @@ tune_grid_loop_iter <- function(split,
next
}

if (workflows::.should_inner_split(workflow)) {
if (has_postprocessor(workflow)) {
# note that, since we're training a postprocessor, `iter_predictions`
# are the predictions from the potato set rather than the
# assessment set

# train the post-processor on the predictions generated from the model
# on the potato set
# todo: needs a `.catch_and_log`
workflow_with_post <- .fit_post(workflow, potato)
#
# if the postprocessor does not require training, then `potato` will
# be NULL and nothing other than the column names is learned from
# `assessment`.
workflow_with_post <- .fit_post(workflow, potato %||% assessment)

workflow_with_post <- .fit_finalize(workflow_with_post)

Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-last-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,41 @@ test_that("can use `last_fit()` with a workflow - postprocessor (requires traini

expect_equal(last_fit_preds[".pred"], wflow_preds)
})

test_that("can use `last_fit()` with a workflow - postprocessor (requires training)", {
skip_if_not_installed("tailor")

y <- seq(0, 7, .001)
dat <- data.frame(y = y, x = y + (y-3)^2)

dat

set.seed(1)
split <- rsample::initial_split(dat)

wflow <-
workflows::workflow(
y ~ x,
parsnip::linear_reg()
) %>%
workflows::add_tailor(
tailor::tailor("regression") %>% tailor::adjust_numeric_range(lower_limit = 1),
prop = 2/3,
method = class(split)
)

set.seed(1)
last_fit_res <-
last_fit(
wflow,
split
)

last_fit_preds <- collect_predictions(last_fit_res)

set.seed(1)
wflow_res <- generics::fit(wflow, rsample::analysis(split))
wflow_preds <- predict(wflow_res, rsample::assessment(split))

expect_equal(last_fit_preds[".pred"], wflow_preds)
})
48 changes: 48 additions & 0 deletions tests/testthat/test-resample.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,54 @@ test_that("can use `fit_resamples()` with a workflow - postprocessor (requires t
expect_equal(tune_wflow, wflow_res)
})

test_that("can use `fit_resamples()` with a workflow - postprocessor (no training)", {
skip_if_not_installed("tailor")

y <- seq(0, 7, .001)
dat <- data.frame(y = y, x = y + (y-3)^2)

folds <- rsample::vfold_cv(dat, v = 2)

wflow <-
workflows::workflow(
y ~ x,
parsnip::linear_reg()
) %>%
workflows::add_tailor(
tailor::tailor("regression") %>% tailor::adjust_numeric_range(lower_limit = 1)
)

set.seed(1)
tune_res <-
fit_resamples(
wflow,
folds,
control = control_resamples(save_pred = TRUE, extract = identity)
)

tune_preds <-
collect_predictions(tune_res) %>%
dplyr::filter(id == "Fold1")

tune_wflow <-
collect_extracts(tune_res) %>%
pull(.extracts) %>%
`[[`(1)

# mock `tune::tune_grid_loop_iter`'s RNG scheme
set.seed(1)
seed <- generate_seeds(TRUE, 1)[[1]]
old_kind <- RNGkind()[[1]]
assign(".Random.seed", seed, envir = globalenv())

wflow_res <- generics::fit(wflow, rsample::analysis(folds$splits[[1]]))
wflow_preds <- predict(wflow_res, rsample::assessment(folds$splits[[1]]))

tune_wflow$fit$fit$elapsed$elapsed <- wflow_res$fit$fit$elapsed$elapsed
expect_equal(tune_preds$.pred, wflow_preds$.pred)
expect_equal(tune_wflow, wflow_res)
})

# Error capture ----------------------------------------------------------------

test_that("failure in recipe is caught elegantly", {
Expand Down
Loading