Skip to main content

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 three optional arguments:

  • fd_step — finite-difference step size (default 1e-5)
  • noise_stddev — jitter added to the step directions, which keeps the rotation matrix from going singular when successive steps align (default 7e-8)
  • seed — seed for that jitter. Unset, each run differs slightly; set it when you need reproducible results:
opt = SmartGradientOptimizer(objective_fun=rosenbrock, n=4, seed=42)

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.1

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

Source distribution (sdist)

Source distribution for smartGrad 0.1.1
File Size Uploaded Provenance
smartgrad-0.1.1.tar.gz 4.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for smartGrad 0.1.1
File Interpreter ABI Platform
smartgrad-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / smartgrad-0.1.1.tar.gz

Download URL smartgrad-0.1.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2befaddcbd22a8c6bd86af4edd0b644772d7c9d2c405673b0eb5409a0fa842d2
BLAKE2b-256 checksum
How to use checksums
b956ad6db65d927cb3bcc1d65cc78ad095084c91cbd16df9e276392222c2a142
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.1-py3-none-any.whl

Download URL smartgrad-0.1.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2166e713afb300c4e0011f8c335235967b9cba2d718c3590ec107f95025a54f
BLAKE2b-256 checksum
How to use checksums
1920a218033e8ef6ed0ed955f625944c4b0f120719b2a51d86f720608a1f1799
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.1

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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