pysolvegn.BatchTerm#
- class BatchTerm(*, 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 a batch of independent least squares problems.
This is the batched counterpart of
pysolvegn.Term. Instead of a single parameter vector with shape(n_parameters,), the solver optimizeskindependent parameter vectors stored in an array with shape(k, n_parameters). For each problem \(k\) of the batch:\[\min_{\mathbf{p}_k} \frac{1}{2} \sum_{i} w_{i,k} \sum_j \rho_i\left(\| \mathbf{r}_{i,j}(\mathbf{p}_k) \|^2\right)\]where \(i\) indexes the different terms (
pysolvegn.BatchTerm), \(j\) indexes the residuals within each term and \(w_{i,k}\) is the weight of the term \(i\) for the problem \(k\): either the same scalar weight for all the problems, or one weight per problem (vector weight with shape(k,)).As for
pysolvegn.Term, a term is defined either by its residual and Jacobian functions ("rJ"term) or by its gradient and Hessian functions ("gH"term), but not both.Important
The number of evaluated problems
mis NOT fixed.All the callables take two arguments
(parameters, indices):parameters: the parameters of the problems currently evaluated, with shape(m, n_parameters)wheremis any integer1 <= m <= k.indices: the indices of these problems in the full batch, with shape(m,).parameters[a]are the parameters of the problemindices[a].
The solver removes the stopped problems at each iteration, so
mchanges from one call to the next (and the finite differences may also call the functions with any subset). The callables must therefore never assume a fixedmnorm == k: all their outputs must havemas first dimension, and the data attached to each problem must be selected withindices(e.g.observations[indices]), never with a full-size array.- Parameters:
residual_func (Optional[Callable] (default=None)) – The function computing the residuals. Returns an array with shape
(m, n_residuals).jacobian_func (Optional[Callable] (default=None)) – The function computing the Jacobian of the residuals with respect to the parameters. Returns an array with shape
(m, n_residuals, n_parameters). Can be numerically computed usingfinite_differenceargument.gradient_func (Optional[Callable] (default=None)) – The function computing the gradient \(\mathbf{g}_i = \mathbf{J}_i^T \mathbf{r}_i\). Returns an array with shape
(m, n_parameters).hessian_func (Optional[Callable] (default=None)) – The function computing the Hessian \(\mathbf{H}_i = \mathbf{J}_i^T \mathbf{J}_i\). Returns an array with shape
(m, n_parameters, n_parameters).cost_func (Optional[Callable] (default=None)) – The function computing the cost of the term for each problem, independent of the
weight. Returns a non-negative array with shape(m,). If not provided, the default cost is \(\frac{1}{2} \sum_j \rho_i(\| \mathbf{r}_{i,j}(\mathbf{p}_k) \|^2)\) for"rJ"terms and0.0for"gH"terms.weight (Union[Real, ArrayLike] (default=1.0)) –
The weight of the term, applied to both the gradient and the Hessian.
A scalar: the same strictly positive weight is used for all the problems.
A 1D array-like with shape
(k,):weight[k]is the weight of the term for the problemkof the batch. The values must be finite and strictly positive.
loss (Optional[Union[str, Callable]] (default=None)) – The loss function (only for
"rJ"terms). Available loss functions are"linear"[default for None],"cauchy","arctan","soft_l1","huber"and"tukey"(seepysolvegn.Term). Iflossis a callable, it must take the squared residuals with shape(m, n_residuals)and return three arrays with the same shape containing the loss value, its first derivative and its second derivative. Thelossproperty is then"custom".loss_scale (Real (default=1.0)) – The soft threshold \(C\) of the loss function (only for
"rJ"terms), in the unit of the residuals. The loss function (predefined or custom) is replaced by \(\rho_C(x) = C^2 \rho(x / C^2)\), same asf_scaleinscipy.optimize.least_squares. The same scale is used for all the problems. 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 (seepysolvegn.build_batch_numerical_jacobian()). 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 | _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str])
loss (str | Callable | None)
loss_scale (Real)
finite_difference (str | None)
finite_difference_epsilon (Real | None)
Methods
__init__(*[, residual_func, jacobian_func, ...])evaluate(parameters, indices, *[, compute_cost])Compute the weighted cost, gradient and Gauss-Newton Hessian of the term for each problem.
evaluate_cost(parameters, indices)Compute the weighted cost of the term for each problem, without computing the Jacobian (useful to test a trial step).
from_gH([gradient_func, hessian_func, ...])Create a
pysolvegn.BatchTermobject from the batched gradient and Hessian functions.from_rJ([residual_func, jacobian_func, ...])Create a
pysolvegn.BatchTermobject from the batched residual and Jacobian functions.weight_at(indices)Get the weights of the term for the given problems.
Attributes
H_funcJ_funcc_funccost_func[Get] the batched cost function
(parameters (m, n_p), indices (m,)) -> (m,).g_funcgradient_func[Get] the batched gradient function
(parameters (m, n_p), indices (m,)) -> (m, n_p).hessian_func[Get] the batched Hessian function
(parameters (m, n_p), indices (m,)) -> (m, n_p, n_p).is_vector_weight[Get] whether the weight is a vector (one value per problem).
jacobian_func[Get] the batched Jacobian function
(parameters (m, n_p), indices (m,)) -> (m, n_r, n_p).loss[Get] the name of the loss function of the term (
'custom'for a callable, None for"gH"terms).loss_func[Get] the loss function of the term.
loss_scale[Get] the soft threshold \(C\) of the loss function, in the unit of the residuals (always
1.0for"gH"terms).n_problems[Get] the number of problems
kimposed by a vector weight.r_funcresidual_func[Get] the batched residual function
(parameters (m, n_p), indices (m,)) -> (m, n_r).type"rJ"(residual and Jacobian functions) or"gH"(gradient and Hessian functions).weight[Get/Set] the weight of the term.