如何有效地扫描许多参数以在 R 中运行脚本?



我在R中有一个脚本,它有4个可以改变的参数。我想使用这些参数的几个组合运行扫描,并对这些运行进行计时以在之后进行比较。像这样:

dim_map = c(10,40,80,120)
epochs = c(200,600,1000)
dim_input = c(3,80,400,1000,3000)
datapoints = c(15000,50000,100000)
results = data.frame(dim_map = c(),
epochs = c(),
dim_input = c(),
datapoints = c(),
time = c()
)
for(dim in dim_map){
for (epoch in epochs){
for (m in dim_input){
for (n in datapoints){
t = Sys.time() # Start time
## Run some script
elapsed_time = as.numeric(Sys.time() - t, units = 'secs')
results[nrow(results)+1,] = c(dim, epoch, m, n, elapsed_time)
}
}
}
}

有没有办法避免循环?我觉得这些嵌套循环正在减慢扫描速度,但我不知道这是否只是我的想象。或者也许是使用这些参数变体对脚本进行计时的更好方法?

我认为做这种事情的最简单方法之一是将pmapcross_df结合起来purrr.我们可以轻松创建参数的所有组合,然后为每个参数运行代码,将结果存储在新列中:

library(tidyverse)
params <-  cross_df(list(
dim_map = c(10,40,80,120),
epochs = c(200,600,1000),
dim_input = c(3,80,400,1000,3000),
datapoints = c(15000,50000,100000)
))
timer <- function(dim_map, epochs, dim_input, datapoints){
start_time = Sys.time()
Sys.sleep(0.01) # your code to time here
end_time = Sys.time()
return(end_time - start_time)
}
params %>%
mutate(time = pmap_dbl(., timer))
#> # A tibble: 180 x 5
#>    dim_map epochs dim_input datapoints   time
#>      <dbl>  <dbl>     <dbl>      <dbl>  <dbl>
#>  1      10    200         3      15000 0.0110
#>  2      40    200         3      15000 0.0110
#>  3      80    200         3      15000 0.0110
#>  4     120    200         3      15000 0.0110
#>  5      10    600         3      15000 0.0110
#>  6      40    600         3      15000 0.0110
#>  7      80    600         3      15000 0.0110
#>  8     120    600         3      15000 0.0109
#>  9      10   1000         3      15000 0.0110
#> 10      40   1000         3      15000 0.0110
#> # ... with 170 more rows

创建于 2018-09-21 由 reprex 包 (v0.2.0(.

最新更新