Skip to main content

An automatic differentiation library for Python+NumPy.

Project description

auto_diff

An automatic differentiation library for Python+NumPy

How To Use

There are five public elements of the API:

  • AutoDiff is 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 the AutoDiff constructor.

  • value, jacobian, get_value_and_jacobian, these functions, which must be called in an AutoDiff context, extract the value, Jacobian, or both from a dependent variable.

  • get_value_and_jacobians, if multiple vectors are passed in as arguments to AutoDiff, this method returns a tuple of Jacobians wrt to the different variables.

If you are using get_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_diff also supports using sparse matrices instead of ndarrays to store the Jacobians. Simple use the SparseAutoDiff context manager instead of AutoDiff. Also if you use SparseAutoDiff, you need to verify that your code and none of non-NumPy dependencies use the np.ndarray constructor for a floating point vector. If using SparseAutoDiff, get_value_and_jacobian, jacobian, and get_value_and_jacobians return scipy.sparse.lil_matrixes instead of ndarrays.

Example

import auto_diff
import numpy as np

# Define a function f
# f can have other arguments, if they are constant wrt x
# Define the input vector, x

with auto_diff.AutoDiff(x) as x:
    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,

with auto_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, if f and x are very high-dimensional, then we can use SparseAutoDiff to save memory.

with auto_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. Let y = f(x), where x is a numpy array of shape x.shape, and y is 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 accessing y.der. This represents the gradients of each component of y with respect to x. To find the gradient of the norm of a vector x, for example one can do

import auto_diff
import numpy as np
x = np.array([[np.pi], [3.0], [17.0]])

with auto_diff.AutoDiff(x) as x:
    print(np.linalg.norm(x).der)

Restrictions

  • You must import numpy and use that object, rather then do something like from numpy import ..., where ... is either * or just function names.

Crashes, Bug Reports, and Feedback: Email parthnobel@berkeley.edu

There are missing features right now. I'm working on them, feel free to email me if you want something prioritized.

How It Works

Parth 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.3408219

Prerequisite

A 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_diff-0.4.1.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

auto_diff-0.4.1-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file auto_diff-0.4.1.tar.gz.

File metadata

  • Download URL: auto_diff-0.4.1.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for auto_diff-0.4.1.tar.gz
Algorithm Hash digest
SHA256 47bf57cea347b12b0c1027d8ca1a900a88cf310c99bf11fd8dcf92344ec356c7
MD5 2553696e79f50cf584b917e56180f8a7
BLAKE2b-256 f887f5544c677732e8cca9321b902031532c5db6d7c7ebabd4c62510f9cfaf7a

See more details on using hashes here.

File details

Details for the file auto_diff-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: auto_diff-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for auto_diff-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bf06d68a06eedeab625142d0ffb5a44bf07e91073ae656356756249f8cfce9d7
MD5 c74f49f2357946d1cc6a7f8147254e8a
BLAKE2b-256 4f9dfa6345351d24cc2d74f3e6cad664a59700fb1ee291cf441a203398b1cbcb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page