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.2.tar.gz (12.0 kB view hashes)

Uploaded Source

Built Distribution

spsa-0.0.2-py3-none-any.whl (14.7 kB view hashes)

Uploaded Python 3

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