Python 中 numpy.random 和 random.random 之间的性能差异 [英] Performance difference between numpy.random and random.random in Python
问题描述
我想看看我的神经网络中哪个随机数生成器包更快.
I want to see what random number generator package is faster in my neural network.
我目前正在更改 github 中的代码,其中 numpy.random 和 random 包都用于生成随机整数、随机选择、随机样本等.
I am currently changing a code from github, in which both numpy.random and random packages are used to generate random integers, random choices, random samples etc.
我更改此代码的原因是出于研究目的,我想设置一个全局种子,以便能够比较不同超参数设置的准确度性能.问题是此时我必须为 random 包和 numpy 包设置 2 个全局种子.理想情况下,我只想设置一个种子,因为来自两个随机数生成器序列的绘图可能会更快地相关联.
The reason that I am changing this code is that for research purposes I would like to set a global seed to be able to compare accuracy performance for different settings of hyperparameters. The problem is that at this moment I have to set 2 global seeds, both for the random package and for the numpy package. Ideally, I would like to set only one seed as drawings from two sequences of random number generators might become correlated more quickly.
但是,我不知道哪个包会表现得更好(在速度方面):numpy 或 random.因此,我想找到与完全相同的 Mersenne Twister 序列对应的两个包的种子.这样,两个模型的绘图是相同的,因此每个梯度下降步骤的迭代次数也相同,导致速度差异仅由我使用的包引起.
However, I do not know what package will perform better (in terms of speed): numpy or random. So I would like to find seeds for both packages that correspond to exactly the same Mersenne Twister sequence. In that way, the drawings for both models are the same and therefore also the number of iterations in each gradient descent step are the same, leading to a difference in speed only caused by the package I use.
我找不到任何关于以相同随机数序列结束两个包的种子对的任何文档,而且尝试各种组合似乎有点麻烦.
I could not find any documentation on pairs of seeds that end up in the same random number sequence for both packages and also trying out all kind of combinations seems a bit cumbersome.
我尝试了以下方法:
np.random.seed(1)
numpy_1=np.random.randint(0,101)
numpy_2=np.random.randint(0,101)
numpy_3=np.random.randint(0,101)
numpy_4=np.random.randint(0,101)
for i in range(20000000):
random.seed(i)
random_1=random.randint(0,101)
if random_1==numpy_1:
random_2=random.randint(0,101)
if random_2==numpy_2:
random_3=random.randint(0,101)
if random_3==numpy_3:
random_4=random.randint(0,101)
if random_4==numpy_4:
break
print(np.random.randint(0,101))
print(random.randint(0,101))
但这并没有像预期的那样真正奏效.
But this did not really work, as could be expected.
推荐答案
numpy.random
和 python random
以不同的方式工作,尽管正如你所说,他们使用相同的算法.
numpy.random
and python random
work in different ways, although, as you say, they use the same algorithm.
就种子而言:您可以使用 numpy.random
中的 set_state
和 get_state
函数(在python random
调用了 getstate
和 setstate
) 并将状态从一个传递到另一个.结构略有不同(在 python 中 pos
整数附加到状态元组中的最后一个元素).请参阅numpy.random.get_state()的文档a> 和 random.getstate():
In terms of seed: You can use the set_state
and get_state
functions from numpy.random
(in python random
called getstate
and setstate
) and pass the state from one to another. The structure is slightly different (in python the pos
integer is attached to the last element in the state tuple). See the docs for numpy.random.get_state() and random.getstate():
import random
import numpy as np
random.seed(10)
s1 = list(np.random.get_state())
s2 = list(random.getstate())
s1[1] = np.array(s2[1][:-1]).astype('int32')
s1[2] = s2[1][-1]
np.random.set_state(tuple(s1))
print(np.random.random())
print(random.random())
>> 0.5714025946899135
0.5714025946899135
在效率方面:这取决于你想做什么,但 numpy 通常更好,因为你可以在不需要循环的情况下创建元素数组:
In terms of efficiency: it depends on what you want to do, but numpy is usually better because you can create arrays of elements without the need of a loop:
%timeit np.random.random(10000)
142 µs ± 391 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit [random.random() for i in range(10000)]
1.48 ms ± 2.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
就随机性"而言,numpy是(根据他们的docs),也更好:
In terms of "randomness", numpy is (according to their docs), also better:
注意:Python stdlib 模块random"还包含一个 Mersenne Twister 伪随机数生成器,其中包含许多方法与 RandomState
中可用的类似.RandomState
,除了支持 NumPy 之外,还有一个优点是它提供了很多有更多的概率分布可供选择.
Notes: The Python stdlib module "random" also contains a Mersenne Twister pseudo-random number generator with a number of methods that are similar to the ones available in
RandomState
.RandomState
, besides being NumPy-aware, has the advantage that it provides a much larger number of probability distributions to choose from.
这篇关于Python 中 numpy.random 和 random.random 之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!