Skip to content

Commit 61d040b

Browse files
committed
document new callbacks
1 parent bd1bf1d commit 61d040b

2 files changed

Lines changed: 63 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
* XX.X.X
22
- New features:
33
- LSQR algorithm added to the CIL algorithm class (#1975)
4+
- Callback improvements:
5+
- Added `Callback.interval` and `Callback.skip_iteration(Algorithm)` convenience methods (#1909)
6+
- Added `TimingCallback` and `CSVCallback`
47
- Bug fixes:
58
- `CentreOfRotationCorrector.image_sharpness` data is now correctly smoothed to reduce aliasing artefacts and improve robustness. (#2202)
69
- `PaganinProcessor` now correctly applies scaling with magnification for cone-beam geometry (#2225)

docs/source/optimisation.rst

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ SIRT
101101

102102
ISTA/PGD
103103
--------
104-
The Iterative Soft Thresholding Algorithm (ISTA) is also known as Proximal Gradient Descent (PGD). Note that in CIL, :ref:`PGD<ISTA>` is an alias of `ISTA`.
104+
The Iterative Soft Thresholding Algorithm (ISTA) is also known as Proximal Gradient Descent (PGD). Note that in CIL, :ref:`PGD<ISTA>` is an alias of `ISTA`.
105105

106106
.. _ISTA:
107107
.. autoclass:: cil.optimisation.algorithms.ISTA
@@ -111,7 +111,7 @@ The Iterative Soft Thresholding Algorithm (ISTA) is also known as Proximal Gradi
111111

112112
FISTA
113113
-----
114-
The Fast Iterative Soft Thresholding Algorithm (FISTA).
114+
The Fast Iterative Soft Thresholding Algorithm (FISTA).
115115

116116
.. _FISTA:
117117
.. autoclass:: cil.optimisation.algorithms.FISTA
@@ -120,18 +120,18 @@ The Fast Iterative Soft Thresholding Algorithm (FISTA).
120120

121121
APGD
122122
-----
123-
The Accelerated Proximal Gradient Descent Algorithm (APGD). This is an extension of the PGD/ISTA algorithm allowing you to either use a constant momemtum or a momentum that is updated at each iteration.
123+
The Accelerated Proximal Gradient Descent Algorithm (APGD). This is an extension of the PGD/ISTA algorithm allowing you to either use a constant momemtum or a momentum that is updated at each iteration.
124124

125125
.. autoclass:: cil.optimisation.algorithms.APGD
126126
:members:
127127
:inherited-members: run, update_objective_interval
128128

129129
Current options for are based on the scalar momentum, with base class:
130130

131-
.. autoclass:: cil.optimisation.algorithms.APGD.ScalarMomentumCoefficient
131+
.. autoclass:: cil.optimisation.algorithms.APGD.ScalarMomentumCoefficient
132132
:members:
133133

134-
Implemented examples are:
134+
Implemented examples are:
135135

136136
.. autoclass:: cil.optimisation.algorithms.APGD.ConstantMomentum
137137
:members:
@@ -164,25 +164,25 @@ PD3O
164164
Algorithms (Stochastic)
165165
========================
166166

167-
Consider optimisation problems that take the form of a separable sum:
167+
Consider optimisation problems that take the form of a separable sum:
168168

169169
.. math:: \min_{x} f(x)+g(x) = \min_{x} \sum_{i=0}^{n-1} f_{i}(x) + g(x) = \min_{x} (f_{0}(x) + f_{1}(x) + ... + f_{n-1}(x))+g(x)
170170

171-
where :math:`n` is the number of functions. Where there is a large number of :math:`f_i` or their gradients are expensive to calculate, stochastic optimisation methods could prove more efficient.
172-
There is a growing range of Stochastic optimisation algorithms available with potential benefits of faster convergence in number of iterations or in computational cost.
173-
This is an area of continued development for CIL and, depending on the properties of the :math:`f_i` and the regulariser :math:`g`, there is a range of different options for the user.
171+
where :math:`n` is the number of functions. Where there is a large number of :math:`f_i` or their gradients are expensive to calculate, stochastic optimisation methods could prove more efficient.
172+
There is a growing range of Stochastic optimisation algorithms available with potential benefits of faster convergence in number of iterations or in computational cost.
173+
This is an area of continued development for CIL and, depending on the properties of the :math:`f_i` and the regulariser :math:`g`, there is a range of different options for the user.
174174

175175

176176

177177
SPDHG
178178
-----
179-
Stochastic Primal Dual Hybrid Gradient (SPDHG) is a stochastic version of PDHG and deals with optimisation problems of the form:
180-
179+
Stochastic Primal Dual Hybrid Gradient (SPDHG) is a stochastic version of PDHG and deals with optimisation problems of the form:
180+
181181
.. math::
182-
182+
183183
\min_{x} f(Kx) + g(x) = \min_{x} \sum f_i(K_i x) + g(x)
184184
185-
where :math:`f_i` and the regulariser :math:`g` need only be proper, convex and lower semi-continuous ( i.e. do not need to be differentiable).
185+
where :math:`f_i` and the regulariser :math:`g` need only be proper, convex and lower semi-continuous ( i.e. do not need to be differentiable).
186186
Each iteration considers just one index of the sum, potentially reducing computational cost. For more examples see our [user notebooks]( https://github.com/vais-ral/CIL-Demos/blob/master/Tomography/Simulated/Single%20Channel/PDHG_vs_SPDHG.py).
187187

188188

@@ -194,23 +194,23 @@ Each iteration considers just one index of the sum, potentially reducing computa
194194
Approximate gradient methods
195195
----------------------------------
196196

197-
Alternatively, consider that, in addition to the functions :math:`f_i` and the regulariser :math:`g` being proper, convex and lower semi-continuous, the :math:`f_i` are differentiable. In this case we consider stochastic methods that replace a gradient calculation in a deterministic algorithm with a, potentially cheaper to calculate, approximate gradient.
197+
Alternatively, consider that, in addition to the functions :math:`f_i` and the regulariser :math:`g` being proper, convex and lower semi-continuous, the :math:`f_i` are differentiable. In this case we consider stochastic methods that replace a gradient calculation in a deterministic algorithm with a, potentially cheaper to calculate, approximate gradient.
198198
For example, when :math:`g(x)=0`, the standard Gradient Descent algorithm utilises iterations of the form
199199

200200
.. math::
201201
x_{k+1}=x_k-\alpha \nabla f(x_k) =x_k-\alpha \sum_{i=0}^{n-1}\nabla f_i(x_k).
202-
:math:`\nabla f(x_k)=\sum_{i=0}^{n-1}\nabla f_i(x_k)` with :math:`n \nabla f_i(x_k)`, for an index :math:`i` which changes each iteration, leads to the well known stochastic gradient descent algorithm.
202+
:math:`\nabla f(x_k)=\sum_{i=0}^{n-1}\nabla f_i(x_k)` with :math:`n \nabla f_i(x_k)`, for an index :math:`i` which changes each iteration, leads to the well known stochastic gradient descent algorithm.
203203

204204

205205

206-
Replacing, :math:`\nabla f(x_k)=\sum_{i=0}^{n-1}\nabla f_i(x_k)` with :math:`n \nabla f_i(x_k)`, for an index :math:`i` which changes each iteration, leads to the well known stochastic gradient descent algorithm.
206+
Replacing, :math:`\nabla f(x_k)=\sum_{i=0}^{n-1}\nabla f_i(x_k)` with :math:`n \nabla f_i(x_k)`, for an index :math:`i` which changes each iteration, leads to the well known stochastic gradient descent algorithm.
207207

208-
In addition, if :math:`g(x)\neq 0` and has a calculable proximal ( need not be differentiable) one can consider ISTA iterations:
208+
In addition, if :math:`g(x)\neq 0` and has a calculable proximal ( need not be differentiable) one can consider ISTA iterations:
209209

210210
.. math::
211211
x_{k+1}=prox_{\alpha g}(x_k-\alpha \nabla f(x_k) )=prox_{\alpha g}(x_k-\alpha \sum_{i=0}^{n-1}\nabla f_i(x_k))
212212
213-
and again replacing :math:`\nabla f(x_k)=\sum_{i=0}^{n-1}\nabla f_i(x_k)` with an approximate gradient.
213+
and again replacing :math:`\nabla f(x_k)=\sum_{i=0}^{n-1}\nabla f_i(x_k)` with an approximate gradient.
214214

215215
In a similar way, plugging approximate gradient calculations into deterministic algorithms can lead to a range of stochastic algorithms. In the following table, the left hand column has the approximate gradient function subclass, :ref:`Approximate Gradient base class` the header row has one of CIL's deterministic optimisation algorithm and the body of the table has the resulting stochastic algorithm.
216216

@@ -228,9 +228,9 @@ In a similar way, plugging approximate gradient calculations into deterministic
228228
| LSVRGFunction\| LSVRG | Prox-LSVRG | Acc-Prox-LSVRG |
229229
+----------------+-------+------------+----------------+
230230

231-
\*In development
231+
\*In development
232232

233-
The stochastic gradient functions can be found listed under functions in the documentation.
233+
The stochastic gradient functions can be found listed under functions in the documentation.
234234

235235
Stochastic Gradient Descent Example
236236
----------------------------------
@@ -239,52 +239,52 @@ The below is an example of Stochastic Gradient Descent built of the SGFunction a
239239
.. code-block :: python
240240
241241
from cil.optimisation.utilities import Sampler
242-
from cil.optimisation.algorithms import GD
242+
from cil.optimisation.algorithms import GD
243243
from cil.optimisation.functions import LeastSquares, SGFunction
244244
from cil.utilities import dataexample
245245
from cil.plugins.astra.operators import ProjectionOperator
246-
247-
# get the data
246+
247+
# get the data
248248
data = dataexample.SIMULATED_PARALLEL_BEAM_DATA.get()
249249
data.reorder('astra')
250250
data = data.get_slice(vertical='centre')
251251
252-
# create the geometries
253-
ag = data.geometry
252+
# create the geometries
253+
ag = data.geometry
254254
ig = ag.get_ImageGeometry()
255255
256256
# partition the data and build the projectors
257-
n_subsets = 10
257+
n_subsets = 10
258258
partitioned_data = data.partition(n_subsets, 'sequential')
259259
A_partitioned = ProjectionOperator(ig, partitioned_data.geometry, device = "cpu")
260260
261-
# create the list of functions for the stochastic sum
261+
# create the list of functions for the stochastic sum
262262
list_of_functions = [LeastSquares(Ai, b=bi) for Ai,bi in zip(A_partitioned, partitioned_data)]
263263
264-
#define the sampler and the stochastic gradient function
264+
#define the sampler and the stochastic gradient function
265265
sampler = Sampler.staggered(len(list_of_functions), stride=2)
266-
f = SGFunction(list_of_functions, sampler=sampler)
267-
268-
#set up and run the gradient descent algorithm
266+
f = SGFunction(list_of_functions, sampler=sampler)
267+
268+
#set up and run the gradient descent algorithm
269269
alg = GD(initial=ig.allocate(0), objective_function=f, step_size=1/f.L)
270270
alg.run(300)
271271
272272
273273
Note
274274
----
275-
All the approximate gradients written in CIL are of a similar order of magnitude to the full gradient calculation. For example, in the :code:`SGFunction` we approximate the full gradient by :math:`n\nabla f_i` for an index :math:`i` given by the sampler.
275+
All the approximate gradients written in CIL are of a similar order of magnitude to the full gradient calculation. For example, in the :code:`SGFunction` we approximate the full gradient by :math:`n\nabla f_i` for an index :math:`i` given by the sampler.
276276
The multiplication by :math:`n` is a choice to more easily allow comparisons between stochastic and non-stochastic methods and between stochastic methods with varying numbers of subsets.
277277
The multiplication ensures that the (SAGA, SGD, and SVRG and LSVRG) approximate gradients are an unbiased estimator of the full gradient ie :math:`\mathbb{E}\left[\tilde\nabla f(x)\right] =\nabla f(x)`.
278-
This has an implication when choosing step sizes. For example, a suitable step size for GD with a SGFunction could be
279-
:math:`\propto 1/(L_{max}*n)`, where :math:`L_{max}` is the largest Lipschitz constant of the list of functions in the SGFunction and the additional factor of :math:`n` reflects this multiplication by :math:`n` in the approximate gradient.
278+
This has an implication when choosing step sizes. For example, a suitable step size for GD with a SGFunction could be
279+
:math:`\propto 1/(L_{max}*n)`, where :math:`L_{max}` is the largest Lipschitz constant of the list of functions in the SGFunction and the additional factor of :math:`n` reflects this multiplication by :math:`n` in the approximate gradient.
280+
280281

281-
282282
Memory requirements
283283
-------------------
284284
Note that the approximate gradient methods have different memory requirements:
285285
+ The `SGFunction` has the same requirements as a `SumFunction`, so no increased memory usage
286-
+ `SAGFunction` and `SAGAFunction` both store `n+3` times the image size in memory to store the last calculated gradient for each function in the sum and for intermediary calculations.
287-
+ `SVRGFunction` and `LSVRGFunction` with the default `store_gradients = False` store 4 times the image size in memory, including the "snapshot" point and gradient. If `store_gradients = True`, some computational effort is saved, at the expensive of stored memory `n+4` times the image size.
286+
+ `SAGFunction` and `SAGAFunction` both store `n+3` times the image size in memory to store the last calculated gradient for each function in the sum and for intermediary calculations.
287+
+ `SVRGFunction` and `LSVRGFunction` with the default `store_gradients = False` store 4 times the image size in memory, including the "snapshot" point and gradient. If `store_gradients = True`, some computational effort is saved, at the expensive of stored memory `n+4` times the image size.
288288

289289

290290
Operators
@@ -542,55 +542,55 @@ Total variation
542542
:members:
543543
:inherited-members:
544544

545-
Function of Absolute Value
545+
Function of Absolute Value
546546
--------------------------
547547

548548
.. autoclass:: cil.optimisation.functions.FunctionOfAbs
549549
:members:
550550
:inherited-members:
551551

552552

553-
Approximate Gradient base class
553+
Approximate Gradient base class
554554
--------------------------------
555555

556-
.. autoclass:: cil.optimisation.functions.ApproximateGradientSumFunction
556+
.. autoclass:: cil.optimisation.functions.ApproximateGradientSumFunction
557557
:members:
558558
:inherited-members:
559-
560559

561-
Stochastic Gradient function
560+
561+
Stochastic Gradient function
562562
-----------------------------
563563

564-
.. autoclass:: cil.optimisation.functions.SGFunction
564+
.. autoclass:: cil.optimisation.functions.SGFunction
565565
:members:
566566
:inherited-members:
567567

568568
SAG function
569569
-------------
570570

571-
.. autoclass:: cil.optimisation.functions.SAGFunction
571+
.. autoclass:: cil.optimisation.functions.SAGFunction
572572
:members:
573573
:inherited-members:
574574

575575
SAGA function
576576
--------------
577577

578-
.. autoclass:: cil.optimisation.functions.SAGAFunction
578+
.. autoclass:: cil.optimisation.functions.SAGAFunction
579579
:members:
580580
:inherited-members:
581581

582582

583583

584-
Stochastic Variance Reduced Gradient Function
584+
Stochastic Variance Reduced Gradient Function
585585
----------------------------------------------
586-
.. autoclass:: cil.optimisation.functions.SVRGFunction
586+
.. autoclass:: cil.optimisation.functions.SVRGFunction
587587
:members:
588588
:inherited-members:
589589

590590

591-
Loopless Stochastic Variance Reduced Gradient Function
591+
Loopless Stochastic Variance Reduced Gradient Function
592592
----------------------------------------------
593-
.. autoclass:: cil.optimisation.functions.LSVRGFunction
593+
.. autoclass:: cil.optimisation.functions.LSVRGFunction
594594
:members:
595595
:inherited-members:
596596

@@ -625,12 +625,12 @@ For ease of use we provide the following static methods in `cil.optimisation.uti
625625
They will all instantiate a Sampler defined in the following class:
626626

627627
.. autoclass:: cil.optimisation.utilities.Sampler
628-
628+
629629

630630
The random samplers are instantiated from a random sampling class which is a child class of `cil.optimisation.utilities.sampler` and provides options for sampling with and without replacement:
631631

632632
.. autoclass:: cil.optimisation.utilities.SamplerRandom
633-
633+
634634

635635
Callbacks
636636
---------
@@ -657,6 +657,12 @@ Built-in callbacks include:
657657
.. autoclass:: cil.optimisation.utilities.callbacks.LogfileCallback
658658
:members:
659659

660+
.. autoclass:: cil.optimisation.utilities.callbacks.CSVCallback
661+
:members:
662+
663+
.. autoclass:: cil.optimisation.utilities.callbacks.TimingCallback
664+
:members:
665+
660666
.. autoclass:: cil.optimisation.utilities.callbacks.EarlyStoppingObjectiveValue
661667
:members:
662668

@@ -700,9 +706,9 @@ In each iteration of the :code:`TestAlgo`, the objective :math:`x` is reduced by
700706
15%|███ | 3/20 [00:00<00:00, 11770.73it/s, objective=3.05e-5]
701707
702708
703-
Step size methods
709+
Step size methods
704710
------------------
705-
A step size method is a class which acts on an algorithm and can be passed to `cil.optimisation.algorithm.GD`, `cil.optimisation.algorithm.ISTA` `cil.optimisation.algorithm.FISTA` and it's method `get_step_size` is called after the calculation of the gradient before the gradient descent step is taken. It outputs a float value to be used as the step-size.
711+
A step size method is a class which acts on an algorithm and can be passed to `cil.optimisation.algorithm.GD`, `cil.optimisation.algorithm.ISTA` `cil.optimisation.algorithm.FISTA` and it's method `get_step_size` is called after the calculation of the gradient before the gradient descent step is taken. It outputs a float value to be used as the step-size.
706712

707713
Currently in CIL we have a base class:
708714

@@ -724,7 +730,7 @@ We also have a number of example classes:
724730

725731
Preconditioners
726732
----------------
727-
A preconditioner is a class which acts on an algorithm and can be passed to `cil.optimisation.algorithm.GD`, `cil.optimisation.algorithm.ISTA` or `cil.optimisation.algorithm.FISTA` and it's method `apply` is called after the calculation of the gradient before the gradient descent step is taken. It modifies and returns a passed `gradient`.
733+
A preconditioner is a class which acts on an algorithm and can be passed to `cil.optimisation.algorithm.GD`, `cil.optimisation.algorithm.ISTA` or `cil.optimisation.algorithm.FISTA` and it's method `apply` is called after the calculation of the gradient before the gradient descent step is taken. It modifies and returns a passed `gradient`.
728734

729735
Currently in CIL we have a base class:
730736

0 commit comments

Comments
 (0)