t3_probe_corewise_derivatives_hessian#

t3toolbox.backend.sampling_derivatives.t3_probe_corewise_derivatives_hessian(ztildes, ww, pp, core_pair, core_directions, order, chunk_size=DEFAULT_CHUNK_SIZE)#
def t3_probe_corewise_derivatives_hessian(
        ztildes:            typ.Sequence[NDArray],  # residual jets (omega^2 applied), len=d, elm_shape=(order+1,)+W+C+(Ni,)
        ww:                 typ.Sequence[NDArray],  # probe vectors X,        len=d, elm_shape=W+(Ni,)
        pp:                 typ.Sequence[NDArray],  # perturbation vectors P, 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))
        ],
        order:              int,                    # highest derivative order
        chunk_size:         typ.Optional[int] = DEFAULT_CHUNK_SIZE,  # W-chunk size of the assemblies; None -> dense
) -> 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 t3_probe_corewise_derivatives_transpose() along core directions: the core-coordinate Hessian of theta -> <z~, S(Phi(theta))> applied to theta_hat,

K_z theta_hat = d/ds grad_theta <z~, S(Phi(theta + s theta_hat))> |_{s=0},

for S the probe-derivative jets (orders 0..order) and fixed residual jets z~. This is the sampling half of the exact Riemannian Hessian’s curvature term (with z~ = omega^2 r, the weighted residual the Gauss-Newton transpose receives), by a hand-written “two-hole” sweep – two cores replaced at once – rather than automatic differentiation; it is exact (no finite differences) and symmetric.

Stacks: the residual jets and core_pair carry W + C / C; the directions carry a tangent stack K (K + C), and the output is raw core variations shaped like the directions, the sample stack W summed. Polymorphic over ragged tuples and uniform supercores (packed ztildes/ww/ pp). chunk_size W-chunks the assemblies on the uniform path (bounding their peak memory on jax) exactly as in tv_probe_derivatives_transpose() (Memory chunking of the derivative transpose (chunk_size)).

Mask-free by contract. It assumes theta and theta_hat arrive with their padded slots already zero: a uniform caller masks both once up front with theta’s masks, so the shared algorithm runs mask-free. A second derivative multiplies a direction’s padded slot into real slots, so an unmasked theta_hat corrupts the real block; masking is the uniform wrapper’s job.

The pieces: compute_deta_tilde_tangent_jets(), compute_tau_tilde_tangent_jets(), compute_sigma_tilde_tangent_jets(), compute_dxi_tilde_tangent_jets(), assemble_tucker_tangent_variation_jets(), assemble_tt_tangent_variation_jets(); the precomputed-sweep variant is t3_probe_corewise_derivatives_hessian_from_sweep().

Examples

The two-hole is the directional derivative of the corewise transpose (a polynomial in s, so a central difference is accurate to O(h^2)), and a symmetric operator:

>>> import numpy as np
>>> import t3toolbox.backend.t3_constructors as t3k
>>> import t3toolbox.backend.sampling_derivatives as sd
>>> np.random.seed(0)                                                  # t3_corewise_randn draws from it
>>> rng = np.random.default_rng(0)
>>> shape, order, W = (4, 5, 3), 2, 6
>>> theta = t3k.t3_corewise_randn(shape, (2, 3, 2), (1, 2, 2, 1))
>>> ww = [rng.standard_normal((W, N)) for N in shape]
>>> pp = [rng.standard_normal((W, N)) for N in shape]
>>> zt = [rng.standard_normal((order + 1, W, N)) for N in shape]       # residual jets, (order+1)+W+(Ni,)
>>> 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 = sd.t3_probe_corewise_derivatives_hessian(zt, ww, pp, theta, dirs, order)
>>> [k.shape for k in KG]                                              # K + the TT core shapes
[(2, 1, 2, 2), (2, 2, 3, 2), (2, 2, 2, 1)]
>>> one = lambda cores: sd.t3_probe_corewise_derivatives_transpose(zt, ww, pp, cores, order,
...                                                                sum_over_probes=True)
>>> shift = lambda h: tuple(tuple(c + h * dc[0] for c, dc in zip(cs, dcs)) for cs, dcs in zip(theta, dirs))
>>> fd = [(a - b) / 2e-4 for a, b in zip(sum(one(shift(1e-4)), ()), sum(one(shift(-1e-4)), ()))]
>>> got = [k[0] for k in KB + KG]
>>> bool(max(np.linalg.norm(g - f) / np.linalg.norm(f) for g, f in zip(got, 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())     # <a, K b> = <b, K a>
True
Parameters:
  • ztildes (t3toolbox.backend.common.typ.Sequence[NDArray])

  • ww (t3toolbox.backend.common.typ.Sequence[NDArray])

  • pp (t3toolbox.backend.common.typ.Sequence[NDArray])

  • core_pair (t3toolbox.backend.common.typ.Tuple[t3toolbox.backend.common.typ.Sequence[NDArray], t3toolbox.backend.common.typ.Sequence[NDArray]])

  • core_directions (t3toolbox.backend.common.typ.Tuple[t3toolbox.backend.common.typ.Sequence[NDArray], t3toolbox.backend.common.typ.Sequence[NDArray]])

  • order (int)

  • chunk_size (t3toolbox.backend.common.typ.Optional[int])

Return type:

t3toolbox.backend.common.typ.Tuple[t3toolbox.backend.common.typ.Tuple[NDArray, …], t3toolbox.backend.common.typ.Tuple[NDArray, …]]