pylops.optimization.basesolver.Solver

class pylops.optimization.basesolver.Solver[source]

This is a template class which a user must subclass when implementing a new solver. This class comprises of the following mandatory methods:

  • __init__: initialization method to which the operator Op must be passed

  • memory_usage: a method to compute upfront the memory used by each step of the solver

  • setup: a method that is invoked to setup the solver, basically it will create anything required prior to applying a step of the solver

  • step: a method applying a single step of the solver

  • run: a method applying multiple steps of the solver

  • finalize: a method that is invoked at the end of the optimization process. It can be used to do some final clean-up of the properties of the operator that we want to expose to the user

  • solve: a method applying the entire optimization loop of the solver for a certain number of steps

and optional methods:

  • _print_solver: a method print on screen details of the solver (already implemented)

  • _print_setup: a method print on screen details of the setup process

  • _print_step: a method print on screen details of each step

  • _print_finalize: a method print on screen details of the finalize process

  • callback: a method implementing a callback function, which is called after every step of the solver

Parameters:
Oppylops.LinearOperator

Operator to invert

callbackspylops.optimization.callback.Callbacks

Callbacks object used to implement custom callbacks

Attributes:
iiterint

Iteration counter.

preallocatebool

Whether to preallocate all variables used by the solver (True) or not (False). Available only after setup is called. Note that preallocation is not always possible, for example when using JAX arrays.

tstartfloat

Time at the start of the optimization process.

tendfloat

Time at the end of the optimization process. Available only after finalize is called.

telapsedfloat

Total time elapsed during the optimization process. Available only after finalize is called.

Methods

__init__(Op[, callbacks])

callback(x, *args, **kwargs)

Callback routine

finalize(*args[, show])

Finalize solver

memory_usage([show, unit])

Compute memory usage of the solver

run(x, *args[, show])

Run multiple steps of solver

setup(y, *args[, preallocate, show])

Setup solver

solve(y, *args[, show])

Solve

step(x, *args[, show])

Run one step of solver

Solver.callback(x, *args, **kwargs)[source]

Callback routine

This routine must be passed by the user. Its function signature must contain a single input that contains the current solution (when using the solve method it will be automatically invoked after each step of the solve)

Parameters:
xnumpy.ndarray

Current solution

Examples

>>> import numpy as np
>>> from pylops.basicoperators import Identity
>>> from pylops.optimization.cls_basic import CG
>>> def callback(x):
...     print(f"Running callback, current solution {x}")
...
>>> IOp = Identity(10)
>>> IOp
<10x10 Identity with dtype=float64>
>>> cgsolve = CG(IOp)
>>> cgsolve.callback = callback
>>> x = np.ones(10)
>>> cgsolve.callback(x)
Running callback, current solution [1,1,1...]
Solver.finalize(*args, show=False, **kwargs)[source]

Finalize solver

This method is used to finalize the solver. Users can change the function signature by including any other input parameter required when finalizing the solver

Parameters:
showbool, optional

Display finalize log

abstractmethod Solver.memory_usage(show=False, unit='B')[source]

Compute memory usage of the solver

This method computes an estimate of the memory required by the solver given the shape of the operator. This is useful to assess upfront if the solver will run out of memory.

Note, that the memory usage of the operator itself is not taken into account in this estimate.

Parameters:
showbool, optional

Display memory usage

unit: :obj:`str`, optional

Unit used to display memory usage ( B, KB, MB or GB)

Returns:
memuse float

Memory usage in bytes

abstractmethod Solver.run(x, *args, show=False, **kwargs)[source]

Run multiple steps of solver

This method is used to run multiple step of the solver. Users can change the function signature by including any other input parameter required when applying multiple steps of the solver

Parameters:
xnumpy.ndarray

Current model vector to be updated by multiple steps of the solver

showbool, optional

Display step log

abstractmethod Solver.setup(y, *args, preallocate=False, show=False, **kwargs)[source]

Setup solver

This method is used to setup the solver. Users can change the function signature by including any other input parameter required during the setup stage

Parameters:
ynumpy.ndarray

Data of size \([N imes 1]\)

preallocatebool, optional

Added in version 2.6.0.

Pre-allocate all variables used by the solver.

showbool, optional

Display setup log

abstractmethod Solver.solve(y, *args, show=False, **kwargs)[source]

Solve

This method is used to run the entire optimization process. Users can change the function signature by including any other input parameter required by the solver

Parameters:
ynumpy.ndarray

Data

showbool, optional

Display finalize log

abstractmethod Solver.step(x, *args, show=False, **kwargs)[source]

Run one step of solver

This method is used to run one step of the solver. Users can change the function signature by including any other input parameter required when applying one step of the solver

Parameters:
xnumpy.ndarray

Current model vector to be updated by a step of the solver

showbool, optional

Display step log

Examples using pylops.optimization.basesolver.Solver

Linear Regression

Linear Regression

03. Solvers (Advanced)

03. Solvers (Advanced)