Python+NumPy的自动区分库。
auto-diff的Python项目详细描述
自动差异
Python+NumPy的自动区分库
如何使用
API有五个公共元素:
- 在
在AutoDiff
是上下文管理器,必须使用with语句输入。__enter__
方法返回必须用于的x的新版本,而不是作为参数传递给AutoDiff
构造函数的x。在 - 在
在value
,jacobian
,get_value_and_jacobian
,这些函数必须是 在AutoDiff
上下文中调用,从 因变量。在 - 在
{{wrt}作为不同的变量返回到cd1}方法中。在
在
如果使用get_value_and_jacobian
,x必须是二维列向量,并且
必须为派生分析的值也必须是二维列
矢量。在大多数情况下,如何转换成雅可比矩阵
不明显。如果你想处理这些案件,请参阅
例子。在
auto_diff
还支持使用稀疏矩阵代替ndarray
来存储雅可比矩阵。
简单地使用SparseAutoDiff
上下文管理器,而不是AutoDiff
。
另外,如果使用SparseAutoDiff
,则需要验证代码和非NumPy依赖项是否对浮点向量使用np.ndarray
构造函数。
如果使用SparseAutoDiff
、get_value_and_jacobian
、jacobian
、和{
示例
importauto_diffimportnumpyasnp# Define a function f# f can have other arguments, if they are constant wrt x# Define the input vector, xwithauto_diff.AutoDiff(x)asx:f_eval=f(x,u)y,Jf=auto_diff.get_value_and_jacobian(f_eval)# y is the value of f(x, u) and Jf is the Jacobian of f with respect to x.
如果你需要x和u的Jacobian wrt
^{pr2}$最后,如果f
和{SparseAutoDiff
来节省内存。在
withauto_diff.SparseAutoDiff(x,u)as(x,u):f_eval=f(x,u)y,(Jfx,Jfu)=auto_diff.get_value_and_jacobians(f_eval)# y is the value of f(x, u), Jfx is the Jacobian of f with respect to x, and# Jfu is the Jacobian of f with respect to u.# Jfx and Jfu are instances of scipy.sparse.lil_matrix.
我们还可以将函数与任意形状的numpy数组区分开来
任意形状的输出。让y = f(x)
,其中x
是一个numpy形状数组
x.shape
,而{f
。在
然后,我们可以通过访问
崩溃、错误报告和反馈:
电子邮件 现在缺少一些功能。我正在处理,请随时给我发邮件
如果你想优先考虑某件事。在 可能需要NumPy>;=1.17的版本。老版本上的错误总是
提出了错误,所以不必担心。在 作者:Parth Nobel(Github:/PTNobel,parthnobel@berkeley.edu)
版本:0.3 标签:y.der
。这表示y
的每个分量相对于
^{importauto_diffimportnumpyasnpx=np.array([[np.pi],[3.0],[17.0]])withauto_diff.AutoDiff(x)asx:print(np.linalg.norm(x).der)
限制
from numpy import ...
,其中...
要么是{parthnobel@berkeley.edu
先决条件
推荐PyPI第三方库