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,callbackandoptions), so you can swap backends to compare them;methodand the keys ofoptionsare torchmin’s. Everything stays in PyTorch:funand torchmin’s solver run on the device and in the dtype ofx0. 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)withhas_aux=True, wherexhas the shape ofx0andlossis a scalar tensor.x0 (
torch.Tensor) – starting point, of any shape.method (
str, optional) – one ofpiazzi.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) – whetherfunalso returns an auxiliary output (any tree of tensors), returned detached inresult.auxandinfo.aux. Default:Falsemanifold (
piazzi.Manifold, optional) – not supported, since torchmin has no retraction: giving one raisesValueError. Usepiazzi.lbfgs()for manifolds. Default:Nonecallback (
Callable, optional) – called after every iteration with anpiazzi.IterationInfo(tensors, with the evaluation counts so far), as for the native solvers; returningTruestops the run.info.acceptedisFalsewhen the iterate did not move (a rejected trust-region step), and theninfo.xis the previous iterate. Default:Noneoptions (
dict, optional) – torchmin’s method-specific options (max_iter,gtol,xtol,history_size,line_search, …).callbackandreturn_allare not accepted; torchmin’s owndispis. Default:None
- Returns:
OptimizeResult –
xwith the shape, dtype and device ofx0,funthe value andauxthe auxiliary output there.n_iteris torchmin’snit. Every evaluation offunin torchmin is a value-and-gradient one (its line searches included), son_fun_gradcounts the calls tofun,n_funis0andn_systemis0: 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 offun, and are not counted. The trust-region methods compute the gradient and the Hessian at every trial point, rejected ones included.convergedis torchmin’ssuccesswith two more checks: the final value is finite, and the max-norm of the gradient there is at mostoptions["gtol"](torchmin’s default for the method if absent:1e-4for the trust-region methods,1e-5otherwise) or1e-3times the max-norm of the gradient atx0, whichever is larger. This matters forbfgs,l-bfgs,newton-cgandnewton-exact, which also report success when a step or a decrease falls belowxtol; such a stall at a large gradient is not converged, andmessagesays so.messageis otherwise torchmin’s, andrawis torchmin’s own result. Those methods stop onxtolbefore moving to the last trial point, soxis then the previous iterate, and the lastinfo.xthe 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),funis evaluated there once more and the counts include it.
See also
piazzi.minimize(): the native interface with the same calling convention.piazzi.scipy.minimize(): the same problem with SciPy’s methods.
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".