Skip to main content

Algosto implements stochastic algorithms.

Project description

Algosto : May randomness be with you

Algosto, from the concatenation of french words Algorithmes Stochastiques (/al.ɡo.ʁitm stɔ.kas.tik/), is a Python package built on top of NumPy that provides implementations of various stochastic algorithms.

The full documentation is available here.

Installation

You can install Algosto with:

pip install algosto

Usage

This section shows an example on how Algosto works by applying the stochastic gradient descent algorithm (SGD) to a quadratic function.

Workflow

The basic workflow needs four elements :

An objective function : This is the function we want to minimize. It is a Python function that takes a numpy matrix (n, d), where n is the number of points to handle and d is the dimension of points, and returns a result vector of length n.

A constraint : It is an object that defines the space in which the solver will optimize the function. Obviously, the objective function needs to be defined on this space.

A solver : In Algosto, solvers are always classes that need an objective function and a constraint to be instanciated. Simply call the fit method to minimize the objective on the constraint.

A plot : Algosto provides some functions to plot most used graph. You can build your own graph using Matplotlib or Plotly.

Objective function

Based on the workflow given just before, we start by defining the quadratic objective function and its gradient in order to use the SGD.

import numpy as np

def objective(x: np.array) -> float :
    return np.sum(x**2, axis=1)

def grad(x: np.array) -> float :
    return 2*x

[!WARNING] As said before, objective functions and gradients need to be able to process multiple points simultaneously to work with Algosto. Specifically, if the function operates on points of dimension d, it should accept a numpy array with shape (n, d) and returns a numpy array of length n, where n is the number of points provided to the objective function or gradient.

Algosto provides some toy objective functions, of which the quadratic function is a part, that you can import like this :

from algosto.utils.functions import quadratic

objective, grad = quadratic()

[!NOTE] You can find a list of all available functions in the references section of the documentation.

Constraints

Now, we need to specify the definition space. To do that, Algosto provides object called constraints that you can import from the module algosto.constraints as follow :

import numpy as np
from algosto.constraints import RdBallConstraint

ct = RdBallConstraint(2, np.zeros(2), 5)

We define a two-dimensional ball in $\mathbb{R}^d$, centered at the origin. Constraints provide the solver with information about the space within which it can optimize the objective function.

[!NOTE] You can find a list of all available constraints in the references section of the documentation.

[!NOTE] If your constraint is not yet implemented, you can define your own. Refer to the constraint chapter in the cookbook to learn how.

Solver

It's time to speak about the solver itself. Solvers are avaible from the algosto.solvers module where you can find all the solvers implemented in Algosto. In this example, we are going to use the stochastic gradient descent (SGD) to minimize the objective.

from algosto.solvers import SGDSolver

solver = SGDSolver(ct, objective)   

[!NOTE] You can find a list of all available solvers in the references section of the documentation.

Finally, we can minimize the objective function with the help of the fit method :

from algosto.utils import plot

solver.fit()

plot(solver)

Full workflow code

The full Python code is avaible just below

import numpy as np
from algosto.utils.functions import quadratic
from algosto.constraints import RdBallConstraint
from algosto.solvers import SGDSolver
from algosto.utils import plot

objective, grad = quadratic()

ct = RdBallConstraint(2, np.zeros(2), 5)

solver = SGDSolver(ct, objective, grad)

solver.fit()

plot(solver)

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

algosto-0.0.16.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

algosto-0.0.16-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file algosto-0.0.16.tar.gz.

File metadata

  • Download URL: algosto-0.0.16.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for algosto-0.0.16.tar.gz
Algorithm Hash digest
SHA256 1cf659a6c312efe0cc3a05a5ed24ad50156737457c595d48c2a299300ebde875
MD5 d4a98890a50fa1374d457528dd143ca7
BLAKE2b-256 588520bd55fb242fc056976e552f0b5cd9cb5eb528a1e49d30b5ebf49d004426

See more details on using hashes here.

File details

Details for the file algosto-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: algosto-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for algosto-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 ca82f0e19a13d8fb78f696ae0d1802073e868be95dca434bd507913438cc770c
MD5 9b94701a0170df49773d451ebb73f4ae
BLAKE2b-256 b156757d88f513b25c3ac5ccc9d5faa15f4a00dd7dba9a59e443f2b3e39bbb12

See more details on using hashes here.

Supported by

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