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

torch.add

torch.add(input, other, *, alpha=1, out=None) Tensor

other 加上 alpha 缩放的结果加到 input 上。

outi=inputi+alpha×otheri\text{{out}}_i = \text{{input}}_i + \text{{alpha}} \times \text{{other}}_i

支持广播到公共形状,类型提升和整数、浮点数和复数输入。

参数:
  • input (Tensor) – 输入张量。

  • 其他(张量或数字)- 要添加到 input 的张量或数字。

关键字参数:
  • alpha(数字)- other 的乘数。

  • 输出(张量,可选)- 输出张量。

示例:

>>> a = torch.randn(4)
>>> a
tensor([ 0.0202,  1.0985,  1.3506, -0.6056])
>>> torch.add(a, 20)
tensor([ 20.0202,  21.0985,  21.3506,  19.3944])

>>> b = torch.randn(4)
>>> b
tensor([-0.9732, -0.3497,  0.6245,  0.4022])
>>> c = torch.randn(4, 1)
>>> c
tensor([[ 0.3743],
        [-1.7724],
        [-0.5811],
        [-0.8017]])
>>> torch.add(b, c, alpha=10)
tensor([[  2.7695,   3.3930,   4.3672,   4.1450],
        [-18.6971, -18.0736, -17.0994, -17.3216],
        [ -6.7845,  -6.1610,  -5.1868,  -5.4090],
        [ -8.9902,  -8.3667,  -7.3925,  -7.6147]])

© 版权所有 PyTorch 贡献者。

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

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源