optim.CG#
- class bartorch.optim.CG#
Bases:
Conjugate gradients on the normal equations of a quadratic problem.
Solves
\[\min_x \; \| A x - y \|^2 + \lambda \| x \|^2 + \sum_i w_i \| G_i x - b_i \|^2\]where the sum is over the
Tikhonovterms.- Parameters:
lambda (float, default=0.0) – \(\lambda\), the Tikhonov weight on the image.
terms (Tikhonov or iterable of Tikhonov, default=None) – Quadratic penalties \(w_i \| G_i x - b_i \|^2\).
maxiter (int, default=30) – Maximum number of iterations.
tol (float, default=0.0) – Stop once the residual of the normal equations is at most
tol * ||A^H y||; zero, BART’s default, runs every iteration.cclambda (float, default=0.0) – Weight of an identity added to the normal operator (
lsqr’scclambda).precond (LinearOperator, default=None) – Left preconditioner \(M\) from
A.ishapeto itself, positive definite (lsqr2_create’sprecond_op); BART’s reconstructions use none.device (torch.device or str, default=None) – Where the vectors of the solve live. The data, the start and the right-hand side are copied there, and the image is returned on the device the data came from, so an encoding built on a card solves for an image held on the host without the iteration’s vectors crossing at every step; the card’s cached blocks are released afterwards.
Nonesolves where the data is.
- Raises:
TypeError – A term is not a
Tikhonovpenalty, such as a proximal regularizer.
Notes
BART’s
iter2_conjgradtakes the weight \(\lambda\) and no regularizing operators or biases. The terms are therefore stacked under the encoding, \([A; \sqrt{w_i} G_i] x \approx [y; \sqrt{w_i} b_i]\), and the stacked operator is solved; its normal is applied as the sum of the parts’ normals.Examples
>>> CG(maxiter=30)(kspace, A) >>> CG(terms=Tikhonov(0.1, bias=prior))(kspace, A) >>> CG(terms=[Tikhonov(0.1, operator=G), Tikhonov(0.01)])(kspace, A) >>> CG(0.5, maxiter=5, device="cuda").inverse(rhs, A, x0=x) # (A^H A + 0.5) x = rhs
- __call__()#
Solve for the image.
- Parameters:
y (tensor) – Data of
A.oshape, or(batch, *A.oshape); a batch is solved one item at a time.A (LinearOperator) – Encoding operator.
x0 (tensor, default=None) – Starting image of
A.ishape, or one per batch item; zero when omitted.steps (list, default=None) – When given, the number of iterations each solve took is appended to it.
- Returns:
Complex64 image of
A.ishape, withy’s batch axis in front.- Return type:
torch.Tensor
Notes
When
yrequires a gradient the solve is recorded as one operation: the forward pass is the library’s iteration, and the backward pass a second solve with the same normal operator (Learned reconstruction). The warm start receives no gradient. For a linearization of a nonlinear operator the gradient also reaches the point it was linearized at. The solve can therefore serve as the data-consistency layer of an unrolled network.
- inverse()#
Solve the normal equations from their right-hand side.
Where calling the solver forms \(A^H y\) from data, this solves \(N x = b\) for a given
bofA.ishape, with \(N\) the normal operator the call inverts, \(A^H A + \lambda I\) plus the terms’ \(w_i G_i^H G_i\). A splitting method whose x-update has a right-hand side of its own, \(A^H y + \rho (z - u)\), calls this with \(A^H y\) computed once. The terms’ biases do not enter: they belong to the right-hand side, which the caller supplies.- Parameters:
b (tensor) – Right-hand side of
A.ishape.A (LinearOperator) – Encoding operator, whose normal (
gram()) is applied; an encoding built withtoeplitz=Trueapplies its point-spread convolution.x0 (tensor, default=None) – Starting image of
A.ishape; zero when omitted.
- Returns:
Complex64 image of
A.ishape, onb’s device.- Return type:
torch.Tensor
Notes
_WithNormal(Identity, A.gram())is an operator whose adjoint is the identity and whose normal is \(A^H A\), so conjugate gradients on it solves \(N x = b\) instead of \(N x = A^H b\). The backward pass of a call solves through the same route, so it costs what the forward pass does. No gradient is recorded.