Code Monkey home page Code Monkey logo

optimparallel-python's Introduction

optimparallel - A parallel version of scipy.optimize.minimize(method='L-BFGS-B')

DOI PyPI Build Status License: GPL v3

Using optimparallel.minimize_parallel() can significantly reduce the optimization time. For an objective function with an execution time of more than 0.1 seconds and p parameters the optimization speed increases by up to factor 1+p when no analytic gradient is specified and 1+p processor cores with sufficient memory are available.

A similar extension of the L-BFGS-B optimizer exists in the R package optimParallel:

Installation

To install the package run:

$ pip install optimparallel

Usage

Replace scipy.optimize.minimize(method='L-BFGS-B') by optimparallel.minimize_parallel() to execute the minimization in parallel:

from optimparallel import minimize_parallel
from scipy.optimize import minimize
import numpy as np
import time

# objective function
def f(x, sleep_secs=0.5):
    print('fn')
    time.sleep(sleep_secs)
    return sum((x - 14)**2)

# start value
x0 = np.array([10, 20])

# minimize with parallel evaluation of 'fun' and
# its approximate gradient.
o1 = minimize_parallel(fun=f, x0=x0, args=0.5)
print(o1)

# test against scipy.optimize.minimize()
o2 = minimize(fun=f, x0=x0, args=0.5, method='L-BFGS-B')
print(all(np.isclose(o1.x, o2.x, atol=1e-10)),
      np.isclose(o1.fun, o2.fun, atol=1e-10),
      all(np.isclose(o1.jac, o2.jac, atol=1e-10)))

The evaluated x values, fun(x), and jac(x) can be returned:

o1 = minimize_parallel(fun=f, x0=x0, args=0.5, parallel={'loginfo': True})
print(o1.loginfo)

More examples are given in example.py and the Jupyter Notebook example_extended.ipynb.

Note for Windows users: It may be necessary to run minimize_parallel() in the main scope. See example_windows_os.py.

Citation

When using this package please cite:

Author

Contributor

Contributions

Contributions via pull requests are welcome.

optimparallel-python's People

Contributors

florafauna avatar lewisblake avatar nikosavola avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.