CallbackApply¶
- class odl.solvers.util.callback.CallbackApply(function, step=1)[source]¶
Bases:
Callback
Callback for applying a custom function to iterates.
Methods
__call__
(result)Apply function to result.
reset
()Set
iter
to 0.- __init__(function, step=1)[source]¶
Initialize a new instance.
- Parameters:
- functioncallable
Function to call on the current iterate.
- stepint, optional
Number of iterates between applications of
function
.
Examples
By default, the function is called on each iterate:
>>> def func(x): ... print(np.max(x)) >>> callback = CallbackApply(func) >>> x = odl.rn(3).element([1, 2, 3]) >>> callback(x) 3.0 >>> callback(x) 3.0
To apply only to each n-th iterate, supply
step=n
:>>> callback = CallbackApply(func, step=2) >>> callback(x) 3.0 >>> callback(x) # no output >>> callback(x) # next output 3.0