python-3.x - Pytorch autograd.grad 如何写多个输出的参数?

标签 python-3.x deep-learning pytorch

documentationtorch.autograd.grad ,据说,对于参数,

parameters:

outputs (sequence of Tensor) – outputs of the differentiated function.

inputs (sequence of Tensor) – Inputs w.r.t. which the gradient will be returned (and not accumulated into .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 另一个张量列表。输入参数的正确方法是什么?

最佳答案

torch.autograd.grad提到,torch.autograd.grad计算并返回输出 w.r.t. 的梯度总和。输入。由于您的 cd不是标量值,grad_outputs是必要的。

import torch

a = torch.rand(2,requires_grad=True)
b = torch.rand(2, requires_grad=True)

a
# tensor([0.2308, 0.2388], requires_grad=True)

b
# tensor([0.6314, 0.7867], requires_grad=True)

c = a*a + b*b
d = 2*a+4*b

torch.autograd.grad([c,d], inputs=[a,b], grad_outputs=[torch.Tensor([1.,1.]), torch.Tensor([1.,1.])])
# (tensor([2.4616, 2.4776]), tensor([5.2628, 5.5734]))

解释:dc/da = 2*a = [0.2308*2, 0.2388*2]dd/da = [2.,2.]所以第一个输出是 dc/da*grad_outputs[0]+dd/da*grad_outputs[1] = [2.4616, 2.4776] .第二个输出的计算相同。

如果你只是想得到c的梯度和 d w.r.t.输入,也许你可以这样做:

a = torch.rand(2,requires_grad=True)
b = torch.rand(2, requires_grad=True)

a
# tensor([0.9566, 0.6066], requires_grad=True)
b
# tensor([0.5248, 0.4833], requires_grad=True)

c = a*a + b*b
d = 2*a+4*b

[torch.autograd.grad(t, inputs=[a,b], grad_outputs=[torch.Tensor([1.,1.])]) for t in [c,d]]
# [(tensor([1.9133, 1.2132]), tensor([1.0496, 0.9666])),
# (tensor([2., 2.]), tensor([4., 4.]))]

关于python-3.x - Pytorch autograd.grad 如何写多个输出的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059268/

相关文章:

python - lru_cache 转储到文件中并再次加载回内存中

machine-learning - 每种类型的神经网络(RNN、CNN、LSTM 等)的优势在哪里?

tensorflow - 为什么 'dimension' 在机器学习领域有几个不同的含义?

python - 值错误: expected 2D or 3D input (got 1D input) PyTorch

python - 如何有效地解码 PyTorch 中的嵌入?

python-3.x - iPython/Jupyter notebook 只清除一行输出

python - docker-compose “/usr/local/bin/python: error while loading shared libraries: libpython3.8.so.1.0: ”时出错

python - 在 Django StaticLiveServerTestCase 期间在测试输出中显示服务器错误?

machine-learning - 为什么RL叫 'reinforcement'学习?

neural-network - 如何仅在一个维度(按行或按列)将线性层应用于二维层 - 部分连接层