Python 用 NumPy 计算梯度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16078818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating gradient with NumPy
提问by Mikhail Elizarev
I really can not understand what numpy.gradientfunction does and how to use it for computation of multivariable function gradient.
我真的无法理解numpy.gradient函数的作用以及如何使用它来计算多变量函数梯度。
For example, I have such a function:
例如,我有这样一个功能:
def func(q, chi, delta):
return q * chi * delta
I need to compute it's 3-dimensional gradient (in other words, I want to compute partial derivatives with respect to all variables (q, chi, delta)).
我需要计算它的 3 维梯度(换句话说,我想计算关于所有变量(q、chi、delta)的偏导数)。
How can I calculate this gradient using NumPy?
如何使用 NumPy 计算此梯度?
采纳答案by Stefan
The problem is, that numpy can't give you the derivatives directly and you have two options:
问题是,numpy 不能直接给你导数,你有两个选择:
With NUMPY
与 NUMPY
What you essentially have to do, is to define a grid in three dimension and to evaluate the function on this grid. Afterwards you feed this table of function values to numpy.gradientto get an array with the numerical derivative for every dimension (variable).
您本质上要做的是定义一个三维网格并评估该网格上的函数。之后,您将这个函数值表提供numpy.gradient给一个数组,其中包含每个维度(变量)的数值导数。
Example from here:
来自这里的示例:
from numpy import *
x,y,z = mgrid[-100:101:25., -100:101:25., -100:101:25.]
V = 2*x**2 + 3*y**2 - 4*z # just a random function for the potential
Ex,Ey,Ez = gradient(V)
Without NUMPY
没有 NUMPY
You could also calculate the derivative yourself by using the centered difference quotient.

This is essentially, what numpy.gradientis doingfor every point of your predefined grid.
这实质上numpy.gradient是预定义网格的每个点的作用。
回答by zonksoft
Numpy and Scipy are for numerical calculations. Since you want to calculate the gradient of an analytical function, you have to use the Sympypackage which supports symbolic mathematics. Differentiation is explained here(you can actually use it in the web console in the left bottom corner).
Numpy 和 Scipy 用于数值计算。由于要计算解析函数的梯度,因此必须使用支持符号数学的Sympy包。这里解释了区别(您实际上可以在左下角的 Web 控制台中使用它)。
You can install Sympy under Ubuntu with
你可以在 Ubuntu 下安装 Sympy
sudo apt-get install python-sympy
or under any Linux distribution with pip
或在任何带有pip 的Linux 发行版下
sudo pip install sympy
回答by Hyman Twain
Also theanocan compute the gradient automatically
也theano可以自动计算梯度
http://deeplearning.net/software/theano/tutorial/gradients.html
http://deeplearning.net/software/theano/tutorial/gradients.html
回答by Raymond Hettinger
Numpy doesn't directly support gradient calculations without creating an entire grid of points. Instead, I would use autodifferentiationSee https://code.activestate.com/recipes/580610-auto-differentiation/for how to do it in Python.
Numpy 不直接支持梯度计算而不创建完整的点网格。相反,我会使用自动微分参见https://code.activestate.com/recipes/580610-auto-differentiation/了解如何在 Python 中做到这一点。
回答by maxbellec
You could use scipy.optimize.approx_fprime
你可以用 scipy.optimize.approx_fprime
f = lambda x: x**2
approx_fprime(np.array([2]), f, epsilon=1e-6) # array([ 4.000001])

