- 
                Notifications
    You must be signed in to change notification settings 
- Fork 45
enable tuning postprocessors #966
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
          
     Draft
      
      
            simonpcouch
  wants to merge
  2
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
tune-postprocessors
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
|  | ||
| predict_model <- function(new_data, orig_rows, workflow, grid, metrics, | ||
| submodels = NULL, metrics_info, eval_time = NULL) { | ||
|  | ||
| model <- extract_fit_parsnip(workflow) | ||
|  | ||
| forged <- forge_from_workflow(new_data, workflow) | ||
|  | @@ -260,6 +259,22 @@ finalize_workflow_preprocessor <- function(workflow, grid_preprocessor) { | |
| workflow | ||
| } | ||
|  | ||
| #' @export | ||
| #' @rdname tune-internal-functions | ||
| finalize_workflow_postprocessor <- function(workflow, grid_postprocessor) { | ||
| # Already finalized, nothing to tune | ||
| if (ncol(grid_postprocessor) == 0L) { | ||
| return(workflow) | ||
| } | ||
|  | ||
| postprocessor <- workflows::extract_postprocessor(workflow) | ||
| postprocessor <- merge(postprocessor, grid_postprocessor)$x[[1]] | ||
|  | ||
| workflow <- set_workflow_tailor(workflow, postprocessor) | ||
|  | ||
| workflow | ||
| } | ||
|  | ||
| # ------------------------------------------------------------------------------ | ||
|  | ||
| # For any type of tuning, and for fit-resamples, we generate a unified | ||
|  | @@ -310,16 +325,23 @@ compute_grid_info <- function(workflow, grid) { | |
| grid <- tibble::as_tibble(grid) | ||
|  | ||
| parameters <- hardhat::extract_parameter_set_dials(workflow) | ||
| parameters_model <- dplyr::filter(parameters, source == "model_spec") | ||
|  | ||
| parameters_preprocessor <- dplyr::filter(parameters, source == "recipe") | ||
| parameters_model <- dplyr::filter(parameters, source == "model_spec") | ||
| parameters_postprocessor <- dplyr::filter(parameters, source == "tailor") | ||
|  | ||
| any_parameters_model <- nrow(parameters_model) > 0 | ||
| any_parameters_preprocessor <- nrow(parameters_preprocessor) > 0 | ||
|  | ||
| res <- min_grid(extract_spec_parsnip(workflow), grid) | ||
| any_parameters_model <- nrow(parameters_model) > 0 | ||
| any_parameters_postprocessor <- nrow(parameters_postprocessor) > 0 | ||
|  | ||
| syms_pre <- rlang::syms(parameters_preprocessor$id) | ||
| syms_mod <- rlang::syms(parameters_model$id) | ||
| syms_post <- rlang::syms(parameters_postprocessor$id) | ||
|  | ||
| res <- min_grid(extract_spec_parsnip(workflow), grid) | ||
| if (any_parameters_postprocessor) { | ||
| res <- nest_min_grid(res, parameters_postprocessor$id) | ||
| } | ||
|  | ||
| # ---------------------------------------------------------------------------- | ||
| # Create an order of execution to train the preprocessor (if any). This will | ||
|  | @@ -340,7 +362,7 @@ compute_grid_info <- function(workflow, grid) { | |
| res$.lab_pre <- "Preprocessor1" | ||
| } | ||
|  | ||
| # Make the label shown in the grid and in loggining | ||
| # Make the label shown in the grid and in logging | ||
| res$.msg_preprocessor <- | ||
| new_msgs_preprocessor( | ||
| res$.iter_preprocessor, | ||
|  | @@ -351,7 +373,6 @@ compute_grid_info <- function(workflow, grid) { | |
| # Now make a similar iterator across models. Conditioning on each unique | ||
| # preprocessing candidate set, make an iterator for the model candidate sets | ||
| # (if any) | ||
|  | ||
| res <- | ||
| res %>% | ||
| dplyr::group_nest(.iter_preprocessor, keep = TRUE) %>% | ||
|  | @@ -370,21 +391,74 @@ compute_grid_info <- function(workflow, grid) { | |
| n = res$.num_models, | ||
| res$.msg_preprocessor) | ||
|  | ||
| res %>% | ||
| res <- res %>% | ||
| dplyr::select(-.num_models) %>% | ||
| dplyr::relocate(dplyr::starts_with(".msg")) | ||
|  | ||
| # ---------------------------------------------------------------------------- | ||
| # Finally, iterate across postprocessors. Conditioning on an .iter_config, | ||
| # make an iterator for each postprocessing candidate set (if any). | ||
| if (!any_parameters_postprocessor) { | ||
| return(res) | ||
| } | ||
|  | ||
| res <- | ||
| res %>% | ||
| dplyr::group_nest(.iter_config, keep = TRUE) %>% | ||
| dplyr::mutate( | ||
| data = purrr::map(data, make_iter_postprocessor, parameters_postprocessor$id) | ||
| ) %>% | ||
| tidyr::unnest(cols = data) %>% | ||
| dplyr::relocate(dplyr::starts_with(".iter"), dplyr::starts_with(".msg")) | ||
|  | ||
| res | ||
| } | ||
|  | ||
| make_iter_config <- function(dat) { | ||
| # Compute labels for the models *within* each preprocessing loop. | ||
| num_submodels <- purrr::map_int(dat$.submodels, ~ length(unlist(.x))) | ||
| num_submodels <- purrr::map_int( | ||
| dat$.submodels, | ||
| function(.x) {if (length(.x) == 0) 0 else length(.x[[1]])} | ||
| ) | ||
| num_models <- sum(num_submodels + 1) # +1 for the model being trained | ||
| .mod_label <- recipes::names0(num_models, "Model") | ||
| .iter_config <- paste(dat$.lab_pre[1], .mod_label, sep = "_") | ||
| .iter_config <- vctrs::vec_chop(.iter_config, sizes = num_submodels + 1) | ||
| tibble::tibble(.iter_config = .iter_config) | ||
| } | ||
|  | ||
| make_iter_postprocessor <- function(data, post_params) { | ||
| nested_by_post <- "post" %in% names(data) | ||
| if (nested_by_post) { | ||
| data <- data %>% unnest(post) | ||
| } | ||
|  | ||
| data %>% | ||
| mutate( | ||
| .iter_postprocessor = seq_len(nrow(.)), | ||
| .msg_postprocessor = new_msgs_postprocessor( | ||
| i = .iter_postprocessor, | ||
| n = max(.iter_postprocessor), | ||
| msgs_model = .msg_model | ||
| ), | ||
| .iter_config_post = purrr::map2( | ||
| .iter_config, | ||
| .iter_postprocessor, | ||
| make_iter_config_post | ||
| ) | ||
| ) %>% | ||
| select(-.iter_config) %>% | ||
| nest(post = c(any_of(post_params), ".iter_postprocessor", ".msg_postprocessor", ".iter_config_post")) | ||
| } | ||
|  | ||
| make_iter_config_post <- function(iter_config, iter_postprocessor) { | ||
| paste0( | ||
| iter_config, | ||
| "_Postprocessor", | ||
| iter_postprocessor | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still needs a  | ||
| ) | ||
| } | ||
|  | ||
| # This generates a "dummy" grid_info object that has the same | ||
| # structure as a grid-info object with no tunable recipe parameters | ||
| # and no tunable model parameters. | ||
|  | @@ -420,6 +494,9 @@ new_msgs_preprocessor <- function(i, n) { | |
| new_msgs_model <- function(i, n, msgs_preprocessor) { | ||
| paste0(msgs_preprocessor, ", model ", i, "/", n) | ||
| } | ||
| new_msgs_postprocessor <- function(i, n, msgs_model) { | ||
| paste0(msgs_model, ", postprocessor ", i, "/", n) | ||
| } | ||
|  | ||
| # c(1, 10) -> c("01", "10") | ||
| format_with_padding <- function(x) { | ||
|  | @@ -467,3 +544,8 @@ set_workflow_recipe <- function(workflow, recipe) { | |
| workflow$pre$actions$recipe$recipe <- recipe | ||
| workflow | ||
| } | ||
|  | ||
| set_workflow_tailor <- function(workflow, tailor) { | ||
| workflow$post$actions$tailor$tailor <- tailor | ||
| workflow | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually want postprocessor parameters names both inside and outside of
.submodels, but this results in them only being inside of.submodels.