pytorch-minimize backend

A wrapper that runs the solvers of pytorch-minimize (torchmin) on PyTorch functions, with the calling convention of piazzi.minimize(), so you can compare backends on the same problem. Requires the piazzi[torchmin] extra; import piazzi does not load it, and importing this module registers "torchmin-<method>" in piazzi.MINIMIZE_METHODS. See the backends guide.

piazzi.torchmin.minimize(fun, x0, method='l-bfgs', *, has_aux=False, manifold=None, callback=None, options=None)[source]

Run torchmin.minimize (pytorch-minimize) on a scalar function written in PyTorch.

It has the calling convention of piazzi.minimize() (fun, x0, method, has_aux, callback and options), so you can swap backends to compare them; method and the keys of options are torchmin’s. Everything stays in PyTorch: fun and torchmin’s solver run on the device and in the dtype of x0. Gradients, Hessians and Hessian-vector products come from autograd, so the Newton-type and trust-region methods need an objective that is twice differentiable.

Parameters:
  • fun (Callable) – fun(x) -> loss, or (loss, aux) with has_aux=True, where x has the shape of x0 and loss is a scalar tensor.

  • x0 (torch.Tensor) – starting point, of any shape.

  • method (str, optional) – one of piazzi.torchmin.METHODS: "bfgs", "l-bfgs", "cg", "newton-cg", "newton-exact", "dogleg", "trust-ncg", "trust-exact" or "trust-krylov" (case insensitive). Default: "l-bfgs"

Keyword Arguments:
  • 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

  • manifold (piazzi.Manifold, optional) – not supported, since torchmin has no retraction: giving one raises ValueError. Use piazzi.lbfgs() for manifolds. Default: None

  • callback (Callable, optional) – called after every iteration with an piazzi.IterationInfo (tensors, with the evaluation counts so far), as for the native solvers; returning True stops the run. info.accepted is False when the iterate did not move (a rejected trust-region step), and then info.x is the previous iterate. Default: None

  • options (dict, optional) – torchmin’s method-specific options (max_iter, gtol, xtol, history_size, line_search, …). callback and return_all are not accepted; torchmin’s own disp is. Default: None

Returns:

OptimizeResult – x with the shape, dtype and device of x0, fun the value and aux the auxiliary output there. n_iter is torchmin’s nit. Every evaluation of fun in torchmin is a value-and-gradient one (its line searches included), so n_fun_grad counts the calls to fun, n_fun is 0 and n_system is 0: the Hessian and Hessian-vector products of the Newton-type and trust-region methods are autograd backward passes through the graph kept from the evaluation, not evaluations of fun, and are not counted. The trust-region methods compute the gradient and the Hessian at every trial point, rejected ones included. converged is torchmin’s success with two more checks: the final value is finite, and the max-norm of the gradient there is at most options["gtol"] (torchmin’s default for the method if absent: 1e-4 for the trust-region methods, 1e-5 otherwise) or 1e-3 times the max-norm of the gradient at x0, whichever is larger. This matters for bfgs, l-bfgs, newton-cg and newton-exact, which also report success when a step or a decrease falls below xtol; such a stall at a large gradient is not converged, and message says so. message is otherwise torchmin’s, and raw is torchmin’s own result. Those methods stop on xtol before moving to the last trial point, so x is then the previous iterate, and the last info.x the callback saw can differ from it. If the final point was not among the last evaluations (this can only happen when a callback stops the run), fun is evaluated there once more and the counts include it.

See also

Example:

>>> def rosenbrock(x):
...     return (100 * (x[1:] - x[:-1] ** 2) ** 2 + (1 - x[:-1]) ** 2).sum()
>>> x0 = torch.tensor([-1.2, 1.0], dtype=torch.float64)
>>> res = piazzi.torchmin.minimize(rosenbrock, x0, "trust-exact")
>>> res.x
tensor([1.0000, 1.0000], dtype=torch.float64)
piazzi.torchmin.METHODS

The method names of torchmin.minimize: "bfgs", "l-bfgs", "cg", "newton-cg", "newton-exact", "dogleg", "trust-ncg", "trust-exact" and "trust-krylov".