【问题标题】:What is happening with torch.Tensor.add_?torch.Tensor.add_ 发生了什么?
【发布时间】:2020-03-14 21:23:14
【问题描述】:

我正在研究用于 PyTorch 的 SGD 实现:https://pytorch.org/docs/stable/_modules/torch/optim/sgd.html#SGD

我看到一些我不明白的奇怪计算。

例如,看看p.data.add_(-group['lr'], d_p)。认为有两个参数相乘是有道理的,对吧? (这就是 SGD 的工作原理,-lr * grads

但函数的documentation 并没有说明这一点。

更令人困惑的是,尽管这个 SGD 代码确实有效(我通过复制代码并在 add_ 下方调用 prints 进行了测试),但我不能像这样简单地使用带有两个参数的 add_

#this returns an error about using too many arguments 
import torch

a = torch.tensor([1,2,3])
b = torch.tensor([6,10,15])
c = torch.tensor([100,100,100])
a.add_(b, c)
print(a)

这里发生了什么?我错过了什么?

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    这适用于标量:

    a = t.tensor(1)
    b = t.tensor(2)
    c = t.tensor(3)
    a.add_(b, c)
    print(a)
    

    张量(7)

    或者a可以是张量:

    a = t.tensor([[1,1],[1,1]])
    b = t.tensor(2)
    c = t.tensor(3)
    a.add_(b, c)
    print(a)
    

    张量([[7, 7], [7, 7]])

    输出为 7,因为:(Tensor other, Number alpha)

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 2014-02-28
      • 2010-10-02
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2014-03-26
      • 1970-01-01
      相关资源
      最近更新 更多