【问题标题】:Partial Derivative using Autograd使用 Autograd 的偏导数
【发布时间】:2017-08-09 19:52:49
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: python python-2.7 differentiation automatic-differentiation autograd


    【解决方案1】:

    我在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 函数。

    【讨论】:

    • 但是如何调用偏导函数得到具体值呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2019-01-01
    相关资源
    最近更新 更多