【问题标题】:How to sample from a distribution with constraints in PyTorch?如何从 PyTorch 中具有约束的分布中采样?
【发布时间】:2021-11-13 21:14:42
【问题描述】:

我遇到了一个简单的情况,例如:

从满足 m1~U(5, 80), m2~U(5, 80) 且约束 m1+m2 log_prob .

from torch import distributions
prior = distributions.Uniform(torch.tensor([5.0, 5.0]),
                              torch.tensor([80.0, 80.0]))

我尝试像上面那样在 PyTorch 中编码,但我不知道如何构造一个带有约束条件的torch.distribution。顺便说一下,我看到了一些关于torch.distributions.constraints 的实现,但我不知道如何使用它。

【问题讨论】:

    标签: python pytorch distribution torch


    【解决方案1】:

    这可以使用rejection sampling来实现:

    d = torch.distributions.Uniform(5, 80)
    
    m1 = 80
    m2 = 80
    
    while m1 + m2 >= 100:
        m1 = d.sample()
        m2 = d.sample()
        print(m1, m2)
    

    示例输出:

    tensor(52.3322) tensor(67.8245)
    tensor(68.3232) tensor(40.0983)
    tensor(44.7374) tensor(9.9690)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多