5.1.6.2. numdifftools.nd_algopy.Gradient¶
-
class
Gradient
(fun, n=1, method='forward', full_output=False)[source]¶ Calculate Gradient with Algorithmic Differentiation method
Parameters: - fun : function
function of one array fun(x, *args, **kwds)
- method : string, optional {‘forward’, ‘reverse’}
defines method used in the approximation
Returns: - grad : array
gradient
See also
Notes
Algorithmic differentiation is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. AD exploits the fact that every computer program, no matter how complicated, executes a sequence of elementary arithmetic operations (addition, subtraction, multiplication, division, etc.) and elementary functions (exp, log, sin, cos, etc.). By applying the chain rule repeatedly to these operations, derivatives of arbitrary order can be computed automatically, accurately to working precision, and using at most a small constant factor more arithmetic operations than the original program.
References
Sebastian F. Walter and Lutz Lehmann 2013, “Algorithmic differentiation in Python with AlgoPy”, in Journal of Computational Science, vol 4, no 5, pp 334 - 344, http://www.sciencedirect.com/science/article/pii/S1877750311001013
https://en.wikipedia.org/wiki/Automatic_differentiation
Examples
>>> import numdifftools.nd_algopy as nda >>> fun = lambda x: np.sum(x**2) >>> df = nda.Gradient(fun, method='reverse') >>> np.allclose(df([1,2,3]), [ 2., 4., 6.]) True
#At [x,y] = [1,1], compute the numerical gradient #of the function sin(x-y) + y*exp(x)
>>> sin = np.sin; exp = np.exp >>> z = lambda xy: sin(xy[0]-xy[1]) + xy[1]*exp(xy[0]) >>> dz = nda.Gradient(z) >>> grad2 = dz([1, 1]) >>> np.allclose(grad2, [ 3.71828183, 1.71828183]) True
#At the global minimizer (1,1) of the Rosenbrock function, #compute the gradient. It should be essentially zero.
>>> rosen = lambda x : (1-x[0])**2 + 105.*(x[1]-x[0]**2)**2 >>> rd = nda.Gradient(rosen) >>> grad3 = rd([1,1]) >>> np.allclose(grad3, [ 0., 0.]) True
Methods
__call__
(x, *args, **kwds)Call self as a function. -
__init__
(fun, n=1, method='forward', full_output=False)¶ Initialize self. See help(type(self)) for accurate signature.
Methods
__init__
(fun[, n, method, full_output])Initialize self. computational_graph
(x, *args, **kwds)Attributes
fun