PyPose backend¶
A wrapper that runs PyPose’s pypose.optim.GaussNewton and
pypose.optim.LevenbergMarquardt on a PyTorch residual function, with the calling
convention of piazzi.least_squares(), so you can compare them with the native solvers.
Requires the piazzi[pypose] extra; import piazzi does not load it. See the
backends guide.
- piazzi.pypose.least_squares(fun, x0, *, method='lm', solver=None, strategy=None, solver_options=None, strategy_options=None, reject=None, vectorize=True, has_aux=False, max_iter=100, step_tol=1e-05, callback=None)[source]¶
Minimise a sum of squared residuals with PyPose’s Gauss-Newton or Levenberg-Marquardt.
Runs
pypose.optim.GaussNewton(method="gn") orpypose.optim.LevenbergMarquardt(method="lm") onfun, minimising0.5 * sum(r^2)withr = fun(x), the costpiazzi.least_squares()uses (PyPose’s own loss is twice that).funruns in the dtype and on the device ofx0, and so do PyPose’s linear solves: unlikepiazzi.gauss_newton(), which solves in float64 on the CPU, a float32x0is solved in float32.x0is either a plain tensor, updated by addition, or apypose.LieTensor, updated on the left by PyPose,X <- Exp(delta) X, sofunreceives aLieTensorand the resultxis one. This replacesmanifold=: apiazzi.Manifoldcannot be used.PyPose has no stopping rules: its
steptakes one step and returns the loss. The wrapper runs up tomax_itersteps and applies piazzi’s rules to the loss each returns:an iteration whose cost is below the previous one is accepted. The run converges when the step is below
step_tol(max-norm, relative tomax|x|; in the tangent space for aLieTensor) and the cost fell by less thanstep_toltimes itself (a heavily damped step is small without being near a minimiser), or when the cost is exactly zero;an iteration whose cost is not below the previous one is rejected (the previous point is restored), and the run converges if the step is below
max(step_tol, sqrt(eps))(what the cost can resolve;epsis the machine epsilon of the dtype) and the cost rose by no more than rounding noise (8 * eps * cost), or ifxmoved by no more than8 * eps * max|x|. Any other such step ends the run unconverged: Gauss-Newton takes full steps without a line search and can diverge, and Levenberg-Marquardt stops there afterrejectrejected trials. The run then returns the best point it accepted;a non-finite cost ends the run unconverged, at the previous point.
Evaluations are counted by the wrapper. PyPose evaluates the model at the current point for the residuals, inside the Jacobian computation and for its loss; plain evaluations at a point already evaluated are answered from a one-entry memo, so each trial point costs one
n_funand each iteration onen_system(the residuals and their Jacobian), plus onen_funforx0at the start. The Jacobian evaluation is not memoised and repeats the residuals at its point, as PyPose’s does. Levenberg-Marquardt counts every rejected trial inn_fun. PyPose’s reverse-modejacobianneedsfunto be differentiable bytorch.autograd.- Parameters:
fun (
Callable) –fun(x) -> r, or(r, aux)withhas_aux=True, whereris a tensor of residuals of any shape andxhas the type and shape ofx0.funis called with a copy of the point (and with the parameter itself while PyPose differentiates, so it must not modifyxin place).x0 (
torch.Tensor) – starting point, a floating-point tensor of any shape or apypose.LieTensor.
- Keyword Arguments:
method (
str, optional) –"gn"or"lm". Default:"lm"solver (
strorCallable, optional) – the linear solver for the normal equations:"pinv","lstsq","cholesky"or"cg", or a PyPose solver such aspypose.optim.solver.PCG. Default: PyPose’s:"pinv"for"gn","cholesky"for"lm"strategy (
strorAny, optional) –"lm"only: the damping strategy,"constant","adaptive"or"trust_region", or a PyPose strategy object. Default: PyPose’s"trust_region"solver_options (
dict, optional) – constructor arguments of the solver chosen by name. Default:Nonestrategy_options (
dict, optional) – constructor arguments of the strategy chosen by name, for example{"damping": 1e-3}. Default:Nonereject (
int, optional) –"lm"only: how many unsuccessful trial steps PyPose tries in one iteration before it accepts a worse one (which the wrapper then undoes). Default: PyPose’s16vectorize (
bool, optional) – whether PyPose vectorises its Jacobian computation. Default:Truehas_aux (
bool, optional) – whetherfunalso returns an auxiliary output (any tree of tensors), returned detached inresult.auxandinfo.aux. Default:Falsemax_iter (
int, optional) – maximum number of PyPose steps. Default:100step_tol (
float, optional) – convergence tolerance on the size of an accepted step, relative tomax|x|(absolute in the tangent space of aLieTensor). Default:1e-5callback (
Callable, optional) – called after every iteration, accepted or not, with anpiazzi.IterationInfo; returningTruestops the run. Default:None
- Returns:
OptimizeResult –
xwith the type, shape, dtype and device ofx0, andfunthe cost.n_fun_gradis always0.rawis the PyPose optimiser.
See also
piazzi.least_squares(): the native solver with the same calling convention.piazzi.levenberg_marquardt(): the native Levenberg-Marquardt.
Example:
>>> t = torch.linspace(0, 4, 40, dtype=torch.float64) >>> y = 2.5 * torch.exp(-1.3 * t) >>> x0 = torch.tensor([1.0, 0.5], dtype=torch.float64) >>> res = piazzi.pypose.least_squares(lambda p: p[0] * torch.exp(-p[1] * t) - y, x0) >>> res.x tensor([2.5000, 1.3000], dtype=torch.float64)