如何使用foreach和doMC软件包为随机模拟设置种子? [英] How to set seed for random simulations with foreach and doMC packages?

查看:217
本文介绍了如何使用foreach和doMC软件包为随机模拟设置种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行一些模拟,并且出于调试目的,我想使用set.seed获得相同的结果.这是我要尝试的示例:

I need to do some simulations and for debugging purposes I want to use set.seed to get the same result. Here is the example of what I am trying to do:

library(foreach)
library(doMC)
registerDoMC(2)

set.seed(123)
a <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
set.seed(123)
b <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}

对象ab应该相同,即sum(abs(a-b))应该为零,但事实并非如此.我做错了什么,还是偶然发现了某些功能?

Objects a and b should be identical, i.e. sum(abs(a-b)) should be zero, but this is not the case. I am doing something wrong, or have I stumbled on to some feature?

我能够在R 2.13和R 2.14的两个不同系统上重现这一点

I am able to reproduce this on two different systems with R 2.13 and R 2.14

推荐答案

我的默认答案曾经是好吧,那就别那样做"(使用 snow 包为您做到(可靠!)

My default answer used to be "well then don't do that" (using foreach) as the snow package does this (reliably!) for you.

但是正如@Spacedman指出的那样,如果您想保留Renaud的新 doRNG doFoo/ foreach 系列.

But as @Spacedman points out, Renaud's new doRNG is what you are looking for if you want to remain with the doFoo / foreach family.

尽管,真正的钥匙是clusterApply样式的调用,以在所有节点上设置种子.并且以跨流协调的方式.哦,我是否提到Tierney,Rossini,Li和Sevcikova的几乎已经为您完成了十年?

The real key though is a clusterApply-style call to get the seeds set on all nodes. And in a fashion that coordinated across streams. Oh, and did I mention that snow by Tierney, Rossini, Li and Sevcikova has been doing this for you for almost a decade?

尽管您没有询问,但出于完整性考虑这是命令行示例:

And while you didn't ask about snow, for completeness here is an example from the command-line:

edd@max:~$ r -lsnow -e'cl <- makeSOCKcluster(c("localhost","localhost"));\
         clusterSetupRNG(cl);\
         print(do.call("rbind", clusterApply(cl, 1:4, \
                                             function(x) { stats::rnorm(1) } )))'
Loading required package: utils
Loading required package: utils
Loading required package: rlecuyer
           [,1]
[1,] -1.1406340
[2,]  0.7049582
[3,] -0.4981589
[4,]  0.4821092
edd@max:~$ r -lsnow -e'cl <- makeSOCKcluster(c("localhost","localhost"));\
         clusterSetupRNG(cl);\
         print(do.call("rbind", clusterApply(cl, 1:4, \
                                             function(x) { stats::rnorm(1) } )))'
Loading required package: utils
Loading required package: utils
Loading required package: rlecuyer
           [,1]
[1,] -1.1406340
[2,]  0.7049582
[3,] -0.4981589
[4,]  0.4821092
edd@max:~$ 

为完整起见,以下示例与的文档中的内容结合在一起doRNG

And for completeness, here is your example combined with what is in the docs for doRNG

> library(foreach)
R> library(doMC)
Loading required package: multicore

Attaching package: ‘multicore’

The following object(s) are masked from ‘package:parallel’:

    mclapply, mcparallel, pvec

R> registerDoMC(2)
R> library(doRNG)
R> set.seed(123)
R> a <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
R> set.seed(123)
R> b <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
R> identical(a,b)
[1] FALSE                     ## ie standard approach not reproducible
R>
R> seed <- doRNGseed()
R> a <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
R> b <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
R> doRNGseed(seed)
R> a1 <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
R> b1 <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
R> identical(a,a1) && identical(b,b1)
[1] TRUE                      ## all is well now with doRNGseed()
R> 

这篇关于如何使用foreach和doMC软件包为随机模拟设置种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