Problem.local_model#

t3toolbox.backend.optimizers.Problem.local_model(x_cores, sample=None, data=None, hessian='gauss_newton', curvature_damping=None, frame=None)#
def local_model(
        self,
        x_cores:            Cores,                   # the point to linearize at
        sample:             typ.Any = None,          # an explicit minibatch sample (with data), or None
        data:               typ.Any = None,          # its observed data (with sample), or None
        hessian:            str = 'gauss_newton',    # or 'exact' (case-insensitive): the model's Hessian
        curvature_damping:  typ.Optional[float] = None,  # exact only: the lift damping; None -> the geometry's
        frame:              typ.Any = None,          # a frame of x_cores to linearize at (checked by geom.require_frame_structure); None -> geom.frame(x_cores)
) -> LocalModel:

Linearize at x_cores on the full data (sample=None) or an explicit minibatch (sample AND data – passing one without the other is a structural error).

hessian='exact' builds the exact-mode model (LocalModel): it checks, structurally and before any work, that the kind implements corewise_hessian and the geometry the four exact-mode members (see Geometry), then does the model’s ONE misfit transpose and the geometry’s per-frame curvature data. curvature_damping (relative to ‖X‖_HS) is handed to the geometry unread; None means the geometry’s own default, and a value with hessian='gauss_newton' – where nothing would read it – raises.

frame linearizes at a given frame of x_cores (a padded start’s aligned frame) instead of geom.frame(x_cores). Its structure is checked by the geometry’s require_frame_structure (integers and held masks only); a geometry without that member raises.

Examples

The exact model’s quadratic form is the objective’s second derivative along the retraction (the T3-SVD retraction is second order on a minimal, numerically full-rank frame, as here), where the Gauss-Newton one misses the curvature term:

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> import t3toolbox.corewise as cw
>>> import t3toolbox.backend.fitting as bfit
>>> import t3toolbox.backend.geometry as bgeo
>>> import t3toolbox.backend.optimizers as bopt
>>> import t3toolbox.backend.sampling_derivatives as sd
>>> np.random.seed(0)
>>> shape, ranks = (4, 5, 6), ((2, 2, 2), (1, 2, 2, 1))
>>> ww = [np.random.randn(20, N) for N in shape]
>>> pp = [np.random.randn(20, N) for N in shape]
>>> data = sd.t3_probe_derivatives(ww, pp, t3.TuckerTensorTrain.randn(shape, *ranks).data, 1)
>>> problem = bopt.least_squares_problem(bgeo.ManifoldGeometryOps(), bfit.ProbeDerivativesKind(order=1),
...                                      (ww, pp), data)
>>> x = t3.TuckerTensorTrain.randn(shape, *ranks).data
>>> gn, exact = problem.local_model(x), problem.local_model(x, hessian='exact')
>>> gn.is_exact, exact.is_exact
(False, True)
>>> g = exact.gradient                                         # the Gauss-Newton gradient, bit for bit
>>> p = cw.corewise_scale(g, 1.0 / float(problem.geom.inner(g, g)) ** 0.5)
>>> f = lambda t: float(problem.objective(exact.retract(cw.corewise_scale(p, t))))
>>> fd = (f(1e-3) - 2 * f(0.0) + f(-1e-3)) / 1e-6
>>> bool(abs(float(exact.quadratic(p)) - fd) < 1e-4 * abs(fd))
True
>>> bool(abs(float(gn.quadratic(p)) - fd) < 1e-2 * abs(fd))
False
Parameters:
  • x_cores (Cores)

  • sample (t3toolbox.backend.common.typ.Any)

  • data (t3toolbox.backend.common.typ.Any)

  • hessian (str)

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

  • frame (t3toolbox.backend.common.typ.Any)

Return type:

LocalModel