Results and callbacks¶
Every solver and wrapper returns an OptimizeResult, and passes an
IterationInfo to callback after every iteration. print_progress() is a
ready-made callback that prints them.
- class piazzi.OptimizeResult(x, fun, aux, n_iter, n_fun_grad=0, n_fun=0, n_system=0, converged=False, message='', raw=None)[source]¶
Bases:
objectOutcome of a solver run, returned by every solver and wrapper.
- x¶
the solution, with the shape, dtype and device of
x0.- Type:
- fun¶
the objective value at
x(for least squares, the cost, not the residuals).- Type:
- n_system¶
evaluations of second-order information: the normal equations of Gauss-Newton / Levenberg-Marquardt, or SciPy’s Hessian or Hessian-vector products.
- Type:
- converged¶
whether the run stopped at a minimiser to working precision: on a step or decrease below tolerance (
step_tol, rounding noise, or an optimiser’s own tolerances), at a zero gradient, or with no free parameters.- Type:
- raw¶
the backend’s own result object for third-party wrappers (
piazzi.scipy), or the optimiser fortorch.optim.Optimizermethods;Nonefor the native solvers.- Type:
- class piazzi.IterationInfo(iteration, x, fun, fun_start, aux, accepted, n_fun_grad=0, n_fun=0, n_system=0)[source]¶
Bases:
objectState of a solver run, passed to
callbackafter every iteration, accepted or not.Use the evaluation counts to plot progress against cost rather than iterations, which hide the evaluations a line search spends.
- x¶
the current iterate, shaped like
x0: the new point after an accepted step, the unchanged point after a rejected one.- Type:
- fun¶
the objective value at
x(a scalar tensor).- Type:
- fun_start¶
the objective value at the start of the iteration.
- Type:
- n_system¶
evaluations of second-order information so far (see
piazzi.OptimizeResult).- Type:
- piazzi.print_progress(every=1, file=None)[source]¶
Make a
callbackthat prints a table of the solver’s progress, one row per iteration.The columns are the iteration, the objective value after it, its relative decrease over the iteration, whether the step was accepted or rejected, and the evaluations so far by kind (see
piazzi.IterationInfo), in the spirit of Ceres Solver’s per-iteration summary. A header is printed before the first iteration of each run, so the same callback can be reused. It never stops the run. Every solver and wrapper also takesverbose=True(orverbose=k), which runs this printer before itscallback; callprint_progressyourself to print to anotherfile.- Parameters:
every (
int, optional) – print everyevery-th iteration. Default:1file (
TextIO, optional) – where to print. Default:None, which prints tosys.stdout(looked up at each call).
- Returns:
Callable –
callback(info)for any solver or wrapper’scallbackargument.
Example:
>>> t = torch.linspace(0, 1, 20, dtype=torch.float64) >>> y = 2 * torch.exp(-3 * t) >>> res = piazzi.least_squares( ... lambda p: p[0] * torch.exp(-p[1] * t) - y, ... torch.tensor([1.0, 1.0], dtype=torch.float64), ... callback=piazzi.print_progress(every=2), ... ) iter fun rel_decrease step n_fun_grad n_fun n_system 0 3.876612e-01 7.208e-01 accepted 0 0 2 2 4.938297e-10 1.000e+00 accepted 0 0 4...