因此,我读了pytorch文档,试图学习和理解一些东西(因为我对机器学习还不熟悉),我发现torch.bernoulli()和我理解(我想念它),它近似于1到0之间的值或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。
发布于 2018-07-18 09:35:31
伯努利是概率分布。具体来说,torch.distributions.Bernoulli()从分布中采样并返回一个二进制值(即0或1)。这里,它返回1与概率p,并返回0与概率1-p.
下面的例子将说明这一理解:
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次采样,其中得到了7次1,接近63%。我们需要对这个有限的大次数进行采样,才能得到0s和1s的确切百分比分别为37和63;这是因为https://en.wikipedia.org/wiki/Law_of_large_numbers。
https://stackoverflow.com/questions/51392046
复制相似问题