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 four 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.

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.

Example

import auto_diff
import numpy as np

# Define a function f
# f can have other constant 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.

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.

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.3.3.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_diff-0.3.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: auto_diff-0.3.3.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for auto_diff-0.3.3.tar.gz
Algorithm Hash digest
SHA256 ff9ad9927f615f8795cbb64ce991e72ebc824a5951c8ca7519c2a9fae5179ed5
MD5 5743ebd502617fa2d930a04c794de7fa
BLAKE2b-256 029a7f00407b5c831f21c19419b3ebb1a0658a69534678cf992f4dbc37ecd783

See more details on using hashes here.

File details

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

File metadata

  • Download URL: auto_diff-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for auto_diff-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 68bc94e1099af8bbf8edb15fb3fa0c87b5f89a05d85e403a53bbbc2429fbe90b
MD5 24518054e15afa32334270da1c735b2f
BLAKE2b-256 17b137ad47c29db21b47a53b3176e9469bef9033e4d98f4b6727d70290f83db1

See more details on using hashes here.

Supported by

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