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