当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R dials grid_regular 创建调整参数网格


可以为任意数量的参数对象创建随机和规则网格。

用法

grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for parameters
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for list
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for param
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for workflow
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for parameters
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for list
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for param
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for workflow
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

参数

x

param 对象、列表或 parameters

...

一个或多个 param 对象(例如 mtry()penalty() )。所有对象都不能在参数范围或值中具有 unknown() 值。

levels

用于制作规则网格的每个参数值的数量的整数。 levels 可以是单个整数或整数向量,其长度与 ... 中的参数数量相同。 levels 可以是命名整数向量,其名称与参数的 id 值匹配。

original

逻辑:参数应该采用原始单位还是变换后的空间(如果有)?

filter

逻辑:是否应该在生成网格之前过滤参数。必须是引用参数名称且计算结果为逻辑向量的单个表达式。

size

为随机网格返回的参数值组合总数的单个整数。如果从此大小生成重复组合,则返回较小的唯一集合。

一点点。每个参数都有列,每个参数组合都有一行。

细节

请注意,根据函数的调用方式,网格可能会有所不同。如果调用直接使用参数对象,则可能的范围来自 dials 中的对象。例如:

mixture()
## Proportion of Lasso Penalty (quantitative)
## Range: [0, 1]
set.seed(283)
mix_grid_1 <- grid_random(mixture(), size = 1000)
range(mix_grid_1$mixture)
## [1] 0.001490161 0.999741096

但是,在某些情况下,parsniprecipe 包会覆盖特定模型和预处理步骤的默认范围。如果网格函数使用从模型或配方创建的 parameters 对象,则范围可能具有不同的默认值(特定于这些模型)。使用上面的示例,上面的 mixture 参数对于 glmnet 模型是不同的:

library(parsnip)
library(tune)

# When used with glmnet, the range is [0.05, 1.00]
glmn_mod <-
  linear_reg(mixture = tune()) %>%
  set_engine("glmnet")

set.seed(283)
mix_grid_2 <- grid_random(extract_parameter_set_dials(glmn_mod), size = 1000)
range(mix_grid_2$mixture)
## [1] 0.05141565 0.99975404

例子

# filter arg will allow you to filter subsequent grid data frame based on some condition.
p <- parameters(penalty(), mixture())
grid_regular(p)
#> # A tibble: 9 × 2
#>        penalty mixture
#>          <dbl>   <dbl>
#> 1 0.0000000001     0  
#> 2 0.00001          0  
#> 3 1                0  
#> 4 0.0000000001     0.5
#> 5 0.00001          0.5
#> 6 1                0.5
#> 7 0.0000000001     1  
#> 8 0.00001          1  
#> 9 1                1  
grid_regular(p, filter = penalty <= .01)
#> # A tibble: 6 × 2
#>        penalty mixture
#>          <dbl>   <dbl>
#> 1 0.0000000001     0  
#> 2 0.00001          0  
#> 3 0.0000000001     0.5
#> 4 0.00001          0.5
#> 5 0.0000000001     1  
#> 6 0.00001          1  

# Will fail due to unknowns:
# grid_regular(mtry(), min_n())

grid_regular(penalty(), mixture())
#> # A tibble: 9 × 2
#>        penalty mixture
#>          <dbl>   <dbl>
#> 1 0.0000000001     0  
#> 2 0.00001          0  
#> 3 1                0  
#> 4 0.0000000001     0.5
#> 5 0.00001          0.5
#> 6 1                0.5
#> 7 0.0000000001     1  
#> 8 0.00001          1  
#> 9 1                1  
grid_regular(penalty(), mixture(), levels = 3:4)
#> # A tibble: 12 × 2
#>         penalty mixture
#>           <dbl>   <dbl>
#>  1 0.0000000001   0    
#>  2 0.00001        0    
#>  3 1              0    
#>  4 0.0000000001   0.333
#>  5 0.00001        0.333
#>  6 1              0.333
#>  7 0.0000000001   0.667
#>  8 0.00001        0.667
#>  9 1              0.667
#> 10 0.0000000001   1    
#> 11 0.00001        1    
#> 12 1              1    
grid_regular(penalty(), mixture(), levels = c(mixture = 4, penalty = 3))
#> # A tibble: 12 × 2
#>         penalty mixture
#>           <dbl>   <dbl>
#>  1 0.0000000001   0    
#>  2 0.00001        0    
#>  3 1              0    
#>  4 0.0000000001   0.333
#>  5 0.00001        0.333
#>  6 1              0.333
#>  7 0.0000000001   0.667
#>  8 0.00001        0.667
#>  9 1              0.667
#> 10 0.0000000001   1    
#> 11 0.00001        1    
#> 12 1              1    
grid_random(penalty(), mixture())
#> # A tibble: 5 × 2
#>         penalty mixture
#>           <dbl>   <dbl>
#> 1 0.00000000433   0.312
#> 2 0.0000000435    0.174
#> 3 0.00359         0.423
#> 4 0.00000299      0.192
#> 5 0.00000146      0.633

源代码:R/grids.R

相关用法


注:本文由纯净天空筛选整理自Max Kuhn等大神的英文原创作品 Create grids of tuning parameters。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。