當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PyTorch add用法及代碼示例

本文簡要介紹python語言中 torch.add 的用法。

用法:

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

參數

  • input(Tensor) -輸入張量。

  • other(Tensor或者數字) -要添加到輸入的張量或數字。

關鍵字參數

  • alpha(數字) -other 的乘數。

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

將由 alpha 縮放的 other 添加到 input

支持廣播到通用形狀、類型提升以及整數、浮點數和複雜輸入。

例子:

>>> 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.org大神的英文原創作品 torch.add。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。