numpy.random.seed(0)有什么作用?

2020/10/28 11:53 · python/未分类 ·  · 0评论

np.random.seedScikit-Learn教程的以下代码在做什么我对NumPy的随机状态生成器不太熟悉,所以我非常感谢外行对此的解释。

np.random.seed(0)
indices = np.random.permutation(len(iris_X))

np.random.seed(0) 使随机数可预测

>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])

重置种子(每次)后,每次都会出现一组相同的数字。

如果未重置随机种子,则每次调用都会显示不同的数字:

>>> numpy.random.rand(4)
array([ 0.42,  0.65,  0.44,  0.89])
>>> numpy.random.rand(4)
array([ 0.96,  0.38,  0.79,  0.53])

(伪)随机数的工作方式是从一个数字(种子)开始,将其乘以一个大数,再加上一个偏移量,然后对该和取模。然后将所得的数字用作种子,以生成下一个“随机”数字。设置种子时(每次),每次都会执行相同的操作,并为您提供相同的编号。

如果您希望看似随机数,请不要设置种子。但是,如果您的代码使用要调试的随机数,则在每次运行之前设置种子可能非常有帮助,这样每次运行代码时,它都会执行相同的操作。

要获得每次运行的最大随机数,请致电numpy.random.seed()将导致numpy将种子设置为从/dev/urandomWindows或其Windows模拟获得的随机数,或者,如果两者均不可用,它将使用时钟。

有关使用种子生成伪随机数的更多信息,请参见Wikipedia

如果您设置np.random.seed(a_fixed_number)每次调用numpy的其他随机函数,则结果将相同:

>>> import numpy as np
>>> np.random.seed(0) 
>>> perm = np.random.permutation(10) 
>>> print perm 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.rand(4) 
[0.5488135  0.71518937 0.60276338 0.54488318]
>>> np.random.seed(0) 
>>> print np.random.rand(4) 
[0.5488135  0.71518937 0.60276338 0.54488318]

但是,如果只调用一次并使用各种随机函数,结果将仍然不同:

>>> import numpy as np
>>> np.random.seed(0) 
>>> perm = np.random.permutation(10)
>>> print perm 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> print np.random.permutation(10) 
[3 5 1 2 9 8 0 6 7 4]
>>> print np.random.permutation(10) 
[2 3 8 4 5 1 0 6 9 7]
>>> print np.random.rand(4) 
[0.64817187 0.36824154 0.95715516 0.14035078]
>>> print np.random.rand(4) 
[0.87008726 0.47360805 0.80091075 0.52047748]

如前所述,numpy.random.seed(0)将随机种子设置为0,因此从random获得的伪随机数将从同一点开始。在某些情况下,这对于调试非常有用。但是,经过一番阅读后,如果您有线程,这似乎是错误的处理方法,因为它不是线程安全的。

来自python中的numpy随机和随机随机数之间的差异

对于numpy.random.seed(),主要的困难是它不是线程安全的-也就是说,如果您有许多不同的执行线程,则使用它是不安全的,因为如果两个不同的线程正在执行,则不能保证它可以正常工作。同时功能。如果您不使用线程,并且可以合理地期望将来不需要以这种方式重写程序,则numpy.random.seed()应该适合测试。如果有任何理由怀疑您将来可能需要线程,那么从长远来看,按照建议进行操作并创建numpy.random.Random类的本地实例要安全得多。据我所知,random.random.seed()是线程安全的(或者至少我没有发现任何相反的证据)。

如何执行此操作的示例:

from numpy.random import RandomState
prng = RandomState()
print prng.permutation(10)
prng = RandomState()
print prng.permutation(10)
prng = RandomState(42)
print prng.permutation(10)
prng = RandomState(42)
print prng.permutation(10)

可以给:

[3 0 4 6 8 2 1 9 7 5]

[1 6 9 0 2 7 8 3 5 4]

[8 1 5 0 7 2 9 4 3 6]

[8 1 5 0 7 2 9 4 3 6]

最后,请注意,由于xor的工作方式,可能在某些情况下初始化为0(相对于并非所有位均为0的种子)可能会导致一些首次迭代的分布不均匀,但这取决于算法,这超出了我目前的担心和这个问题的范围。

