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 passedmemory_usage: a method to compute upfront the memory used by each step of the solversetup: a method that is invoked to setup the solver, basically it will create anything required prior to applying a step of the solverstep: a method applying a single step of the solverrun: a method applying multiple steps of the solverfinalize: 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 usersolve: 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 processcallback: a method implementing a callback function, which is called after every step of the solver
- Parameters:
- Op
pylops.LinearOperator Operator to invert
- callbacks
pylops.optimization.callback.Callbacks Callbacks object used to implement custom callbacks
- Op
- Attributes:
- iiter
int Iteration counter.
- preallocate
bool Whether to preallocate all variables used by the solver (
True) or not (False). Available only aftersetupis called. Note that preallocation is not always possible, for example when using JAX arrays.- tstart
float Time at the start of the optimization process.
- tend
float Time at the end of the optimization process. Available only after
finalizeis called.- telapsed
float Total time elapsed during the optimization process. Available only after
finalizeis called.
- iiter
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:
- x
numpy.ndarray Current solution
- x
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:
- show
bool, optional Display finalize log
- show
- 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.
- 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:
- x
numpy.ndarray Current model vector to be updated by multiple steps of the solver
- show
bool, optional Display step log
- x
- 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:
- y
numpy.ndarray Data of size \([N imes 1]\)
- preallocate
bool, optional Added in version 2.6.0.
Pre-allocate all variables used by the solver.
- show
bool, optional Display setup log
- y
- 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:
- y
numpy.ndarray Data
- show
bool, optional Display finalize log
- y
- 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:
- x
numpy.ndarray Current model vector to be updated by a step of the solver
- show
bool, optional Display step log
- x