【发布时间】:2020-01-23 08:04:35
【问题描述】:
在torch.autograd.grad的documentation中声明,对于参数,
参数:
outputs(张量序列)- 微分函数的输出。
inputs(张量序列)- 输入 w.r.t. 将返回哪个梯度(而不是累积到 .grad 中)。
我尝试以下方法:
a = torch.rand(2, requires_grad=True)
b = torch.rand(2, requires_grad=True)
c = a+b
d = a-b
torch.autograd.grad([c, d], [a, b]) #ValueError: only one element tensors can be converted to Python scalars
torch.autograd.grad(torch.tensor([c, d]), torch.tensor([a, b])) #RuntimeError: grad can be implicitly created only for scalar outputs
我想得到一个张量列表的梯度 w.r.t 另一个张量列表。输入参数的正确方法是什么?
【问题讨论】:
标签: python-3.x deep-learning pytorch