pysolvegn.Term#
- class Term(*, residual_func=None, jacobian_func=None, gradient_func=None, hessian_func=None, cost_func=None, weight=1.0, loss=None, loss_scale=1.0, finite_difference=None, finite_difference_epsilon=None)[source]#
A class representing a term in the least squares problem.
See also
For more details on the notations for the optimization problem, please refer to the mathematical section of the documentation.
For the following definition, the \(i\)-th term in the least squares problem is denoted as \(\mathbf{r}_i(\mathbf{p}) \in \mathbb{R}^{n_{r_i}}`\).
\[\min_{\mathbf{p}} \frac{1}{2} \sum_{i} w_i \sum_j \rho_i\left(\| \mathbf{r}_{i,j}(\mathbf{p}) \|^2\right)\]where \(i\) indexes the different least squares terms (
pysolvegn.Term) and \(j\) indexes the residuals within each term. Each term can be defined by two callable functions:The Residual and Jacobian functions
The residual function \(\mathbf{r}_i(\mathbf{p})\) to minimize and the associated Jacobian function \(\mathbf{J}_i(\mathbf{p}) = \frac{\partial \mathbf{r}_i}{\partial \mathbf{p}}\) to compute the Jacobian matrix of the residuals with respect to the parameters.
Then the solver will build the gradient \(\mathbf{g}(\mathbf{p})\) and the Hessian of the least squares problem \(\mathbf{H}(\mathbf{p})\) as:
\[\mathbf{g} = w_i \tilde{\mathbf{J}}_i^T \tilde{\mathbf{r}}_i\]\[\mathbf{H} = w_i \tilde{\mathbf{J}}_i^T \tilde{\mathbf{J}}_i\]such that \(\mathbf{H} \Delta \mathbf{p} = -\mathbf{g}\).
The gradient and Hessian functions
The Hessian function \(\mathbf{H}_i(\mathbf{p})\) to compute the Hessian of the least squares problem and the gradient function \(\mathbf{g}_i(\mathbf{p})\) to compute the gradient of the least squares problem.
Then the solver will use them directly without building them from the residuals and Jacobian, such that:
\[\mathbf{g} = w_i \mathbf{g}_i(\mathbf{p})\]\[\mathbf{H} = w_i \mathbf{H}_i(\mathbf{p})\]Such that \(\mathbf{H} \Delta \mathbf{p} = -\mathbf{g}\).
This class is used to represent each term in the least squares problem, and can be used to define the residual and Jacobian functions for each term, as well as the gradient and Hessian functions for each term, which can be used to build the least squares problem.
Important
The term must be define by (residual + jacobian) or (gradient + Hessian) not both.
- Parameters:
residual_func (Optional[Callable] (default=None)) – The function to compute the residuals \(\mathbf{r}_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_{r_i}}\) of the least squares problem. The function should take as inputs the parameters (1D array-like with shape
(n_parameters,)) and return the residuals as a 1D array-like with shape(n_residuals,)representing each \(\mathbf{r}_{i,j}(\mathbf{p})\) where \(j=0,...,n_{r_i}-1\).jacobian_func (Optional[Callable] (default=None)) – The function to compute the Jacobian matrix of the residuals with respect to the parameters. The function should take as inputs the parameters (1D array-like with shape
(n_parameters,)) and return the Jacobian matrix as a 2D array-like with shape(n_residuals, n_parameters)representing each \(\mathbf{J}_{i,(j,l)}(\mathbf{p}) = \frac{\partial \mathbf{r}_{i,j}}{\partial \mathbf{p}_l}\) where \(j=0,...,n_{r_i}-1}\) and \(l=0,...,n_{p}-1}\). Can be numericallty computed usingfinite_differenceargument.gradient_func (Optional[Callable] (default=None)) – The function to compute the gradient \(\mathbf{g}_i(\mathbf{p}) = \mathbf{J}_i^T \mathbf{r}_i\) of the least squares problem. The function should take as inputs the parameters (1D array-like with shape
(n_parameters,)) and return the gradient as a 1D array-like with shape(n_parameters,).hessian_func (Optional[Callable] (default=None)) – The function to compute the Hessian \(\mathbf{H}_i(\mathbf{p}) = \mathbf{J}_i^T \mathbf{J}_i\) of the least squares problem. The function should take as inputs the parameters (1D array-like with shape
(n_parameters,)) and return the Hessian matrix as a 2D array-like with shape(n_parameters, n_parameters).cost_func (Optional[Callable] (default=None)) – The function to compute the cost of the term in the least squares problem. The function should take as inputs the parameters (1D array-like with shape
(n_parameters,)) and return the cost as a positive floating value independent of theweight. If not provided, the default cost during optimization will be \(\frac{1}{2} \sum_j \rho_i(\| \mathbf{r}_{i,j}(\mathbf{p}_{out}) \|^2)\) for"rJ"terms and0.0for"gH"terms.weight (Real (default=1.0)) – The weight of the term in the least squares problem. The weight will be applied to both the gradient and the Hessian matrix.
loss (Optional[Union[str, Callable]] (default=None)) – The loss function to use for the term in the least squares problem (only for
"rJ"terms !). The loss function will affect how the gradient and Hessian are computed from the residuals and Jacobian. Iflossis a string, one of the predefined loss functions is used. Available loss functions are"linear"(\(\rho(x) = x\)) [default for None],"cauchy"(\(\rho(x) = \log(1 + x)\)),"arctan"(\(\rho(x) = \arctan(x)\)),"soft_l1"(\(\rho(x) = 2(\sqrt{1 + x} - 1)\)),"huber"(\(\rho(x) = x\) if \(x \leq 1\), \(2\sqrt{x} - 1\) otherwise), and"tukey"(\(\rho(x) = (1 - (1 - x)^3) / 3\) if \(x \leq 1\), \(1/3\) otherwise). Iflossis a callable, it is used as a custom loss function. The callable must take as input a 1D array-like containing the squared residuals and return three 1D array-like containing, respectively, the loss value, its first derivative, and its second derivative. When a custom loss function is provided, thelossproperty is set to"custom"and the corresponding callable is stored in theloss_funcproperty.loss_scale (Real (default=1.0)) – The soft threshold \(C\) of the loss function (only for
"rJ"terms !), expressed in the unit of the residuals: the residuals with \(|r| \lesssim C\) are treated as inliers and the larger ones as outliers. The loss function (predefined or custom) is replaced by its scaled version \(\rho_C(x) = C^2 \rho\left(\frac{x}{C^2}\right)\). Same asf_scaleinscipy.optimize.least_squares. It has no effect on the"linear"loss. Must be finite and strictly positive.finite_difference (Optional[str] (default=None)) – The finite difference method used to numerically compute the Jacobian when
jacobian_funcis not provided. IfNone, the Jacobian is not computed numerically. In this case,jacobian_funcmust be provided. Available methods are"central","forward", and"backward".finite_difference_epsilon (Optional[Real] (default=None)) – The relative step of the finite differences (only with
finite_difference). If None, the default step of the method is used: about1.5e-8for"forward"and"backward", about6.1e-6for"central"(seepysolvegn.build_numerical_jacobian()).
- __init__(*, residual_func=None, jacobian_func=None, gradient_func=None, hessian_func=None, cost_func=None, weight=1.0, loss=None, loss_scale=1.0, finite_difference=None, finite_difference_epsilon=None)[source]#
- Parameters:
residual_func (Callable | None)
jacobian_func (Callable | None)
gradient_func (Callable | None)
hessian_func (Callable | None)
cost_func (Callable | None)
weight (Real)
loss (str | Callable | None)
loss_scale (Real)
finite_difference (str | None)
finite_difference_epsilon (Real | None)
Methods
__init__(*[, residual_func, jacobian_func, ...])from_gH([gradient_func, hessian_func, ...])Create a
pysolvegn.Termobject from the gradient and Hessian functions.from_rJ([residual_func, jacobian_func, ...])Create a
pysolvegn.Termobject from the residual and Jacobian functions.Attributes
H_funcJ_funcc_funccost_func[Get] the cost function \(C_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}`\) of the term.
g_funcgradient_func[Get] the gradient function \(\mathbf{g}_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_p}\) of the term.
hessian_func[Get] the Hessian function \(\mathbf{H}_i: \mathbb{R}^{n_p} \rightarrow :math:\)mathbb{M}_{n_p, n_p}(mathbb{R})`` of the term.
jacobian_func[Get] the Jacobian function \(\mathbf{J}_i: \mathbb{R}^{n_p} \rightarrow :math:\)mathbb{M}_{n_{r_i}, n_p}(mathbb{R})`` of the term.
loss[Get] the name of the loss function associated with the term in the least squares problem.
loss_func[Get] the loss function \(\rho: \mathbb{R} \rightarrow \mathbb{R}`\) of the term in the least squares problem.
loss_scale[Get] the soft threshold \(C\) of the loss function, in the unit of the residuals.
r_funcresidual_func[Get] the residual function \(\mathbf{r}_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_{r_i}}\) of the term.
type[Get] the type of the term, which can be either
"rJ"for terms defined by residual and Jacobian functions, or"gH"for terms defined by second term and Hessian functions.weight[Get/Set] the weight of the term in the least squares problem.