- Notifications
You must be signed in to change notification settings - Fork913
Efficiently computes derivatives of NumPy code.
License
HIPS/autograd
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Autograd can automatically differentiate native Python and Numpy code. It canhandle a large subset of Python's features, including loops, ifs, recursion andclosures, and it can even take derivatives of derivatives of derivatives. Itsupports reverse-mode differentiation (a.k.a. backpropagation), which means itcan efficiently take gradients of scalar-valued functions with respect toarray-valued arguments, as well as forward-mode differentiation, and the two canbe composed arbitrarily. The main intended application of Autograd isgradient-based optimization. For more information, check out thetutorial and theexamples directory.
Example use:
>>>importautograd.numpyasnp# Thinly-wrapped numpy>>>fromautogradimportgrad# The only autograd function you may ever need>>>>>>deftanh(x):# Define a function...return (1.0-np.exp((-2*x)))/ (1.0+np.exp(-(2*x)))...>>>grad_tanh=grad(tanh)# Obtain its gradient function>>>grad_tanh(1.0)# Evaluate the gradient at x = 1.0np.float64(0.419974341614026)>>> (tanh(1.0001)-tanh(0.9999))/0.0002# Compare to finite differencesnp.float64(0.41997434264973155)
We can continue to differentiate as many times as we like, and use numpy'svectorization of scalar-valued functions across many different input values:
>>>fromautogradimportelementwise_gradasegrad# for functions that vectorize over inputs>>>importmatplotlib.pyplotasplt>>>x=np.linspace(-7,7,700)>>>plt.plot(x,tanh(x),...x,egrad(tanh)(x),# first derivative...x,egrad(egrad(tanh))(x),# second derivative...x,egrad(egrad(egrad(tanh)))(x),# third derivative...x,egrad(egrad(egrad(egrad(tanh))))(x),# fourth derivative>>>plt.show()
See thetanh example file for the code.
You can find a tutorialhere.
- Simple neural net
- Convolutional neural net
- Recurrent neural net
- LSTM
- Neural Turing Machine
- Backpropagating through a fluid simulation
- Variational inference in Bayesian neural network
- Gaussian process regression
- Sampyl, a pure Python MCMC package with HMC and NUTS
Install Autograd using Pip:
pip install autograd
Some features require SciPy, which you can install separately or as anoptional dependency along with Autograd:
pip install"autograd[scipy]"
Autograd was written byDougal Maclaurin,David Duvenaud,Matt Johnson,Jamie Townsendand many other contributors. The package is currently being maintained byAgriya Khetarpal,Fabian Joswig andJamie Townsend.Please feel free to submit any bugs orfeature requests. We'd also love to hear about your experiences with Autogradin general. Drop us an email!
We want to thank Jasper Snoek and the rest of the HIPS group (led by Prof. RyanP. Adams) for helpful contributions and advice; Barak Pearlmutter forfoundational work on automatic differentiation and for guidance on ourimplementation; and Analog Devices Inc. (Lyric Labs) and Samsung Advanced Instituteof Technology for their generous support.
About
Efficiently computes derivatives of NumPy code.