torch.bernoulli¶
- torch.bernoulli(input: Tensor, *, generator: Optional[Generator], out: Optional[Tensor]) Tensor ¶
从伯努利分布中抽取二进制随机数(0 或 1)。
input
矩阵应该是一个包含用于抽取二进制随机数的概率的矩阵。因此,input
中的所有值都必须在 范围内。输出矩阵的 元素将根据
input
中给出的 概率值抽取 的值。返回的
out
矩阵只包含 0 或 1 的值,并且与input
的形状相同。out
可以有整数dtype
,但input
必须有浮点数dtype
。- 参数:
输入(张量)- 伯努利分布的概率值输入张量
- 关键字参数:
生成器(
torch.Generator
,可选)- 用于采样的伪随机数生成器输出(张量,可选)- 输出张量。
示例:
>>> a = torch.empty(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1] >>> a tensor([[ 0.1737, 0.0950, 0.3609], [ 0.7148, 0.0289, 0.2676], [ 0.9456, 0.8937, 0.7202]]) >>> torch.bernoulli(a) tensor([[ 1., 0., 0.], [ 0., 0., 0.], [ 1., 1., 1.]]) >>> a = torch.ones(3, 3) # probability of drawing "1" is 1 >>> torch.bernoulli(a) tensor([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) >>> a = torch.zeros(3, 3) # probability of drawing "1" is 0 >>> torch.bernoulli(a) tensor([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])