• 文档 >
  • torch >
  • torch.bernoulli
快捷键

torch.bernoulli

torch.bernoulli(input: Tensor, *, generator: Optional[Generator], out: Optional[Tensor]) Tensor

从伯努利分布中抽取二进制随机数(0 或 1)。

input 矩阵应该是一个包含用于抽取二进制随机数的概率的矩阵。因此, input 中的所有值都必须在 0inputi10 \leq \text{input}_i \leq 1 范围内。

输出矩阵的 ith\text{i}^{th} 元素将根据 input 中给出的 ith\text{i}^{th} 概率值抽取 11 的值。

outiBernoulli(p=inputi)\text{out}_{i} \sim \mathrm{Bernoulli}(p = \text{input}_{i})

返回的 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.]])

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,主题由 Read the Docs 提供。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源