np.random
np.random.rand()
均匀分布
Create an array of the given shape and populate it with random samples from a uniform distribution [0, 1).
np.random.random()
均匀分布
Return random floats in the half-open interval [0.0, 1.0).
Results are from the “continuous uniform” distribution over the stated interval.
Sample from [a, b), b > a : (b - a) * random_sample() + a
np.random.randn()
正态分布
Return a sample (or samples) from the “standard normal” distribution.
np.random.randint()
Return random integers from low (inclusive) to high (exclusive).
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
Pytorch
torch.rand
均匀分布
Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)
torch.randn
正态分布
Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).
torch.randint
均匀分布
Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive)
>>> torch.randint(3, 5, (3,))
tensor([ 4., 3., 4.])
总结
1、np.random.random()和np.random.rand()的区别:前者可以无需输入参数返回单个浮点数;
2、对于randint,两个框架的操作是基本一致的。
3、np.random.rand <=> torch.rand 均匀分布
4、np.random.randn <=> torch.randn 正态分布
其实两个框架各自还有很多api,这里只选了几个比较相似的来对比哈orz