我有一个接受多元参数x的函数,这里x= x1,x2,x3。假设我的函数是这样的: f(x,T) = np.dot(x,T) + np.exp(np.dot(x,T),其中T是一个常数。
我对寻找df/dx1、df/dx2和df/dx3函数很感兴趣。
我使用scipy diff取得了一些成功,但我有点怀疑,因为它使用了数值差异。昨天,我的同事给我介绍了Autograd (github)。由于它似乎是一个流行的软件包,我希望这里的人知道如何使用这个软件包来获得偏微分。我对这个库的初步测试表明,grad函数只对第一个参数进行区分。我不确定如何将其扩展到其他论点。任何帮助都将不胜感激。
谢谢。
发布于 2018-03-22 18:26:21
我在autograd源代码中找到了下面对grad函数的描述:
def grad(fun, x)
"Returns a function which computes the gradient of `fun` with
respect to positional argument number `argnum`. The returned
function takes the same arguments as `fun`, but returns the
gradient instead. The function `fun`should be scalar-valued. The
gradient has the same type as the argument."所以
def h(x,t):
return np.dot(x,t) + np.exp(np.dot(x,t))
h_x = grad(h,0) # derivative with respect to x
h_t = grad(h,1) # derivative with respect to t还要确保使用autograd附带的numpy libaray
import autograd.numpy as np而不是
import numpy as np以便利用所有numpy函数。
https://stackoverflow.com/questions/45599524
复制相似问题