Numpy与Pytorch对比(二)随机数生成

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值