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") or pypose.optim.LevenbergMarquardt (method="lm") on fun, minimising 0.5 * sum(r^2) with r = fun(x), the cost piazzi.least_squares() uses (PyPose’s own loss is twice that). fun runs in the dtype and on the device of x0, and so do PyPose’s linear solves: unlike piazzi.gauss_newton(), which solves in float64 on the CPU, a float32 x0 is solved in float32.

x0 is either a plain tensor, updated by addition, or a pypose.LieTensor, updated on the left by PyPose, X <- Exp(delta) X, so fun receives a LieTensor and the result x is one. This replaces manifold=: a piazzi.Manifold cannot be used.

PyPose has no stopping rules: its step takes one step and returns the loss. The wrapper runs up to max_iter steps 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 to max|x|; in the tangent space for a LieTensor) and the cost fell by less than step_tol times 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; eps is the machine epsilon of the dtype) and the cost rose by no more than rounding noise (8 * eps * cost), or if x moved by no more than 8 * 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 after reject rejected 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_fun and each iteration one n_system (the residuals and their Jacobian), plus one n_fun for x0 at 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 in n_fun. PyPose’s reverse-mode jacobian needs fun to be differentiable by torch.autograd.

Parameters:
  • fun (Callable) – fun(x) -> r, or (r, aux) with has_aux=True, where r is a tensor of residuals of any shape and x has the type and shape of x0. fun is called with a copy of the point (and with the parameter itself while PyPose differentiates, so it must not modify x in place).

  • x0 (torch.Tensor) – starting point, a floating-point tensor of any shape or a pypose.LieTensor.

Keyword Arguments:
  • method (str, optional) – "gn" or "lm". Default: "lm"

  • solver (str or Callable, optional) – the linear solver for the normal equations: "pinv", "lstsq", "cholesky" or "cg", or a PyPose solver such as pypose.optim.solver.PCG. Default: PyPose’s: "pinv" for "gn", "cholesky" for "lm"

  • strategy (str or Any, 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: None

  • strategy_options (dict, optional) – constructor arguments of the strategy chosen by name, for example {"damping": 1e-3}. Default: None

  • reject (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’s 16

  • vectorize (bool, optional) – whether PyPose vectorises its Jacobian computation. Default: True

  • has_aux (bool, optional) – whether fun also returns an auxiliary output (any tree of tensors), returned detached in result.aux and info.aux. Default: False

  • max_iter (int, optional) – maximum number of PyPose steps. Default: 100

  • step_tol (float, optional) – convergence tolerance on the size of an accepted step, relative to max|x| (absolute in the tangent space of a LieTensor). Default: 1e-5

  • callback (Callable, optional) – called after every iteration, accepted or not, with an piazzi.IterationInfo; returning True stops the run. Default: None

Returns:

OptimizeResult – x with the type, shape, dtype and device of x0, and fun the cost. n_fun_grad is always 0. raw is the PyPose optimiser.

See also

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)