How to check if a recipe requires tuning/finalization?

Hi,

Is there a straightforward way to check if a recipe has any parameters set to tune()? The best I can find right now is to try to prep() the recipe and check the error message . . . I checked a bit into the error message that generates and I see that there is, somewhere in some package and apparently not exported an is_tune function, perhaps I can copy that to my environment if someone knows where it is?

This is working for me but feels . . . like the wrong way to handle the situation. What if prep involves a lot of computation (e.g.)?

check_if_needs_tune<-function(recipe_to_check){
  tryCatch({prep(recipe_to_check)
    FALSE},error=function(e) {
      if(grepl("Argument(s) with `tune()`:",e$message,fixed=T))
      return(TRUE) })
}

Thanks for the time,
TL

There is an api called tune_args() that works for different kinds of objects (e.g. recipes, models, workflows.

It returns a tibble with any arguments marked for tuning (even engine arguments):

library(tidymodels)

methods("tune_args")
#> [1] tune_args.check*      tune_args.model_spec* tune_args.recipe*    
#> [4] tune_args.step*       tune_args.workflow*  
#> see '?methods' for accessing help and source code

###

rec_clean <- 
  recipe(mpg ~ ., data = mtcars) %>% 
  step_mutate(inv_disp = 1/disp)

rec_tune <- 
  rec_clean %>% 
  step_spline_natural(wt, deg_free = tune())

tune_args(rec_clean)
#> # A tibble: 0 × 6
#> # ℹ 6 variables: name <chr>, tunable <lgl>, id <chr>, source <chr>,
#> #   component <chr>, component_id <chr>
tune_args(rec_tune)
#> # A tibble: 1 × 6
#>   name     tunable id       source component           component_id        
#>   <chr>    <lgl>   <chr>    <chr>  <chr>               <chr>               
#> 1 deg_free TRUE    deg_free recipe step_spline_natural spline_natural_yGv0B

### 

mlp() %>% 
  set_engine("brulee", stop_iter = tune()) %>% 
  tune_args()
#> # A tibble: 1 × 6
#>   name      tunable id        source     component component_id
#>   <chr>     <lgl>   <chr>     <chr>      <chr>     <chr>       
#> 1 stop_iter TRUE    stop_iter model_spec mlp       <NA>

Created on 2023-10-10 with reprex v2.0.2

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.