Package: auto-diff
auto_diffAn automatic differentiation library for Python+NumPyHow To UseThere are five public elements of the API:AutoDiffis a context manager and must be entered with a with statement.
The__enter__method returns a new version of x that must be used to instead of the x passed as a parameter to theAutoDiffconstructor.value,jacobian,get_value_and_jacobian, these functions, which must be
called in anAutoDiffcontext, extract the value, Jacobian, or both from a
dependent variable.get_value_and_jacobians, if multiple vectors are passed in as arguments toAutoDiff, this method returns a tuple of Jacobians wrt to the different variables.If you are usingget_value_and_jacobian, x must be a 2D column vector, and
the value you must be parsing for the derivative must also be a 2D column
vector. In most other cases, how to convert to a Jacobian Matrix is
non-obvious. If you wish to deal with those cases see the paragraph after the
example.auto_diffalso supports using sparse matrices instead ofndarrays to store the Jacobians.
Simple use theSparseAutoDiffcontext manager instead ofAutoDiff.
Also if you useSparseAutoDiff, you need to verify that your code and none of non-NumPy dependencies use thenp.ndarrayconstructor for a floating point vector.
If usingSparseAutoDiff,get_value_and_jacobian,jacobian, andget_value_and_jacobiansreturnscipy.sparse.lil_matrixes instead ofndarrays.Exampleimportauto_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.If you need both the Jacobian wrt to x and u,withauto_diff.AutoDiff(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.Finally, iffandxare very high-dimensional, then we can useSparseAutoDiffto save memory.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.We can also differentiate functions from arbitrarily shaped numpy arrays to
arbitrarily shaped outputs. Lety = f(x), wherexis a numpy array of shapex.shape, andyis is the output of the function we wish to differentiate,f.We can then access a numpy array of shape(*y.shape, *x.shape), by accessingy.der. This represents the gradients of each component ofywith respect tox. To find the gradient of the norm of a vector x, for example one can doimportauto_diffimportnumpyasnpx=np.array([[np.pi],[3.0],[17.0]])withauto_diff.AutoDiff(x)asx:print(np.linalg.norm(x).der)RestrictionsYou must import numpy and use that object, rather then do something likefrom numpy import ..., where...is either*or just function names.Crashes, Bug Reports, and Feedback:
Emailparthnobel@berkeley.eduThere are missing features right now. I'm working on them, feel free to email me
if you want something prioritized.How It WorksParth Nobel. 2020. Auto_diff: an automatic differentiation package for Python. In Proceedings of the 2020 Spring Simulation Conference (SpringSim '20). Society for Computer Simulation International, San Diego, CA, USA, Article 10, 1–12.https://dl.acm.org/doi/10.5555/3408207.3408219PrerequisiteA version of NumPy >= 1.17 may be required. Bugs on older versions have always
raised errors, so there should be nothing to worry about.Author: Parth Nobel (Github: /PTNobel,parthnobel@berkeley.edu)
Version: 0.3