optax0.2.4
Published
A gradient processing and optimization library in JAX.
pip install optax
Package Downloads
Authors
Project URLs
Requires Python
>=3.9
Dependencies
- absl-py
>=0.7.1
- chex
>=0.1.87
- jax
>=0.4.27
- jaxlib
>=0.4.27
- numpy
>=1.18.0
- etils
[epy]
- sphinx
>=6.0.0; extra == "docs"
- sphinx-book-theme
>=1.0.1; extra == "docs"
- sphinxcontrib-katex
; extra == "docs"
- sphinx-autodoc-typehints
; extra == "docs"
- ipython
>=8.8.0; extra == "docs"
- myst-nb
>=1.0.0; extra == "docs"
- matplotlib
>=3.5.0; extra == "docs"
- sphinx-gallery
>=0.14.0; extra == "docs"
- sphinx-collections
>=0.0.1; extra == "docs"
- tensorflow
>=2.4.0; extra == "docs"
- tensorflow-datasets
>=4.2.0; extra == "docs"
- flax
; extra == "docs"
- sphinx_contributors
; extra == "docs"
- absl-py
>=1.0.0; extra == "dp-accounting"
- attrs
>=21.4.0; extra == "dp-accounting"
- mpmath
>=1.2.1; extra == "dp-accounting"
- numpy
>=1.21.4; extra == "dp-accounting"
- scipy
>=1.7.1; extra == "dp-accounting"
- tensorflow-datasets
>=4.2.0; extra == "examples"
- tensorflow
>=2.4.0; extra == "examples"
- dp_accounting
>=0.4; extra == "examples"
- ipywidgets
; extra == "examples"
- flax
; extra == "examples"
- dm-tree
>=0.1.7; extra == "test"
- flax
>=0.5.3; extra == "test"
- scipy
>=1.7.1; extra == "test"
- scikit-learn
; extra == "test"
Optax
Introduction
Optax is a gradient processing and optimization library for JAX.
Optax is designed to facilitate research by providing building blocks that can be easily recombined in custom ways.
Our goals are to
- Provide simple, well-tested, efficient implementations of core components.
- Improve research productivity by enabling to easily combine low-level ingredients into custom optimizers (or other gradient processing components).
- Accelerate adoption of new ideas by making it easy for anyone to contribute.
We favor focusing on small composable building blocks that can be effectively combined into custom solutions. Others may build upon these basic components in more complicated abstractions. Whenever reasonable, implementations prioritize readability and structuring code to match standard equations, over code reuse.
An initial prototype of this library was made available in JAX's experimental
folder as jax.experimental.optix
. Given the wide adoption across DeepMind
of optix
, and after a few iterations on the API, optix
was eventually moved
out of experimental
as a standalone open-source library, and renamed optax
.
Documentation on Optax can be found at optax.readthedocs.io.
Installation
You can install the latest released version of Optax from PyPI via:
pip install optax
or you can install the latest development version from GitHub:
pip install git+https://github.com/google-deepmind/optax.git
Quickstart
Optax contains implementations of many popular optimizers and
loss functions.
For example, the following code snippet uses the Adam optimizer from optax.adam
and the mean squared error from optax.l2_loss
. We initialize the optimizer
state using the init
function and params
of the model.
optimizer = optax.adam(learning_rate)
# Obtain the `opt_state` that contains statistics for the optimizer.
params = {'w': jnp.ones((num_weights,))}
opt_state = optimizer.init(params)
To write the update loop we need a loss function that can be differentiated by
Jax (with jax.grad
in this
example) to obtain the gradients.
compute_loss = lambda params, x, y: optax.l2_loss(params['w'].dot(x), y)
grads = jax.grad(compute_loss)(params, xs, ys)
The gradients are then converted via optimizer.update
to obtain the updates
that should be applied to the current parameters to obtain the new ones.
optax.apply_updates
is a convenience utility to do this.
updates, opt_state = optimizer.update(grads, opt_state)
params = optax.apply_updates(params, updates)
You can continue the quick start in the Optax 🚀 Getting started notebook.
Development
We welcome new contributors.
Source code
You can check the latest sources with the following command.
git clone https://github.com/google-deepmind/optax.git
Testing
To run the tests, please execute the following script.
sh test.sh
Documentation
To build the documentation, first ensure that all the dependencies are installed.
pip install -e ".[docs]"
Then, execute the following.
cd docs
make html
Benchmarks
If you feel lost in the crowd of available optimizers for deep learning, there exist some extensive benchmarks:
Benchmarking Neural Network Training Algorithms, Dahl G. et al, 2023,
Descending through a Crowded Valley — Benchmarking Deep Learning Optimizers, Schmidt R. et al, 2021.
If you are interested in developing your own benchmark for some tasks, consider the following framework
Benchopt: Reproducible, efficient and collaborative optimization benchmarks, Moreau T. et al, 2022.
Finally, if you are searching for some recommendations on tuning optimizers, consider taking a look at
Deep Learning Tuning Playbook, Godbole V. et al, 2023.
Citing Optax
This repository is part of the DeepMind JAX Ecosystem, to cite Optax please use the citation:
@software{deepmind2020jax,
title = {The {D}eep{M}ind {JAX} {E}cosystem},
author = {DeepMind and Babuschkin, Igor and Baumli, Kate and Bell, Alison and Bhupatiraju, Surya and Bruce, Jake and Buchlovsky, Peter and Budden, David and Cai, Trevor and Clark, Aidan and Danihelka, Ivo and Dedieu, Antoine and Fantacci, Claudio and Godwin, Jonathan and Jones, Chris and Hemsley, Ross and Hennigan, Tom and Hessel, Matteo and Hou, Shaobo and Kapturowski, Steven and Keck, Thomas and Kemaev, Iurii and King, Michael and Kunesch, Markus and Martens, Lena and Merzic, Hamza and Mikulik, Vladimir and Norman, Tamara and Papamakarios, George and Quan, John and Ring, Roman and Ruiz, Francisco and Sanchez, Alvaro and Sartran, Laurent and Schneider, Rosalia and Sezener, Eren and Spencer, Stephen and Srinivasan, Srivatsan and Stanojevi\'{c}, Milo\v{s} and Stokowiec, Wojciech and Wang, Luyu and Zhou, Guangyao and Viola, Fabio},
url = {http://github.com/google-deepmind},
year = {2020},
}