format_newton_iter#

t3toolbox.backend.optimizer_display.format_newton_iter(info, train_err, val_err=None, obj_unweighted=None, fmt='%.1e')#
def format_newton_iter(
        info:           "bopt.NewtonInfo",       # the per-iteration diagnostics from newton_cg
        train_err:      NDArray,                  # (n_mode, n_order) training relative-error matrix
        val_err:        typ.Optional[NDArray] = None,   # (n_mode, n_order) validation matrix, or None
        obj_unweighted: typ.Optional[float]   = None,   # ½‖r‖²; shown next to the misfit iff a nontrivial ω makes it differ
        fmt:            str = '%.1e',
) -> str:                                         # the formatted header line + relative-error table

Format one Newton iteration as a header line + the relative-error table (pure – returns a string). The layout of the table follows train_err’s shape (Decision 2a). When a regularizer is attached the header splits the objective as obj = misfit + reg (info.misfit / info.regularization); obj_unweighted (the unweighted ½‖r‖²) is shown next to the misfit term only when a nontrivial residual weight ω makes it differ (unregularized, it annotates the objective, as before).

Examples

A converged final iteration (short header) with a 2-mode x 2-order error matrix; note 1.0e+00 and 5.2e-04 line up – %.1e is fixed 7-char width, so columns align with no padding logic:

>>> import numpy as np
>>> import t3toolbox.backend.optimizers as bopt
>>> import t3toolbox.backend.optimizer_display as disp
>>> info = bopt.NewtonInfo(iteration=5, objective=3.78e-4, gnorm=4.8e-6, g0norm=6.5e-2, converged=True)
>>> train = np.array([[1.9e-2, 1.3e-2], [1.0e+00, 5.2e-4]])   # (2 modes, 2 orders)
>>> print(disp.format_newton_iter(info, train))
iter  5 | obj 3.780e-04  ‖g‖ 4.80e-06 (7.4e-05·g₀) | converged (‖g‖ ≤ gtol·‖g₀‖)
  rel err  rows=mode  cols=order
           ord0     ord1
    m0  1.9e-02  1.3e-02
    m1  1.0e+00  5.2e-04

With a regularizer attached, the objective splits as obj = misfit + reg (here a single apply dataset, so a 1x1 error table):

>>> info = bopt.NewtonInfo(iteration=3, objective=6.003e1, gnorm=1.60, g0norm=1.07e1, converged=False,
...                        misfit=5.974e1, regularization=2.9e-1, cg_iters=8, cg_maxiter=200,
...                        cg_tol=6.2e-1, cg_resid=4.9e-1, cg_converged=True, cg_truncated=False,
...                        ls_steps=0, alpha=1.0, step_rel=1.2, delta_f=-2.22, rho=0.07, wall_time=0.03)
>>> print(disp.format_newton_iter(info, np.array([[3.7e-1]])))
iter  3 | obj 6.003e+01 = misfit 5.974e+01 + reg 2.900e-01  ‖g‖ 1.60e+00 (1.5e-01·g₀) | CG 8/200 tol 6.2e-01 resid 4.9e-01 ✓ | ls 0 α 1.00e+00 ‖Δx‖/‖x‖ 1.2e+00 | Δf -2.22e+00 ρ 0.07 | 0.03s
  rel err   3.7e-01

A row from newton_cg carries its reference kind (NewtonInfo.g0_reference), and the converged line names it – here the default zero-tensor reference:

>>> info = bopt.NewtonInfo(iteration=7, objective=1.2e-9, gnorm=3.1e-7, g0norm=4.4e1, converged=True,
...                        g0_reference='zero')
>>> print(disp.format_newton_iter(info, np.array([[2.0e-5]])))
iter  7 | obj 1.200e-09  ‖g‖ 3.10e-07 (7.0e-09·g₀) | converged (‖g‖ ≤ gtol·‖g₀‖, g₀ = zero tensor)
  rel err   2.0e-05

A trust-region row of the exact Hessian (hessian='exact'): CG stopped on the region’s boundary (∂), the trial was rejected (✗; the next row keeps x and a quarter of the step length as Δ), and the exact cells give κ (the curvature term’s share of pᵀHp, relative to the Gauss-Newton part), the smallest lift singular value σ and the lift damping ε:

>>> info = bopt.NewtonInfo(iteration=4, objective=2.31e-3, gnorm=7.2e-4, g0norm=5.1e1, converged=False,
...                        cg_iters=6, cg_maxiter=200, cg_tol=1.9e-5, cg_resid=3.3e-5, cg_converged=False,
...                        cg_truncated=False, cg_boundary=True, alpha=1.0, step_rel=4.0e-2, delta_f=6.1e-5,
...                        rho=-0.52, wall_time=0.12, step_control='trust_region', hessian='exact',
...                        tr_radius=3.1e-2, tr_accepted=False, pHp=8.0e-4, pKp=-2.0e-4,
...                        lift_sigma_min=4.2e-1, lift_eps=1.3e-5)
>>> print(disp.format_newton_iter(info, np.array([[2.0e-3]])))
iter  4 | obj 2.310e-03  ‖g‖ 7.20e-04 (1.4e-05·g₀) | CG 6/200 tol 1.9e-05 resid 3.3e-05 ∂ | TR Δ 3.10e-02 ✗ ‖Δx‖/‖x‖ 4.0e-02 | Δf +6.10e-05 ρ -0.52 | 0.12s | κ -2.0e-01 σ 4.2e-01 ε 1.3e-05
  rel err   2.0e-03

