pylops.optimization.basesolver.Solver

class pylops.optimization.basesolver.Solver(Op, callbacks=None)[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
  • 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:
Op : pylops.LinearOperator

Operator to invert of

callbacks : pylops.optimization.callback.Callbacks

Callbacks object used to implement custom callbacks

Methods

__init__(Op[, callbacks]) Initialize self.
callback(x, *args, **kwargs) Callback routine
finalize(*args[, show]) Finalize solver
run(x, *args[, show]) Run multiple steps of solver
setup(y, *args[, show]) Setup solver
solve(y, *args[, show]) Solve
step(x, *args[, show]) Run one step of solver
setup(y, *args, 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:
y : np.ndarray

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

show : bool, optional

Display setup log

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:
x : np.ndarray

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

show : bool, optional

Display step log

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:
x : np.ndarray

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

show : bool, optional

Display step log

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:
show : bool, optional

Display finalize log

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:
y : np.ndarray

Data

show : bool, optional

Display finalize log

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:
x : np.ndarray

Current solution

Examples

>>> import numpy as np
>>> from pylops.basicoperators import Identity
>>> from pylops.optimization.solver import CG
>>> def callback(x):
...     print(f"Running callback, current solution {x}")
...
>>> I = Identity(10)
>>> I
<10x10 Identity with dtype=float64>
>>> cgsolve = CG(I, np.arange(10))
>>> cgsolve.callback = callback
>>> x = np.ones(10)
>>> cgsolve.callback(x)
Running callback, current solution [1,1,1...]

Examples using pylops.optimization.basesolver.Solver