首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Autograd的偏导数

使用Autograd的偏导数
EN

Stack Overflow用户
提问于 2017-08-10 03:52:49
回答 1查看 3.1K关注 0票数 3

我有一个接受多元参数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函数只对第一个参数进行区分。我不确定如何将其扩展到其他论点。任何帮助都将不胜感激。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-03-22 18:26:21

我在autograd源代码中找到了下面对grad函数的描述:

代码语言:javascript
复制
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."

所以

代码语言:javascript
复制
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

代码语言:javascript
复制
import autograd.numpy as np

而不是

代码语言:javascript
复制
import numpy as np

以便利用所有numpy函数。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45599524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档