LocalModel ========== .. toctree:: :hidden: /autoapi/t3toolbox/fitting/LocalModel.__post_init__ /autoapi/t3toolbox/fitting/LocalModel.objective_value /autoapi/t3toolbox/fitting/LocalModel.gradient /autoapi/t3toolbox/fitting/LocalModel.jacobian /autoapi/t3toolbox/fitting/LocalModel.gn_quadratic /autoapi/t3toolbox/fitting/LocalModel.gn_hessian /autoapi/t3toolbox/fitting/LocalModel.hessian /autoapi/t3toolbox/fitting/LocalModel.quadratic /autoapi/t3toolbox/fitting/LocalModel.curvature_hessian /autoapi/t3toolbox/fitting/LocalModel.curvature_quadratic /autoapi/t3toolbox/fitting/LocalModel.evaluate /autoapi/t3toolbox/fitting/LocalModel.retract .. py:class:: t3toolbox.fitting.LocalModel The local model of a least-squares objective at ``frame``, generic over the geometry: the objective value, the gradient, and the curvature actions there (the Gauss-Newton ``H = JᵀJ``: :py:meth:`gn_hessian` / :py:meth:`gn_quadratic`). The frontend twin of the backend :py:class:`~t3toolbox.backend.optimizers.LocalModel`, with ``T3Tangent`` / ``UT3Tangent`` in and out. Built by a sampling-kind factory (:py:func:`apply_model` / :py:func:`entries_model` / :py:func:`probe_model`), not directly. Holds the geometry, the frame ``frame = geometry.frame(X)``, the sampling ``kind``, the measurement ``sample`` (the probe/apply vectors ``ww`` or the integer grid ``index``), and the data residual ``r = S(X) − y``. The frame sweep, objective value, and gradient are cached on first use. **Two modes**, chosen by the factory's ``hessian`` argument and derived rather than stored (:py:attr:`is_exact` is ``curvature is not None``): * **Gauss-Newton** (``hessian='gauss_newton'``, the default): :py:meth:`hessian` / :py:meth:`quadratic` are :py:meth:`gn_hessian` / :py:meth:`gn_quadratic`. * **Exact** (``hessian='exact'``): :py:meth:`hessian` is the exact Riemannian Hessian -- the Gauss-Newton term plus the curvature term :py:meth:`curvature_hessian` (``Π Lᵀ(K_{Aᵀω²r} − K_{grad f}) L Π``; see :py:meth:`t3toolbox.backend.optimizers.Problem.local_model`), and :py:meth:`quadratic` likewise. The model's one misfit transpose is done at construction (``curvature``), so :py:attr:`gradient` is the Gauss-Newton gradient bit for bit. Every sampling kind implements it, ragged and uniform. On a shared manifold geometry, ragged or uniform, the curvature goes through the tied lift (one factor direction per sharing group). ``gradient``, ``jacobian``, ``gn_hessian``, ``gn_quadratic``, ``objective_value`` and :py:meth:`retract` mean the same in both modes. **One class, both representations.** The factories dispatch on ``x``: a ragged ``TuckerTensorTrain`` gives a model on a ``T3Frame``, whose gradient and Hessian actions are of type ``T3Tangent``; a ``UniformTuckerTensorTrain`` gives one on a ``UT3Frame``, running on the packed supercores with ``UT3Tangent`` at the boundary. Everything between -- the objective, the gradient, the Gauss-Newton actions, the regularizer folding -- is the same code. .. rubric:: Examples >>> import numpy as np >>> import t3toolbox.tucker_tensor_train as t3 >>> import t3toolbox.manifold as t3m >>> import t3toolbox.fitting as fitting >>> np.random.seed(0) >>> x = t3.TuckerTensorTrain.randn((6, 7, 8), (2, 3, 2), (1, 2, 2, 1)) >>> ww = [np.random.randn(15, N) for N in (6, 7, 8)] # 15 samples, each a (w_1, w_2, w_3) >>> r = np.random.randn(15) # the data residual at x On the **manifold** geometry the gradient ``g = Π 𝒥ᵀ r`` is a gauged tangent and ``H p`` stays on the gauged tangent space: >>> model = fitting.apply_model(t3m.MANIFOLD, x, ww, r) >>> print(round(float(model.objective_value), 6)) 8.295196 >>> print(model.gradient.is_gauged()) True >>> p = t3m.MANIFOLD.randn(model.frame) # a gauged trial step at the model's frame >>> print(model.gn_hessian(p).is_gauged()) True ``evaluate(p)`` is the quadratic-model value, consistent with assembling it from the cached ``c`` / ``g`` and the Hessian action (and costs only one forward apply, not a Hessian apply): >>> hp = model.gn_hessian(p) >>> m_built = float(model.objective_value + model.gradient.corewise_inner(p) + 0.5 * p.corewise_inner(hp)) >>> bool(np.allclose(float(model.evaluate(p)), m_built)) True The Gauss-Newton quadratic form ``pᵀHp = ‖Jp‖²`` comes from one forward sweep (no ``H p`` assembly) -- the cheap step-length denominator for Cauchy / line search (``alpha = g.corewise_inner(g) / gn_quadratic(g)``): >>> bool(np.allclose(float(model.gn_quadratic(p)), float(p.corewise_inner(model.gn_hessian(p))))) True The **corewise** geometry is the same call with a different geometry: the gradient is a tangent at the raw ``(B,G,G,G)`` frame (a core perturbation), with **no** gauge projection: >>> cmodel = fitting.apply_model(t3m.COREWISE, x, ww, r) >>> print(cmodel.gradient.is_gauged()) False >>> cp = t3m.COREWISE.randn(cmodel.frame) >>> bool(np.allclose(float(cmodel.evaluate(cp)), ... float(cmodel.objective_value + cmodel.gradient.corewise_inner(cp) ... + 0.5 * cp.corewise_inner(cmodel.gn_hessian(cp))))) True The **uniform** layer is the same call with a uniform ``x`` (and a uniform geometry): the gradient comes back as a gauged ``UT3Tangent``, and ``pᵀHp = ‖J p‖²`` agrees with the Hessian action there too: >>> import t3toolbox.uniform_tucker_tensor_train as ut3 >>> import t3toolbox.uniform_manifold as ut3m >>> ux = ut3.UniformTuckerTensorTrain.from_t3(x) >>> umodel = fitting.apply_model(ut3m.UNIFORM_MANIFOLD, ux, ww, r) >>> type(umodel).__name__, type(umodel.gradient).__name__ ('LocalModel', 'UT3Tangent') >>> print(bool(umodel.gradient.is_gauged().all())) True >>> up = ut3m.UNIFORM_MANIFOLD.randn(umodel.frame) >>> bool(np.allclose(float(umodel.gn_quadratic(up)), float(up.corewise_inner(umodel.gn_hessian(up))))) True A trial step at a *different* frame is a structural error (the model is tied to its frame): >>> other = t3.TuckerTensorTrain.randn((6, 7, 8), (2, 3, 2), (1, 2, 2, 1)) >>> model.gn_hessian(t3m.MANIFOLD.randn(t3m.MANIFOLD.frame(other))) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ValueError: trial tangent must live at the model's frame (it is at a different frame) .. py:attribute:: geometry :type: t3toolbox.backend.common.typ.Any .. py:attribute:: frame :type: t3toolbox.backend.common.typ.Any .. py:attribute:: kind :type: t3toolbox.backend.fitting.SamplingKind .. py:attribute:: sample :type: t3toolbox.backend.common.typ.Any .. py:attribute:: residual :type: t3toolbox.backend.common.typ.Any .. py:attribute:: sweep :type: t3toolbox.backend.common.typ.Any .. py:attribute:: regularizer :type: t3toolbox.backend.common.typ.Any :value: None .. py:attribute:: geometry_aux :type: t3toolbox.backend.common.typ.Any :value: None .. py:attribute:: curvature :type: t3toolbox.backend.common.typ.Any :value: None .. py:property:: is_exact :type: bool Whether this model's Hessian is the exact one (built with ``hessian='exact'``) rather than Gauss-Newton. Derived, never stored: ``curvature is not None``. Methods ------- .. autoapisummary:: t3toolbox.fitting.LocalModel.__post_init__ t3toolbox.fitting.LocalModel.objective_value t3toolbox.fitting.LocalModel.gradient t3toolbox.fitting.LocalModel.jacobian t3toolbox.fitting.LocalModel.gn_quadratic t3toolbox.fitting.LocalModel.gn_hessian t3toolbox.fitting.LocalModel.hessian t3toolbox.fitting.LocalModel.quadratic t3toolbox.fitting.LocalModel.curvature_hessian t3toolbox.fitting.LocalModel.curvature_quadratic t3toolbox.fitting.LocalModel.evaluate t3toolbox.fitting.LocalModel.retract