快捷方式

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) – 輸入張量。

  • other (TensorNumber) – 要加到 input 上的張量或數字。

關鍵字引數
  • alpha (Number) – other 的乘數。

  • out (Tensor, 可選) – 輸出張量。

示例

>>> 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 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源