【问题标题】:How to get a uniform distribution in a range [r1,r2] in PyTorch?如何在 PyTorch 的 [r1,r2] 范围内获得均匀分布?
【发布时间】:2017-11-03 20:11:51
【问题描述】:

我想在 PyTorch 中获得一个大小为 [a,b] 的二维 torch.Tensor,其中填充了来自均匀分布(在 [r1,r2] 范围内)的值。

【问题讨论】:

  • idk 如果只有我,但我发现 torch.rand 是非常糟糕的命名。如果不查看文档,我不知道它是高斯还是均匀(顺便说一句,由于某种原因在浏览器上加载需要很长时间)。
  • 刮掉我之前的评论。使用这个:r2 = torch.torch.distributions.Uniform(low=lb, high=ub).sample((num_samples,Din))

标签: python pytorch uniform-distribution


【解决方案1】:

如果U是均匀分布在[0, 1]上的随机变量,那么(r1 - r2) * U + r2均匀分布在[r1, r2]上。

因此,您只需要:

(r1 - r2) * torch.rand(a, b) + r2

或者,您可以简单地使用:

torch.FloatTensor(a, b).uniform_(r1, r2)

为了充分解释这个公式,让我们看一些具体的数字:

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分解如下:

  1. torch.rand(a, b) 产生一个 a x b (1x7) 张量,其数字均匀分布在 [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]])
  1. (r1 - r2) * torch.rand(a, b) 产生分布在统一范围 [0.0, -3.0) 内的数字
print((r1 - r2) * x)
tensor([[-1.7014, -2.9441, -2.4972, -0.0722, -0.6216, -1.8577, -1.4112]])
  1. (r1 - r2) * torch.rand(a, b) + r2 产生统一范围内的数字 [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

  1. 同样,torch.rand(a, b) 生成 (1x7) 个数字,均匀分布在 [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]])
  1. (r2 - r1) * torch.rand(a, b) 产生均匀分布在 [0.0, 3.0) 范围内的数字。
print((r2 - r1) * x)
# tensor([[1.7014, 2.9441, 2.4972, 0.0722, 0.6216, 1.8577, 1.4112]])
  1. (r2 - r1) * torch.rand(a, b) + r1 产生均匀分布在 [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) 内的数字。

【讨论】:

  • 不应该反过来吗? IE。 (r2-r1)*torch.rand(a,b) + r1?
  • 不一样吗?
  • 我发现将其写成a + (b - a) * torch.rand(*size) 更自然,以实现 [a, b] 上的均匀分布。
  • @stackoverflowuser2010 连续分布中的任何单个值的概率为零,因此在封闭、开放或半开放区间上定义的 PDF 为 all equivalent
  • @CharlieParker there is: torch.distributions.uniform.Uniform()
【解决方案2】:
torch.FloatTensor(a, b).uniform_(r1, r2)

【讨论】:

  • 虽然接受的答案更详细地介绍了不同的方法以及它们的工作原理,但这个答案是最简单的。
  • 我还是不明白为什么没有像 Numpy 那样的 torch.uniform 之类的东西。
  • @CharlieParker there is: torch.distributions.uniform.Uniform()
【解决方案3】:

要获得均匀的随机分布,可以使用

torch.distributions.uniform.Uniform()

例子,

import torch
from torch.distributions import uniform

distribution = uniform.Uniform(torch.Tensor([0.0]),torch.Tensor([5.0]))
distribution.sample(torch.Size([2,3])

这将给出输出,张量大小为 [2, 3]。

【讨论】:

    【解决方案4】:

    请您尝试以下方法:

    import torch as pt
    pt.empty(2,3).uniform_(5,10).type(pt.FloatTensor)
    

    【讨论】:

      【解决方案5】:

      这个答案使用 NumPy 首先生成一个随机矩阵,然后将矩阵转换为 PyTorch 张量。我发现 NumPy API 更容易理解。

      import numpy as np
      
      torch.from_numpy(np.random.uniform(low=r1, high=r2, size=(a, b)))
      

      【讨论】:

        【解决方案6】:

        查看所有发行版:https://pytorch.org/docs/stable/distributions.html#torch.distributions.uniform.Uniform

        这是我找到工作的方式:

        # generating uniform variables
        
        import numpy as np
        
        num_samples = 3
        Din = 1
        lb, ub = -1, 1
        
        xn = np.random.uniform(low=lb, high=ub, size=(num_samples,Din))
        print(xn)
        
        import torch
        
        sampler = torch.distributions.Uniform(low=lb, high=ub)
        r = sampler.sample((num_samples,Din))
        
        print(r)
        
        r2 = torch.torch.distributions.Uniform(low=lb, high=ub).sample((num_samples,Din))
        
        print(r2)
        
        # process input
        f = nn.Sequential(OrderedDict([
            ('f1', nn.Linear(Din,Dout)),
            ('out', nn.SELU())
        ]))
        Y = f(r2)
        print(Y)
        

        但我不得不承认,我不知道生成采样器的意义何在,为什么不直接调用它,就像我在一个衬里(最后一行代码)中所做的那样。

        评论:


        参考:

        【讨论】:

          【解决方案7】:

          利用torch.distributions 包生成来自不同分布的样本。

          例如,要从 range(low, high) 的均匀分布中采样大小为 [a,b] 的二维 PyTorch 张量,请尝试以下示例代码

          import torch
          a,b = 2,3   #dimension of the pytorch tensor to be generated
          low,high = 0,1 #range of uniform distribution
          
          x = torch.distributions.uniform.Uniform(low,high).sample([a,b]) 
          

          【讨论】:

            【解决方案8】:

            PyTorch 内置了许多分布。您可以使用从均匀分布中提取的元素构建所需的张量 shape,如下所示:

            from torch.distributions.uniform import Uniform
            
            shape = 3,4
            r1, r2 = 0,1
            
            x = Uniform(r1, r2).sample(shape) 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-01-22
              • 1970-01-01
              • 2020-03-23
              • 2014-03-02
              • 2011-07-16
              • 1970-01-01
              相关资源
              最近更新 更多