t3_apply_corewise_hessian ========================= .. py:function:: t3toolbox.backend.apply.t3_apply_corewise_hessian(c, ww, core_pair, core_directions) .. code-block:: python def t3_apply_corewise_hessian( c: NDArray, # residual (omega^2 applied), shape=W+C ww: typ.Union[typ.Sequence[NDArray], NDArray], # apply vectors, len=d, elm_shape=W+(Ni,) core_pair: typ.Tuple[ typ.Sequence[NDArray], # tucker_cores of theta. len=d, elm_shape=C+(ni,Ni) typ.Sequence[NDArray], # tt_cores of theta. len=d, elm_shape=C+(ri,ni,r(i+1)) ], # = TuckerTensorTrain.data core_directions: typ.Tuple[ typ.Sequence[NDArray], # Tucker directions theta_hat. len=d, elm_shape=K+C+(ni,Ni) typ.Sequence[NDArray], # TT directions theta_hat. len=d, elm_shape=K+C+(ri,ni,r(i+1)) ], ) -> typ.Tuple[ typ.Tuple[NDArray, ...], # Tucker block of K theta_hat. len=d, elm_shape=K+C+(ni,Ni) typ.Tuple[NDArray, ...], # TT block of K theta_hat. len=d, elm_shape=K+C+(ri,ni,r(i+1)) ]: The derivative of :py:func:`t3_apply_corewise_transpose` along core directions: the core-coordinate Hessian of ``theta -> `` applied to ``theta_hat``, ``K_c theta_hat = d/ds grad_theta |_{s=0}``, for a fixed residual ``c``. The sampling half of the exact Riemannian Hessian's curvature term for ``apply``, by a hand-written "two-hole" sweep -- exact and symmetric. It differentiates the transpose's adjoint-state form (:py:func:`_apply_transpose_adjoint` at the corewise frame ``(B, G, G, G)``) by the product rule: the residual-seeded right context ``sigma_hat`` is swept through ``G`` (there is no ``nu`` or ``eta``), then its derivative and those of ``xi`` and ``mu`` -- the GN tangent forward's ``compute_dxi`` / ``compute_sigma`` and ``compute_tau`` -- feed the derivatives of the single-term assemblies. :py:func:`t3_entries_corewise_hessian` is the same with fiber-sliced ``xi`` and one-hot apply vectors. Stacks: ``c`` and ``core_pair`` carry ``W + C`` / ``C``; the directions carry a tangent stack ``K``; the output is raw core variations shaped like the directions, ``W`` summed. Polymorphic over ragged tuples and uniform supercores (packed ``ww``). **Mask-free by contract** (as :py:func:`~t3toolbox.backend.probing.t3_probe_corewise_hessian`): a uniform caller masks ``theta`` and ``theta_hat`` with ``theta``'s masks (:py:func:`~t3toolbox.backend.ut3_sampling.ut3_apply_corewise_hessian`). .. rubric:: Examples The directional derivative of the corewise transpose (a central difference is accurate to ``O(h^2)``), and symmetric: >>> import numpy as np >>> import t3toolbox.backend.t3_constructors as t3k >>> import t3toolbox.backend.apply as t3a >>> np.random.seed(0) # t3_corewise_randn draws from it >>> rng = np.random.default_rng(0) >>> shape, W = (4, 5, 3), 6 >>> theta = t3k.t3_corewise_randn(shape, (2, 3, 2), (1, 2, 2, 1)) >>> ww = [rng.standard_normal((W, N)) for N in shape] >>> c = rng.standard_normal(W) # one residual per apply >>> dirs = (tuple(rng.standard_normal((2,) + u.shape) for u in theta[0]), # a tangent stack K = (2,) ... tuple(rng.standard_normal((2,) + g.shape) for g in theta[1])) >>> KB, KG = t3a.t3_apply_corewise_hessian(c, ww, theta, dirs) >>> [k.shape for k in KB] # K + the Tucker core shapes [(2, 2, 4), (2, 3, 5), (2, 2, 3)] >>> one = lambda cores: t3a.t3_apply_corewise_transpose(c, ww, cores, sum_over_probes=True) >>> shift = lambda h: tuple(tuple(x + h * dx[0] for x, dx in zip(xs, dxs)) for xs, dxs in zip(theta, dirs)) >>> fd = [(a - b) / 2e-4 for a, b in zip(sum(one(shift(1e-4)), ()), sum(one(shift(-1e-4)), ()))] >>> bool(max(np.linalg.norm(k[0] - f) / np.linalg.norm(f) for k, f in zip(KB + KG, fd)) < 1e-6) True >>> gram = sum(a.reshape(2, -1) @ b.reshape(2, -1).T for a, b in zip(dirs[0] + dirs[1], KB + KG)) >>> bool(abs(gram[0, 1] - gram[1, 0]) < 1e-12 * abs(gram).max()) # = True