Registry / ai-ml / jaxopt

jaxopt

JSON →
library0.8.5pypypi✓ verified 84d ago

JAXopt is a Python library providing hardware-accelerated, batchable, and differentiable optimizers built on JAX. It offers a wide range of solvers for convex and non-convex optimization problems, suitable for machine learning and scientific computing, including gradient descent, L-BFGS, and quadratic programming. The current version is 0.8.5, and the project maintains a frequent release cadence with bug fixes and new features.

pip install jaxopt
INSTALL
IMPORT
SIG · JAXOPT
J
jaxopt
ai-mlpythonv0.8.5
Install
11.9s avg
Import
3104ms
Disk
605MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 11.9s · import 3.104s · 586MB
605MB installed
● package 605MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GradientDescent
from jaxopt import GradientDescent
LBFGS
from jaxopt import LBFGS
OSQP
from jaxopt import OSQP

This example demonstrates how to use `GradientDescent` from JAXopt to minimize a simple quadratic loss function. It sets up dummy data, initializes model parameters, and then runs the optimizer to find the optimal parameters. This covers the basic workflow of defining an objective, choosing a solver, and executing it.

import jax import jax.numpy as jnp from jaxopt import GradientDescent # Define a quadratic function to minimize def quadratic_loss(params, data): X, y = data return jnp.mean((jnp.dot(X, params['weights']) + params['bias'] - y)**2) # Generate some dummy data key = jax.random.PRNGKey(0) num_samples = 100 num_features = 2 true_weights = jnp.array([1.0, 2.0]) true_bias = 3.0 X = jax.random.normal(key, (num_samples, num_features)) y = jnp.dot(X, true_weights) + true_bias + 0.1 * jax.random.normal(key, (num_samples,)) data = (X, y) # Initialize parameters init_params = {'weights': jnp.zeros(num_features), 'bias': 0.0} # Instantiate the optimizer gd = GradientDescent(fun=quadratic_loss, maxiter=1000, tol=1e-3) # Run the optimization sol = gd.run(init_params, data=data) print(f"Optimal parameters: {sol.params}") print(f"True weights: {true_weights}, True bias: {true_bias}")
Debug
Known issues
breakingSupport for Python 3.8 and 3.9 has been removed in recent versions (v0.8.4 and v0.8.5 respectively). Users on these Python versions must upgrade to Python 3.10 or newer.
fix
Upgrade your Python environment to version 3.10 or later.
affects: >=0.8.4
gotchaThe usage of `jax.pure_callback` has been migrated, specifically affecting how it's handled under `vmap` when `vmap_method` is not explicitly specified. The default behavior to `vmap_method='sequential'` is deprecated, and future versions will raise `NotImplementedError` without explicit `vmap_method`.
fix
If your code or custom JAXopt extensions utilize `jax.pure_callback`, explicitly define the `vmap_method` argument (e.g., `vmap_method='sequential'`) to avoid future breaking changes.
affects: >=0.8.5
gotchaEarly versions of JAXopt had issues with PyTree handling in certain solvers (e.g., `prox` functions, `BoxOSQP`), leading to incorrect behavior or errors when parameters were structured as JAX PyTrees. These have been fixed in newer releases.
fix
Ensure you are using JAXopt version 0.8.4 or later if you are experiencing issues with PyTree-structured parameters in constrained optimization problems.
affects: <0.8.4
deprecatedThe 'boston' dataset, previously used in some examples, was removed due to ethical concerns. Its removal might affect older tutorials or user code that directly referenced it.
fix
Update any code or examples referencing the 'boston' dataset to use alternative datasets, such as those recommended by scikit-learn (e.g., California housing dataset), or other suitable benchmarks.
affects: >=0.8.3
Errors
Common errors & fixes
TypeError: Scanned function carry input and carry output must have equal types (e.g. shapes and dtypes of arrays), but they differ:
This error often occurs in JAXopt's LBFGS solver when a custom objective function that uses non-JAX operations (e.g., NumPy functions) is supplied, leading to JIT compilation issues even when `jit=False` is specified, as the internal `jax.lax.scan` operation expects consistent types and shapes.
fix
Ensure that any custom objective function, and especially its outputs, are fully JAX-compatible, using `jax.numpy` and JAX control flow (`jax.lax.cond`, `jax.lax.scan`) instead of Python/NumPy equivalents, or explicitly manage jittability. The output `carry` of the scanned function must have the same type, shape, and dtype as the input `carry`.
AttributeError: 'float' object has no attribute 'shape'
This error indicates that a standard Python float was passed to a JAXopt function or an underlying JAX operation expecting a JAX array (e.g., `jax.numpy.ndarray`), which has a `.shape` attribute.
fix
Convert scalar Python floats to JAX arrays using `jax.numpy.array()` or `jnp.asarray()` before passing them to JAXopt solvers or JAX-jitted functions. Ensure all initial parameters are JAX arrays.
ValueError: init_x must be provided when fun is not None.
This error occurs in some JAXopt Quadratic Programming (QP) solvers (like `OSQP` or `BoxOSQP`) when a quadratic function `fun` is provided as an argument, but the initial primal parameters (`init_params` or `init_x`) are not explicitly supplied, which are necessary for the solver to infer problem dimensions.
fix
Provide an initial guess for the primal variables (e.g., `init_params=jnp.zeros(problem_dim)`) to the solver's `run` method when `fun` is used to define the quadratic program, especially if the primal variable shape cannot be inferred otherwise.
TypeError: true_fun and false_fun output must have identical types
This error typically arises within JAXopt solvers when conditional logic (e.g., `jax.lax.cond`) is used and the branches (`true_fun` and `false_fun`) return outputs that differ in their PyTree structure, shape, or dtype, which is not allowed under JIT compilation.
fix
Ensure that all possible return paths within JAX's conditional control flow functions (`jax.lax.cond`, `jax.lax.while_loop`) return outputs with identical PyTree structures (same number of leaves, same nested structure), and that corresponding leaves have the same shape and dtype. Padding or explicit type casting might be necessary to unify outputs.
Upgrade
Version history
0.8.5latest on PyPI · released Apr 14, 2025
Audit
Dependencies
jaxrequiredCore dependency for numerical computation and automatic differentiation.
Agent activity
6 hits · last 30 days
node
6
Resources