How to get a uniform distribution in a range [r1,r2] in PyTorch?(如何在 PyTorch 中获得 [r1,r2] 范围内的均匀分布?)
问题描述
我想得到一个大小为 [a,b] 的二维 torch.Tensor 填充了来自均匀分布的值(范围 [r1,r2]) 在 PyTorch 中.
I want to get a 2-D torch.Tensor with size [a,b] filled with values from a uniform distribution (in range [r1,r2]) in PyTorch.
推荐答案
如果 U 是均匀分布在 [0, 1] 上的随机变量,则 (r1 - r2) * U+ r2 均匀分布在 [r1, r2] 上.
If U is a random variable uniformly distributed on [0, 1], then (r1 - r2) * U + r2 is uniformly distributed on [r1, r2].
因此,您只需要:
(r1 - r2) * torch.rand(a, b) + r2
或者,您可以简单地使用:
Alternatively, you can simply use:
torch.FloatTensor(a, b).uniform_(r1, r2)
<小时>
为了充分解释这个公式,让我们看一些具体的数字:
To fully explain this formulation, let's look at some concrete numbers:
r1 = 2 # Create uniform random numbers in half-open interval [2.0, 5.0)
r2 = 5
a = 1 # Create tensor shape 1 x 7
b = 7
我们可以将表达式 (r1 - r2) * torch.rand(a, b) + r2 分解如下:
We can break down the expression (r1 - r2) * torch.rand(a, b) + r2 as follows:
torch.rand(a, b)产生一个a x b(1x7) 张量,其数字均匀分布在 [0.0, 1.0 范围内.
torch.rand(a, b)produces ana x b(1x7) tensor with numbers uniformly distributed in the range [0.0, 1.0).
x = torch.rand(a, b)
print(x)
# tensor([[0.5671, 0.9814, 0.8324, 0.0241, 0.2072, 0.6192, 0.4704]])
(r1 - r2) * torch.rand(a, b)生成分布在统一范围 [0.0, -3.0) 中的数字
(r1 - r2) * torch.rand(a, b)produces numbers distributed in the uniform range [0.0, -3.0)
print((r1 - r2) * x)
tensor([[-1.7014, -2.9441, -2.4972, -0.0722, -0.6216, -1.8577, -1.4112]])
(r1 - r2) * torch.rand(a, b) + r2生成统一范围内的数字 [5.0, 2.0)
(r1 - r2) * torch.rand(a, b) + r2produces numbers in the uniform range [5.0, 2.0)
print((r1 - r2) * x + r2)
tensor([[3.2986, 2.0559, 2.5028, 4.9278, 4.3784, 3.1423, 3.5888]])
<小时>
现在,让我们分解@Jonasson 建议的答案:(r2 - r1) * torch.rand(a, b) + r1
- 同样,
torch.rand(a, b)生成 (1x7) 均匀分布在 [0.0, 1.0] 范围内的数字.
- Again,
torch.rand(a, b)produces (1x7) numbers uniformly distributed in the range [0.0, 1.0).
x = torch.rand(a, b)
print(x)
# tensor([[0.5671, 0.9814, 0.8324, 0.0241, 0.2072, 0.6192, 0.4704]])
(r2 - r1) * torch.rand(a, b)生成均匀分布在 [0.0, 3.0 范围内的数字.
(r2 - r1) * torch.rand(a, b)produces numbers uniformly distributed in the range [0.0, 3.0).
print((r2 - r1) * x)
# tensor([[1.7014, 2.9441, 2.4972, 0.0722, 0.6216, 1.8577, 1.4112]])
(r2 - r1) * torch.rand(a, b) + r1生成在 [2.0, 5.0) 范围内均匀分布的数字
(r2 - r1) * torch.rand(a, b) + r1produces numbers uniformly distributed in the range [2.0, 5.0)
print((r2 - r1) * x + r1)
tensor([[3.7014, 4.9441, 4.4972, 2.0722, 2.6216, 3.8577, 3.4112]])
总结,(r1 - r2) * torch.rand(a, b) + r2 生成范围 [r2, r1) 内的数字,而 (r2 - r1) * torch.rand(a, b) + r1 生成 [r1, r2 范围内的数字.
In summary, (r1 - r2) * torch.rand(a, b) + r2 produces numbers in the range [r2, r1), while (r2 - r1) * torch.rand(a, b) + r1 produces numbers in the range [r1, r2).
这篇关于如何在 PyTorch 中获得 [r1,r2] 范围内的均匀分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PyTorch 中获得 [r1,r2] 范围内的均匀分布?
基础教程推荐
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
