为什么不numpy.random和多发挥好? [英] Why doesn't numpy.random and multiprocessing play nice?

查看:197
本文介绍了为什么不numpy.random和多发挥好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个随机游走的功能,使用 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 多处理发挥好?


解决方案

  

这是怎么回事,所以多不得到它的权利?


您需要补种在每过程,以确保伪随机流是彼此独立的。

我使用 os.urandom 生成种子。

I have a random walk function, that uses numpy.random to do the random step. The function walk, by itself, works just fine. In parallel, it works as expected in most cases, however in conjunction with multiprocessing, it fails. Why does multiprocessing 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 a sleepwalk 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 and multiprocessing 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屋!

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