PyTorch:计算向量函数的向量-雅可比积

5

你好!

我尝试掌握torch.autograd的基础知识,特别是我想测试来自 https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#sphx-glr-beginner-blitz-autograd-tutorial-py 的语句。


enter image description here


所以我的想法是构建一个向量函数,比如:

(y_1; y_2; y_3) = (x_1*x_1 + x_2; x_2 * x_2 + x_3; x_3 * x_3)

然后在点(1,1,1)处计算Jacobian矩阵,并将其乘以向量(3,5,7)。

Jacobian = (2x_1; 1. ; 0. ) (0. ; 2x_2 ; 1. ) (0. ; 0. ; 2x_3)

我期望的结果是 Jacobian(x=(1,1,1)) * v = (6+5, 10 + 7, 2 * 7) = (11, 17, 14)。

现在以下是我在pytorch中的尝试:

import torch

x = torch.ones(3, requires_grad=True)
print(x)

y = torch.tensor([x[0]**2 + x [1], x[1]**2 + x[2], x[2]**2], requires_grad=True)
print(y)

v = torch.tensor([3, 5, 7])

y.backward(v)
x.grad

这给出了不符合预期的结果(2.,2.,1.)。我认为我错误地定义了张量y。如果我只是简单地执行y = x * 2,则梯度将起作用,但是像这种更复杂的张量怎么办?

谢谢。

1个回答

5
你不应该使用torch.tensor()来定义张量y,torch.tensor()是一个张量构造函数而不是操作符,所以在操作图中不会被跟踪。你应该使用torch.stack()代替。
只需将该行更改为:
y = torch.stack((x[0]**2+x[1], x[1]**2+x[2], x[2]**2))

x.grad的结果应该是tensor([ 6., 13., 19.])


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接