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: object

Outcome of a solver run, returned by every solver and wrapper.

x

the solution, with the shape, dtype and device of x0.

Type:

torch.Tensor

fun

the objective value at x (for least squares, the cost, not the residuals).

Type:

torch.Tensor

aux

the auxiliary output at x with has_aux=True, detached, else None.

Type:

Any

n_iter

number of iterations.

Type:

int

n_fun_grad

value-and-gradient evaluations.

Type:

int

n_fun

value-only evaluations.

Type:

int

n_system

evaluations of second-order information: the normal equations of Gauss-Newton / Levenberg-Marquardt, or SciPy’s Hessian or Hessian-vector products.

Type:

int

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:

bool

message

why the run stopped.

Type:

str

raw

the backend’s own result object for third-party wrappers (piazzi.scipy), or the optimiser for torch.optim.Optimizer methods; None for the native solvers.

Type:

Any

class piazzi.IterationInfo(iteration, x, fun, fun_start, aux, accepted, n_fun_grad=0, n_fun=0, n_system=0)[source]

Bases: object

State of a solver run, passed to callback after 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.

iteration

index of the iteration, from 0.

Type:

int

x

the current iterate, shaped like x0: the new point after an accepted step, the unchanged point after a rejected one.

Type:

torch.Tensor

fun

the objective value at x (a scalar tensor).

Type:

torch.Tensor

fun_start

the objective value at the start of the iteration.

Type:

torch.Tensor

aux

the auxiliary output at x with has_aux=True, detached, else None.

Type:

Any

accepted

whether the iteration’s step was accepted.

Type:

bool

n_fun_grad

value-and-gradient evaluations so far, including this iteration’s.

Type:

int

n_fun

value-only evaluations so far.

Type:

int

n_system

evaluations of second-order information so far (see piazzi.OptimizeResult).

Type:

int

piazzi.print_progress(every=1, file=None)[source]

Make a callback that 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 takes verbose=True (or verbose=k), which runs this printer before its callback; call print_progress yourself to print to another file.

Parameters:
  • every (int, optional) – print every every-th iteration. Default: 1

  • file (TextIO, optional) – where to print. Default: None, which prints to sys.stdout (looked up at each call).

Returns:

Callable – callback(info) for any solver or wrapper’s callback argument.

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...