如何解决在R中为整个脚本使用parallel
我对整个脚本的并行计算有疑问。我的脚本导入数据,然后在训练和验证数据帧中随机分割,然后进行预处理和验证。我想用许多不同的种子来迭代同一脚本。
是否可以并行执行此操作?这些脚本不会互相干扰。
非常感谢!
seeds <- c(2343242,324256,764865,3524526,574574,75624,15436,674767,4325265,2462626,245264,647474,2465374,4253532,5787462,35636,357484,34524,74859,1352637)
for (i in 1:length(seeds))
{
set.seed(seeds[i])
seed <- seeds[i]
print(seeds[i])
print("begin import")
source(file = "import.r")
print("preprocessing")
source(file = "preProc.r")
print("normal")
source(file = "algorithms and datasets.r")
print("resampled")
source(file = "algorithms and datasets up down.r")
}
解决方法
逐字一对一解决方案:
library(future.apply)
plan(multisession)
seeds <- c(2343242,324256,764865,3524526,574574,75624,15436,674767,4325265,2462626,245264,647474,2465374 (but not,4253532,5787462,35636,357484,34524,74859,1352637)
empty <- future_lapply(seeds,function(seed) {
set.seed(seed)
print(seed)
print("begin import")
source(file = "import.r")
print("preprocessing")
source(file = "preProc.r")
print("normal")
source(file = "algorithms and datasets.r")
print("resampled")
source(file = "algorithms and datasets up down.r")
})
除非您选择的那些种子是必不可少的,否则您可能想使用统计上合理的并行RNG,如果这样做,您会自动获得它:
library(future.apply)
plan(multisession)
set.seed(42) ## Optional to fix the initial seed
n <- 20L ## Number of runs
empty <- future_lapply(1:n,function(ii) {
print(.Random.seed)
print("begin import")
source(file = "import.r")
print("preprocessing")
source(file = "preProc.r")
print("normal")
source(file = "algorithms and datasets.r")
print("resampled")
source(file = "algorithms and datasets up down.r")
},seed = TRUE)
由于我们此处不使用ii
,因此后者也可以使用未来版本base::replicate()
:
library(future.apply)
plan(multisession)
set.seed(42) ## Optional to fix the initial seed
n <- 20L ## Number of runs
empty <- future_replicate(n,{
print(.Random.seed)
print("begin import")
source(file = "import.r")
print("preprocessing")
source(file = "preProc.r")
print("normal")
source(file = "algorithms and datasets.r")
print("resampled")
source(file = "algorithms and datasets up down.r")
})
PS。我不清楚您如何区分不同运行的结果。也许您依靠seed
将这些脚本保存到其他文件中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。