我在神经网络中经常使用它。众所周知,当我们开始训练神经网络时,我们会随机初始化权重。在特定数据集上对这些权重训练模型。经过数个时期后,您将获得一组训练有素的权重。

Now suppose you want to again train from scratch or you want to pass the model to others to reproduce your results, the weights will be again initialised to a random numbers which mostly will be different from earlier ones. The obtained trained weights after same number of epochs ( keeping same data and other parameters ) as earlier one will differ. The problem is your model is no more reproducible that is every time you train your model from scratch it provides you different sets of weights. This is because the model is being initialized by different random numbers every time.

如果每次您从头开始训练时都将模型初始化为同一组随机初始化权重,该怎么办?在这种情况下,您的模型可以重现。这是通过numpy.random.seed(0)实现的。通过将seed()提到特定的数字,您将始终挂在同一组随机数上。

想象一下,您正在向某人展示如何使用一堆“随机”数字进行编码。通过使用numpy种子,他们可以使用相同的种子编号并获得相同的“随机”编号集。

因此,它并不是完全随机的,因为算法会散出数字,但看起来像是随机生成的一堆。

All the answers above show the implementation of np.random.seed() in code. I'll try my best to explain briefly why it actually happens. Computers are machines that are designed based on predefined algorithms. Any output from a computer is the result of the algorithm implemented on the input. So when we request a computer to generate random numbers, sure they are random but the computer did not just come up with them randomly!

So when we write np.random.seed(any_number_here) the algorithm will output a particular set of numbers that is unique to the argument any_number_here. It's almost like a particular set of random numbers can be obtained if we pass the correct argument. But this will require us to know about how the algorithm works which is quite tedious.

So, for example if I write np.random.seed(10) the particular set of numbers that I obtain will remain the same even if I execute the same line after 10 years unless the algorithm changes.

A random seed specifies the start point when a computer generates a random number sequence.

For example, let’s say you wanted to generate a random number in Excel (Note: Excel sets a limit of 9999 for the seed). If you enter a number into the Random Seed box during the process, you’ll be able to use the same set of random numbers again. If you typed “77” into the box, and typed “77” the next time you run the random number generator, Excel will display that same set of random numbers. If you type “99”, you’ll get an entirely different set of numbers. But if you revert back to a seed of 77, then you’ll get the same set of random numbers you started with.

For example, “take a number x, add 900 +x, then subtract 52.” In order for the process to start, you have to specify a starting number, x (the seed). Let’s take the starting number 77:

Add 900 + 77 = 977
Subtract 52 = 925
Following the same algorithm, the second “random” number would be:

900 + 925 = 1825
Subtract 52 = 1773
This simple example follows a pattern, but the algorithms behind computer number generation are much more complicated

All the random numbers generated after setting particular seed value are same across all the platforms/systems.

There is a nice explanation in Numpy docs:
https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.RandomState.html
it refers to Mersenne Twister pseudo-random number generator. More details on the algorithm here: https://en.wikipedia.org/wiki/Mersenne_Twister

numpy.random.seed(0)
numpy.random.randint(10, size=5)

This produces the following output:
array([5, 0, 3, 3, 7])
Again,if we run the same code we will get the same result.

Now if we change the seed value 0 to 1 or others:

numpy.random.seed(1)
numpy.random.randint(10, size=5)

This produces the following output: array([5 8 9 5 0]) but now the output not the same like above.

I hope to give a really short answer:

seed make (the next series) random numbers predictable. You can think every time after you call seed, it pre-defines series numbers and numpy random keeps the iterator of it, then every time you get a random number it just gonna call get next.

e.g.:

np.random.seed(2)
np.random.randn(2) # array([-0.41675785, -0.05626683])
np.random.randn(1) # array([-1.24528809])

np.random.seed(2)
np.random.randn(1) # array([-0.41675785])
np.random.randn(2) # array([-0.05626683, -1.24528809])

You can notice when I set the same seed, no matter how many random number you request from numpy each time, it always gives the same series of numbers, in this case which is array([-0.41675785, -0.05626683, -1.24528809]).

本文地址:http://python.askforanswer.com/numpy-random-seed0youshenmezuoyong.html
文章标签: ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!