smartgrad
Smart Gradient computes finite-difference gradients in a rotated coordinate system. The rotation is built from the directions the optimizer has recently moved in, so the differences are taken along the directions that matter — which in practice reaches the optimum in fewer function evaluations than plain coordinate-wise finite differences.
This is the Python implementation. R and C++ implementations live in the same repository.
Install
pip install smartgrad
Use
Wrap your objective, then hand fun_and_grad to any optimizer that takes
jac=True:
import numpy as np
from scipy.optimize import minimize
from smartgrad import SmartGradientOptimizer
def rosenbrock(x):
return sum((1 - x[i])**2 + 100 * (x[i+1] - x[i]**2)**2
for i in range(0, len(x), 2))
n = 4
opt = SmartGradientOptimizer(objective_fun=rosenbrock, n=n)
res = minimize(opt.fun_and_grad, np.zeros(n), method="L-BFGS-B", jac=True)
print(res.x, res.fun)
SmartGradientOptimizer takes two optional tuning arguments:
fd_step— finite-difference step size (default1e-5)noise_stddev— jitter added to the step directions, which keeps the rotation matrix from going singular when successive steps align (default7e-8)
If you only want the gradient itself, call opt.calculate_smart_grad(x).
Example
examples/rosenbrock.py compares Smart Gradient against SciPy's built-in
finite differences on the same problem:
python examples/rosenbrock.py
Metadata
Release files for smartGrad 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| smartgrad-0.1.0.tar.gz | 3.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| smartgrad-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.8 kB
Release files / smartgrad-0.1.0.tar.gz
| Download URL | smartgrad-0.1.0.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c43d0083eb90e7c6e8d7dd7cf4ff8b3731e7b7ac8d57c3ce0015b192fac8ed55
|
|
BLAKE2b-256 checksum How to use checksums |
5803ea16581cb5f65416015b06ec4773d59ed05768875a5379ffddaec83c0fca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.4
|
Release files / smartgrad-0.1.0-py3-none-any.whl
| Download URL | smartgrad-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0679a37a826933e9857b233d455f91530fe53abf8d34d749d07d629d489e8ef0
|
|
BLAKE2b-256 checksum How to use checksums |
649ef7aeef1c11d2e764a10f31f1b48cfe4ba190315caf3a0fcc13a20e411849
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.4
|
Security for smartGrad 0.1.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.