简体   繁体   English

什么时候使用numpy.random.randn(…)?什么时候使用numpy.random.rand(…)?

[英]When to use numpy.random.randn(…) and when numpy.random.rand(…)?

In my deep learning exercise i had to initialize one parameter D1 of same size as A1 so what i did is: 在我的深度学习练习中,我必须初始化一个与A1大小相同的参数D1,所以我要做的是:

D1 = np.random.randn(A1.shape[0],A1.shape[1]) 

But after computing further equations when i checked the results they didn't matched then after proper reading the doc i discovered that they have said to initialize D1 using rand instead of randn ; 但是在计算了进一步的方程后,当我检查结果不匹配时,在正确阅读文档后,我发现他们说用rand而不是randn初始化D1;

D1 = np.random.rand(A1.shape[0],A1.shape[1]) 

But they didn't specified the reason for it as the code is working in both the cases and also there was a doc for that exercise so I figured out the error, but how , when and why to choose out of these two? 但是他们没有指定原因,因为代码在两种情况下都有效,并且有一个练习的文档,所以我找出了错误,但是如何何时以及为什么从这两种情况中进行选择?

The difference between rand and randn is (besides the letter n ) that rand returns random numbers sampled from a uniform distribution over the interval [0,1), while randn instead samples from a normal (aka Gaussian) distribution with a mean of 0 and a variance of 1. randrandn之间的区别是(字母n除外), rand返回在间隔[0,1)上从均匀分布中采样的随机数,而randn则从平均值为0的正态(aka高斯)分布中采样,方差为1。

In other words, the distribution of the random numbers produced by rand looks like this: 换句话说, rand产生的随机数的分布如下所示:

均匀分布

In a uniform distribution, all the random values are restricted to a specific interval, and are evenly distributed over that interval. 在均匀分布中,所有随机值都被限制在特定间隔内,并在该间隔内均匀分布。 If you generate, say, 10000 random numbers with rand , you'll find that about 1000 of them will be between 0 and 0.1, around 1000 will be between 0.1 and 0.2, around 1000 will be between 0.2 and 0.3, and so on. 如果使用rand生成10000个随机数,则会发现其中大约1000个介于0和0.1之间,大约1000个介于0.1和0.2之间,大约1000个介于0.2和0.3之间,依此类推。 And all of them will be between 0 and 1 — you won't ever get any outside that range. 所有这些都将在0到1之间-您将永远不会超出该范围。

Meanwhile, the distribution for randn looks like this: 同时, randn的分配如下所示:

正态分布

The first obvious difference between the uniform and the normal distributions is that the normal distribution has no upper or lower limits — if you generate enough random numbers with randn , you'll eventually get one that's as big or as small as you like (well, subject to the limitations of the floating point format used to store the numbers, anyway). 均匀分布和正态分布之间的第一个明显区别是,正态分布没有上限或下限-如果使用randn生成足够的随机数,最终将得到一个任意大小的(嗯,无论如何,都受用于存储数字的浮点格式的限制)。 But most of the numbers you'll get will still be fairly close to zero, because the normal distribution is not flat: the output of randn is a lot more likely to fall between, say, 0 and 0.1 than between 0.9 and 1, whereas for rand both of these are equally likely. 但是您将获得的大多数数字仍将非常接近零,因为正态分布并不平坦: randn的输出很有可能落在0到0.1之间,而不是0.9到1之间,而对于rand ,这两种可能性都是一样的。 In fact, as the picture shows, about 68% of all randn outputs fall between -1 and +1, while 95% fall between -2 and +2, and about 99.7% fall between -3 and +3. 实际上,如图所示,所有randn输出中约有68%介于-1和+1之间,而95%则介于-2和+2之间,约99.7%介于-3和+3之间。

These are completely different probability distributions. 这些是完全不同的概率分布。 If you switch one for the other, things are almost certainly going to break. 如果将一个切换为另一个,则几乎肯定会中断。 If the code doesn't simply crash, you're almost certainly going to get incorrect and/or nonsensical results. 如果代码不只是崩溃,几乎肯定会得到不正确和/或荒谬的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 中 numpy.random.rand 与 numpy.random.randn 之间的差异 - Differences between numpy.random.rand vs numpy.random.randn in Python 使用 numpy.random.randn() 在 numpy 中初始化随机权重矩阵时出现“TypeError: an integer is required” - 'TypeError: an integer is required' when initializing random weight matrix in numpy with numpy.random.randn() 具有类属性__iadd__(+ =)和numpy.random.randn()的奇怪行为 - Weird behavior with class attribute, __iadd__ (+=) and numpy.random.randn() 使用 numpy.random.randn() 生成的正态分布均值不是“0” - Mean of normal distribution generated using numpy.random.randn() is not '0' 如何将一维维度数组输入到 numpy.random.randn 中? - How to input a 1-D array of dimensions into numpy.random.randn? `numpy.empty()` 和 `numpy.random.rand()` 相同或不同 - `numpy.empty()` and `numpy.random.rand()` same or different 如何在python中更改numpy.random.rand数组的数据类型? - how to change the datatype of numpy.random.rand array in python? 如何使用 numpy.random.rand() 在 [-1,1] 范围内生成随机浮点数? - How to generate a random float in range [-1,1] using numpy.random.rand()? 与 Python3 numpy.random.rand 计算的 C++ 中的随机数相同 - Same random numbers in C++ as computed by Python3 numpy.random.rand numpy.random.rand(n)中重复的几率是多少(假设完全随机)? - What are the odds of a repeat in numpy.random.rand(n) (assuming perfect randomness)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM