Model#
- class cuqi.model.Model(forward, range_geometry, domain_geometry, gradient=None, jacobian=None)#
Generic model defined by a forward operator.
- Parameters:
forward (2D ndarray or callable function.) – Forward operator.
range_geometry (integer or cuqi.geometry.Geometry) – If integer is given, a cuqi.geometry._DefaultGeometry is created with dimension of the integer.
domain_geometry (integer or cuqi.geometry.Geometry) – If integer is given, a cuqi.geometry._DefaultGeometry is created with dimension of the integer.
gradient (callable function, optional) – The direction-Jacobian product of the forward operator Jacobian with respect to the forward operator input, evaluated at a point (wrt). The signature of the gradient function should be (direction, wrt), where direction is the direction by which the Jacobian matrix is multiplied and wrt is the point at which the Jacobian is computed.
jacobian (callable function, optional) – The Jacobian of the forward operator with respect to the forward operator input, evaluated at a point (wrt). The signature of the Jacobian function should be (wrt). The Jacobian function should return a 2D ndarray of shape (range_dim, domain_dim). The Jacobian function is used to specify the gradient function by computing the vector-Jacobian product (VJP), here we refer to the vector in the VJP as the direction since it is the direction at which the gradient is computed. automatically and thus the gradient function should not be specified when the Jacobian function is specified.
- Variables:
range_geometry – The geometry representing the range.
domain_geometry – The geometry representing the domain.
Example
Consider a forward model \(F: \mathbb{R}^2 \rightarrow \mathbb{R}\) defined by the following forward operator:
\[F(x) = 10x_2 - 10x_1^3 + 5x_1^2 + 6x_1\]The jacobian matrix of the forward operator is given by:
\[J_F(x) = \begin{bmatrix} -30x_1^2 + 10x_1 + 6 & 10 \end{bmatrix}\]The forward model can be defined as follows:
import numpy as np from cuqi.model import Model def forward(x): return 10*x[1] - 10*x[0]**3 + 5*x[0]**2 + 6*x[0] def jacobian(x): # Can use "x" or "wrt" as the input argument name return np.array([[-30*x[0]**2 + 10*x[0] + 6, 10]]) model = Model(forward, range_geometry=1, domain_geometry=2, jacobian=jacobian)
Alternatively, the gradient information in the forward model can be defined by direction-Jacobian product using the gradient keyword argument.
This may be more efficient if forming the Jacobian matrix is expensive.
import numpy as np from cuqi.model import Model def forward(x): return 10*x[1] - 10*x[0]**3 + 5*x[0]**2 + 6*x[0] def gradient(direction, wrt): # Direction-Jacobian product direction@jacobian(wrt) return direction@np.array([[-30*wrt[0]**2 + 10*wrt[0] + 6, 10]]) model = Model(forward, range_geometry=1, domain_geometry=2, gradient=gradient)
- __init__(forward, range_geometry, domain_geometry, gradient=None, jacobian=None)#
Methods
__init__
(forward, range_geometry, ...[, ...])forward
(*args[, is_par])Forward function of the model.
gradient
(direction, wrt[, is_direction_par, ...])Gradient of the forward operator (Direction-Jacobian product)
Attributes
The dimension of the domain
The dimension of the range