Skip to main content

The purpose of this package is to provide multivariable optimizers using SPSA.

Project description

Simultaneous Perturbation Stochastic Optimization (SPSA)

The purpose of this package is to provide multivariable optimizers using SPSA. Although other optimizers exist, not many implement SPSA, which has various pros and cons. Additionally, SPSA has few requirements so that you don't have to install large packages like scipy just to optimize a function.

PIP Install

Unix/macOS:

python3 -m pip install spsa

Windows:

py -m pip install spsa

Usage

Synchronous Functions:

x = spsa.optimize(f, x)
x = spsa.optimize(spsa.maximize(f), x)  # For maximization.

for variables in spsa.optimize_iterator(f, x):
    print(variables)

Asynchronous Functions:

x = await spsa.aio.optimize(f, x)
x = await spsa.aio.optimize(spsa.aio.maximize(f), x)  # For maximization.

async for variables in spsa.aio.optimize(f, x):
    print(variables)

Example

import numpy as np
import spsa

# Sample function which has a minimum at 0.
def sphere(x: np.ndarray) -> float:
    return np.linalg.norm(x) ** 2

# Attempt to find the minimum.
print(spsa.optimize(sphere, [1, 2, 3]))
# Sample result:
#     [-5.50452777e-21 -9.48070248e-21  9.78726993e-21]

Pros & Cons

A comparison of SPSA, Gradient Descent, and Bayesian Optimization are shown below.

SPSA Gradient Descent Bayesian Optimization
Calls per Iteration Constant[1] f(x) 1 fprime(x) Constant f(x)
Stochastic Stochastic f Stochastic fprime Stochastic f
Convergence Local Local Global
Dimensions Any Any <20
Lines of Code ~100 10-100 >100
Integer Optimization Applicable[2] Inapplicable Applicable[3]
Parallel Calls Applicable[4] Not Obvious Applicable

[1]: Normally requires only 2 calls, but linear search and noise-adjusting perturbation sizes require a few extra calls per iteration.

[2]: Use f(round(x)), px=0.5, px_decay=0, and px_tune=False.

[3]: Use a different Gaussian process.

[4]: See spsa.aio.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spsa-0.0.3.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

spsa-0.0.3-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file spsa-0.0.3.tar.gz.

File metadata

  • Download URL: spsa-0.0.3.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for spsa-0.0.3.tar.gz
Algorithm Hash digest
SHA256 4a3f33dbf291c4e27e82b93c2acd6b511d2527ab01e18b8865be7153943fef16
MD5 b43ec36159e05ea484a0bb5e6510a1e2
BLAKE2b-256 a8f6d50dd67cc1ebf9e29dfff96af5d6a82d7d4e0d4bb9ce68be2b1c318aec24

See more details on using hashes here.

File details

Details for the file spsa-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: spsa-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for spsa-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f68a5491f40bb1a322248b105bf277818c561dbeaf2c10e4b65a16c8ccf40d47
MD5 fc9c0c5cc2c7a8b69d8fe595ba2ac49b
BLAKE2b-256 ec4cf220b8768b1df68acc7d0068f27c7d022aa6c42737a23dd3b219bdaed6a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page