为什么不numpy.random和多发挥好? [英] Why doesn't numpy.random and multiprocessing play nice?
问题描述
我有一个随机游走的功能,使用 numpy.random
做随机的一步。
功能步行
,其本身的工作就好了。与此同时,它按预期工作在大多数情况下,然而,在与多结合
,它失败。
为什么多
弄错了吗?
导入numpy的是NPDEF走(X,N = 100,盒= 0.5δ= 0.2):
执行随机游走
W = np.cumsum(X + np.random.uniform(-delta,三角洲,N))
W = np.where(ABS(重量)GT;框)[0]
返回W [0]如果len(W)其他ñN = 10#运行N次试验中,所有从x = 0开始
pwalk = np.vectorize(步行)
打印pwalk(np.zeros(N))#再次运行,使用列表COM prehension代替ufunc的
打印[步行(0),我在范围内(N)]#再次运行,使用多的地图
进口多为MP
P = mp.Pool()
打印p.map(步行,[0] * N)
结果,通常是类似...
47 16 72 15 8 4 38 52 12 41]
[7,45,25,13,16,19,12,30,23,4]
[3,3,3,3,3,3,3,14,3,14]
前两种方法明显显示随机性,而后者却没有。
这是怎么回事,让多
并没有得到它的权利?
如果您添加睡眠
所以这是一个稀里糊涂
并有显著的延迟,多
仍然得到它错了。
不过,如果你像非阵列的方法[(random.random()调用替换为
,那么它将按预期工作。 np.random.uniform
- .5)因为我在范围内(N)]
那么,为什么不 numpy.random
和多处理
发挥好?
这是怎么回事,所以多不得到它的权利?
块引用>您需要补种在每过程,以确保伪随机流是彼此独立的。
I have a random walk function, that uses
numpy.random
to do the random step. The functionwalk
, by itself, works just fine. In parallel, it works as expected in most cases, however in conjunction withmultiprocessing
, it fails. Why doesmultiprocessing
get it wrong?import numpy as np def walk(x, n=100, box=.5, delta=.2): "perform a random walk" w = np.cumsum(x + np.random.uniform(-delta,delta,n)) w = np.where(abs(w) > box)[0] return w[0] if len(w) else n N = 10 # run N trials, all starting from x=0 pwalk = np.vectorize(walk) print pwalk(np.zeros(N)) # run again, using list comprehension instead of ufunc print [walk(0) for i in range(N)] # run again, using multiprocessing's map import multiprocessing as mp p = mp.Pool() print p.map(walk, [0]*N)
The results, are typically something like...
[47 16 72 8 15 4 38 52 12 41] [7, 45, 25, 13, 16, 19, 12, 30, 23, 4] [3, 3, 3, 3, 3, 3, 3, 14, 3, 14]
The first two methods obviously show randomness, while the latter doesn't. What's going on, so that
multiprocessing
doesn't get it right?If you add a
sleep
so it's asleepwalk
and there's significant delay,multiprocessing
still gets it wrong.However, if you replace the call to
np.random.uniform
with a non-array method like[(random.random()-.5) for i in range(n)]
, then it works as expected.So why doesn't
numpy.random
andmultiprocessing
play nice?解决方案What's going on, so that multiprocessing doesn't get it right?
You need to reseed in each process to make sure the psuedo-random streams are independent of one another.
I use os.urandom to generate the seeds.
这篇关于为什么不numpy.random和多发挥好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!