Skip to main content

Zeroth-Learn

A NumPy library for estimating gradients from loss evaluations and training black-box models.

The library implements coordinate finite differences, random-direction estimation, and SGD/Adam updates. Its MNIST experiments check that these components can train models when backpropagation is unavailable. The estimator and optimizer interfaces can also be used with other black boxes, including the simulator in Quantum-Learn.

1. Optimisation without derivatives

Let $\theta\in\mathbb R^d$ be model parameters and $f(\theta)$ a mini-batch loss. The model exposes values of $f$, but not $\nabla f$. Zeroth-order optimisation estimates a descent direction by evaluating nearby parameters:

\theta_{k+1}=\theta_k-\eta_k\widehat{\nabla f}(\theta_k).

For Adam, the estimated gradient enters the usual first- and second-moment update in place of an exact gradient. No derivative of the black-box model is required.

2. Finite-difference estimators

A one-sided coordinate difference estimates component $i$ as

\widehat{\partial_i f}(\theta)
=\frac{f(\theta+\delta e_i)-f(\theta)}{\delta}.

Estimating all $d$ components needs $d+1$ loss evaluations. GlobalFiniteDifference does this for every coordinate; PartialFiniteDifference evaluates only selected coordinates and returns zero elsewhere.

For a cheaper estimate in high dimension, SimultaneousPerturbation draws $T$ Rademacher directions. At construction, the matrix $P\in\mathbb R^{T\times d}$ has entries $P_{ij}=s_{ij}/\sqrt T$, where each $s_{ij}$ is $+1$ or $-1$ with equal probability. The implementation uses

\widehat{\nabla f}(\theta)
=\frac{1}{\delta}P^\top
\begin{pmatrix}
f(\theta+\delta P_1)-f(\theta)\\
\vdots\\
f(\theta+\delta P_T)-f(\theta)
\end{pmatrix}.

Because $\mathbb E[P^\top P]=I$, the first-order term recovers $\nabla f$ in expectation. Each update costs $T+1$ loss evaluations, independent of $d$ for a fixed $T$. Larger $T$ averages more directions but increases evaluation cost and memory.

3. From an estimator to a model update

The interfaces separate three operations:

Operation Implementation
Generate perturbed parameters and reconstruct the gradient gradient_estimators.py
Generate Rademacher directions perturbation_matrices.py
Evaluate a model at nominal and perturbed parameters zeroth_order_blackbox.py
Apply SGD or Adam to the estimated gradient optimizers.py

The included ZerothOrderNeuralNetwork is one black-box implementation. ParameterManager maps weights and biases to a flat $\theta$; NumPy broadcasting evaluates its $T+1$ perturbed networks in one batch. This batching reduces Python overhead, not the number of loss evaluations. Other models choose how to perform those evaluations.

4. Numerical checks

A deterministic test applies the random-direction estimator to a linear function, whose gradient is known. The MNIST experiments provide end-to-end checks of zeroth-order training on CPU. Their protocols, results, plots, and raw loss traces are in Experiments.

5. Reproduce the experiments

With uv, the lockfile fixes the dependency versions. The first experiment run downloads MNIST from OpenML.

git clone https://github.com/nicolasmalet/Zeroth-Learn.git
cd Zeroth-Learn
uv sync --locked --extra mnist
uv run --locked --extra mnist python -m unittest discover -s tests
MPLBACKEND=Agg uv run --locked --extra mnist python -m lab.mnist
MPLBACKEND=Agg uv run --locked --extra mnist python -m lab.mnist.run_experiment nb_perturbations_vs_model_size

The experiments write accuracy, loss traces, and plots under results/.

Without uv, install the package with its MNIST extra in a virtual environment, then run the same Python modules.

6. Scope

The MNIST experiments validate classical models. They are not benchmarks of quantum hardware or of PyTorch, and their saved plots do not contain repeated trials or uncertainty estimates. Memory use grows with the number of perturbations in the vectorized neural-network implementation.

Metadata

Release files for zeroth-learn 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zeroth-learn 0.2.6
File Size Uploaded
zeroth_learn-0.2.6.tar.gz 22.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zeroth-learn 0.2.6
File Interpreter ABI Platform
zeroth_learn-0.2.6-py3-none-any.whl Python 3 none any Details

Total release size: 54.1 kB

Release files / zeroth_learn-0.2.6.tar.gz

Download URL zeroth_learn-0.2.6.tar.gz
Size 22.8 kB
Tags Source
SHA-256 checksum
How to use checksums
79a9e23765564d144ad05561ffd633ee3047c6205508f8e82bd8827c1d60d0c2
BLAKE2b-256 checksum
How to use checksums
e9059958f02af6e96844afccdd2f5a8e961995f36f1e203821c06f35ef415e1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release files / zeroth_learn-0.2.6-py3-none-any.whl

Download URL zeroth_learn-0.2.6-py3-none-any.whl
Size 31.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a3f353b8262452adf88b801edb234c9a3e49b7f2d1f42cc68002f30a248bcd4
BLAKE2b-256 checksum
How to use checksums
ffe300eeed082c2b5707cb682cc598c48c00d1e1580ae577896bd9958f015b95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.6 This release

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page