【发布时间】:2021-08-27 07:36:58
【问题描述】:
在下面实现的代码中,我已经尝试过了,但是代码计算了两个偏导数(例如,它首先计算 d'f/d'x,然后计算 d'f/d'y)。是否有可能以某种方式修改代码,以便我们可以计算关于两个参数的这个导数?
import torch
def function(x,y):
f = x**3+y**3
return f
a = torch.tensor([4., 5., 6.], requires_grad=True)
b = torch.tensor([1., 2., 6.], requires_grad=True)
derivative = torch.autograd.functional.jacobian(function, (a,b))
print(derivative)
提前致谢!
【问题讨论】:
-
你为什么复制你的question?
-
您好,非常感谢您的回答。因为出错@Ivan,我已经删除了上一个
-
你所说的
d'f/d'x,是w.r.t的一阶导数吗?x? -
是的,不幸的是我不能直接在stackoverflow上写公式@Ivan
标签: python pytorch gradient tensor derivative