Calculating gradient with NumPy

痴心易碎 提交于 2020-01-10 07:39:07

问题


I really can not understand what numpy.gradient function does and how to use it for computation of multivariable function 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)).

How can I calculate this gradient using NumPy?


回答1:


The problem is, that numpy can't give you the derivatives directly and you have two options:

With 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.gradient to get an array with the numerical derivative for every dimension (variable).

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

You could also calculate the derivative yourself by using the centered difference quotient.

This is essentially, what numpy.gradient is doing for every point of your predefined grid.




回答2:


Numpy and Scipy are for numerical calculations. Since you want to calculate the gradient of an analytical function, you have to use the Sympy package which supports symbolic mathematics. Differentiation is explained here (you can actually use it in the web console in the left bottom corner).

You can install Sympy under Ubuntu with

sudo apt-get install python-sympy

or under any Linux distribution with pip

sudo pip install sympy



回答3:


Also theano can compute the gradient automatically

http://deeplearning.net/software/theano/tutorial/gradients.html




回答4:


You could use scipy.optimize.approx_fprime

f = lambda x: x**2
approx_fprime(np.array([2]), f, epsilon=1e-6)  # array([ 4.000001])



回答5:


Numpy doesn't directly support gradient calculations without creating an entire grid of points. Instead, I would use autodifferentiation See https://code.activestate.com/recipes/580610-auto-differentiation/ for how to do it in Python.



来源:https://stackoverflow.com/questions/16078818/calculating-gradient-with-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!