3

所以我正在阅读 pytorch 文档,试图学习和理解一些东西(因为我是机器学习的新手),我发现torch.bernoulli()并且我理解(我想念它)它近似于值介于 1 和0 到 1 或 0 取决于值(如经典学校小于 0.5 = 0 ,大于或等于 0.5 = 1)

经过我自己的一些实验,是的,它按预期工作

 >>>y = torch.Tensor([0.500])
 >>>x
 >>>  0.5000
     [torch.FloatTensor of size 1]    
 >>> torch.bernoulli(x)
 >>> 1
     [torch.FloatTensor of size 1]

但是当我查看文档时,有些奇怪

>>> a = torch.Tensor(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1]
>>> a

 0.7544  0.8140  0.9842
**0.5282** 0.0595  0.6445
 0.1925  0.9553  0.9732
[torch.FloatTensor of size 3x3]

>>> torch.bernoulli(a)

 1  1  1
 **0**  0  1
 0  1  1
[torch.FloatTensor of size 3x3]

在示例中0.5282近似为 0 ,这是怎么发生的?或者它是文档中的一个错误,因为我尝试了它并且0.5282按预期近似为 1。

4

1 回答 1

6

嗯,伯努利是一个概率分布。具体来说,从分布中采样并返回一个二进制值(即 0 或 1)。在这里,它以概率p返回并以概率1-p返回。torch.distributions.Bernoulli() 10

下面的例子将使理解更清楚:

In [141]: m =  torch.distributions.Bernoulli(torch.tensor([0.63]))

In [142]: m.sample() # 63% chance 1; 37% chance 0
Out[142]: tensor([ 0.])

In [143]: m.sample() # 63% chance 1; 37% chance 0
Out[143]: tensor([ 1.])

In [144]: m.sample() # 63% chance 1; 37% chance 0
Out[144]: tensor([ 0.])

In [145]: m.sample() # 63% chance 1; 37% chance 0
Out[145]: tensor([ 0.])

In [146]: m.sample() # 63% chance 1; 37% chance 0
Out[146]: tensor([ 1.])

In [147]: m.sample() # 63% chance 1; 37% chance 0
Out[147]: tensor([ 1.])

In [148]: m.sample() # 63% chance 1; 37% chance 0
Out[148]: tensor([ 1.])

In [149]: m.sample() # 63% chance 1; 37% chance 0
Out[149]: tensor([ 1.])

In [150]: m.sample() # 63% chance 1; 37% chance 0
Out[150]: tensor([ 1.])

In [151]: m.sample() # 63% chance 1; 37% chance 0
Out[151]: tensor([ 1.])

因此,我们对其进行了 10 次采样,其中我们得到了17 次,大约接近 63%。我们需要对这个有限的大量次数进行采样,以分别获得0s 和1s 的 37 和 63 的确切百分比;这是因为大数定律

于 2018-07-18T09:35:31.740 回答