跳转到主要内容

高效计算NumPy代码的导数。

项目描述

Autograd 检查状态 测试状态 发布状态 asv

Autograd 可以自动微分原生的 Python 和 Numpy 代码。它可以处理 Python 的大多数特性,包括循环、条件语句、递归和闭包,甚至可以对多级导数进行微分。它支持反向模式微分(也称为反向传播),这意味着它可以有效地计算标量值函数相对于数组值参数的梯度,以及正向模式微分,这两种模式可以任意组合。Autograd 的主要应用是梯度优化。更多信息请参阅 教程示例目录

示例使用

>>> import autograd.numpy as np  # Thinly-wrapped numpy
>>> from autograd import grad    # The only autograd function you may ever need
>>>
>>> def tanh(x):                 # Define a function
...     y = np.exp(-2.0 * x)
...     return (1.0 - y) / (1.0 + y)
...
>>> grad_tanh = grad(tanh)       # Obtain its gradient function
>>> grad_tanh(1.0)               # Evaluate the gradient at x = 1.0
0.41997434161402603
>>> (tanh(1.0001) - tanh(0.9999)) / 0.0002  # Compare to finite differences
0.41997434264973155

我们可以无限次地进行微分,并使用 numpy 在许多不同的输入值上对标量值函数进行向量化

>>> from autograd import elementwise_grad as egrad  # for functions that vectorize over inputs
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-7, 7, 200)
>>> plt.plot(x, tanh(x),
...          x, egrad(tanh)(x),                                     # first  derivative
...          x, egrad(egrad(tanh))(x),                              # second derivative
...          x, egrad(egrad(egrad(tanh)))(x),                       # third  derivative
...          x, egrad(egrad(egrad(egrad(tanh))))(x),                # fourth derivative
...          x, egrad(egrad(egrad(egrad(egrad(tanh)))))(x),         # fifth  derivative
...          x, egrad(egrad(egrad(egrad(egrad(egrad(tanh))))))(x))  # sixth  derivative
>>> plt.show()

请参阅 tanh 示例文件 中的代码。

文档

您可以在 此处 找到教程。

端到端示例

如何安装

使用 Pip 安装 Autograd

pip install autograd

某些功能需要 SciPy,您可以单独安装或与 Autograd 一起作为可选依赖项安装

pip install "autograd[scipy]"

作者和维护者

Autograd 由 Dougal MaclaurinDavid DuvenaudMatt JohnsonJamie Townsend 以及许多其他贡献者编写。该软件包目前由 Agriya KhetarpalFabian JoswigJamie Townsend 维护。请随时提交任何错误或功能请求。我们也想了解您使用 Autograd 的整体体验。请给我们发电子邮件!

我们要感谢 Jasper Snoek 和 HIPS 团队(由 Ryan P. Adams 教授领导)的有益贡献和建议;Barak Pearlmutter 在自动微分的基础性工作和实施指导方面;以及 Analog Devices Inc.(Lyric Labs)和 Samsung Advanced Institute of Technology 对其慷慨的支持。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

autograd-1.7.0.tar.gz (2.6 MB 查看哈希值)

上传时间 源代码

构建分发

autograd-1.7.0-py3-none-any.whl (52.5 kB 查看哈希值)

上传时间 Python 3

支持