A Gauss-Newton-phase row of hessian='switch': the sw segment gives the tracking ratio e_N / e_GN, how far the exact model’s predicted change missed the actual one relative to Gauss-Newton’s miss (— on a row that ran no tracking product), then × the count of consecutive hits (e_N < switch_tau·e_GN on a countable step) after this row. Here the count reaches switch_k = 2, so the row ends with ⇒ exact: from the next row on the model is the exact Hessian, and the rows show its κ σ ε cells instead:

>>> info = bopt.NewtonInfo(iteration=1, objective=2.537e5, gnorm=4.17e2, g0norm=9.84e2, converged=False,
...                        cg_iters=1, cg_maxiter=200, cg_tol=2.1e2, cg_resid=1.2e2, cg_converged=True,
...                        cg_truncated=False, cg_boundary=False, alpha=1.0, step_rel=8.7e-3, delta_f=-2.03e2,
...                        rho=0.85, wall_time=0.03, step_control='trust_region', hessian='gauss_newton',
...                        tr_radius=2.51, tr_accepted=True, pHp=4.1e2, switch_pKp=7.43e1, switch_e_gn=3.70e1,
...                        switch_e_n=1.1e-1, switch_count=2, switch_fired=True)
>>> print(disp.format_newton_iter(info, np.array([[2.8e-1]])))
iter  1 | obj 2.537e+05  ‖g‖ 4.17e+02 (4.2e-01·g₀) | CG 1/200 tol 2.1e+02 resid 1.2e+02 ✓ | TR Δ 2.51e+00 ✓ ‖Δx‖/‖x‖ 8.7e-03 | Δf -2.03e+02 ρ 0.85 | 0.03s | sw 3.0e-03 ×2 ⇒ exact
  rel err   2.8e-01

A row of a timed run (newton_cg(timing=True), its NewtonInfo.timing) adds a time line under the header: the row’s window by phase, in seconds (PhaseSeconds). model is the local model’s build with its parts in brackets, cg also gives its seconds per CG iteration, trials the retractions and objectives tried (×1), cb the previous row’s callback and rest what no phase covers; a phase that did not run on the row (here ref, row 0’s) is left out:

>>> seconds = bopt.PhaseSeconds(previous_callback=2.4e-4, frame=4.6e-4, sweep=9.1e-4, companion=3.4e-6,
...                             residual=9.8e-4, curvature=1.1e-3, gradient=2.7e-3, reference=None, cg=7.3e-2,
...                             forms=1.2e-2, trial_retraction=7.8e-4, trial_objective=1.0e-3, tracking=None,
...                             remainder=1.6e-4)
>>> timing = bopt.NewtonTiming(window=9.3e-2, seconds=seconds, compile_seconds=None, trials=1,
...                            accepted_retraction=7.8e-4)
>>> info = bopt.NewtonInfo(iteration=2, objective=4.62e2, gnorm=5.23e2, g0norm=5.6e4, converged=False,
...                        cg_iters=5, cg_maxiter=200, cg_tol=4.8, cg_resid=2.5, cg_converged=True,
...                        cg_truncated=False, cg_boundary=False, alpha=1.0, step_rel=1.3e-2, delta_f=-4.62e2,
...                        rho=1.01, wall_time=9.3e-2, step_control='trust_region', hessian='exact',
...                        tr_radius=6.92e1, tr_accepted=True, pHp=9.2e2, pKp=1.1e1, lift_sigma_min=1.0e1,
...                        lift_eps=1.4e-4, timing=timing)
>>> print(disp.format_newton_iter(info, np.array([[1.2e-2]])))
iter  2 | obj 4.620e+02  ‖g‖ 5.23e+02 (9.3e-03·g₀) | CG 5/200 tol 4.8e+00 resid 2.5e+00 ✓ | TR Δ 6.92e+01 ✓ ‖Δx‖/‖x‖ 1.3e-02 | Δf -4.62e+02 ρ 1.01 | 0.09s | κ +1.2e-02 σ 1.0e+01 ε 1.4e-04
  time  model 3.5e-03 [frame 4.6e-04 sweep 9.1e-04 comp 3.4e-06 resid 9.8e-04 curv 1.1e-03] | grad 2.7e-03 | cg 7.3e-02 (1.5e-02/it) | forms 1.2e-02 | trials 1.8e-03 ×1 [retr 7.8e-04 obj 1.0e-03] | cb 2.4e-04 | rest 1.6e-04
  rel err   1.2e-02
Parameters:
  • info (NewtonInfo)

  • train_err (NDArray)

  • val_err (t3toolbox.backend.common.typ.Optional[NDArray])

  • obj_unweighted (t3toolbox.backend.common.typ.Optional[float])

  • fmt (str)

Return type:

str