t3_apply_corewise_derivatives_hessian#
- t3toolbox.backend.sampling_derivatives.t3_apply_corewise_derivatives_hessian(c, ww, pp, core_pair, core_directions, order)#
def t3_apply_corewise_derivatives_hessian( c: NDArray, # residual jet (omega^2 applied), shape=(order+1,)+W+C 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 ) -> 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_apply_corewise_derivatives_transpose()along core directions: the core-coordinate Hessian oftheta -> <c, S(Phi(theta))>applied totheta_hat,K_c theta_hat = d/ds grad_theta <c, S(Phi(theta + s theta_hat))> |_{s=0},for
Sthe apply-derivative jets (orders0..order) and fixed residual jetsc– the sampling half of the exact Riemannian Hessian’s curvature term forapply_derivatives(withc = omega^2 r), by a hand-written two-hole sweep: exact and symmetric. It differentiates the transpose’s adjoint-state form by the product rule – thec-seeded right contextsigma_hatswept throughG, then its derivative (compute_sigma_hat_tangent_jets()) and those ofxiandmu(the GN tangent forward) feed the derivatives of the two-rowdxi_hatand of the single-term assemblies. The all-modes special case oft3_probe_corewise_derivatives_hessian()(apply contracts the free mode of probe withw + s p), and atorder = 0the plaint3_apply_corewise_hessian().t3_entries_corewise_derivatives_hessian()is the same with fiber-slicedxiand one-hot vectors.Stacks:
candcore_paircarryW + C/C; the directions carry a tangent stackK; the output is raw core variations shaped like the directions,Wsummed. Polymorphic over ragged tuples and uniform supercores (packedww/pp). Mask-free by contract (ast3_probe_corewise_derivatives_hessian()): a uniform caller masksthetaandtheta_hatwiththeta’s masks (ut3_apply_corewise_derivatives_hessian()). Unchunked, like the GN transpose. The precomputed-sweep variant ist3_apply_corewise_derivatives_hessian_from_sweep().Examples
The directional derivative of the corewise transpose (a polynomial in
s, so a central difference is accurate toO(h^2)), and symmetric:>>> 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] >>> c = rng.standard_normal((order + 1, W)) # residual jets, (order+1)+W >>> 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_apply_corewise_derivatives_hessian(c, ww, pp, theta, dirs, order) >>> [k.shape for k in KB] # K + the Tucker core shapes [(2, 2, 4), (2, 3, 5), (2, 2, 3)] >>> one = lambda cores: sd.t3_apply_corewise_derivatives_transpose(c, ww, pp, cores, order, 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()) # <a, K b> = <b, K a> True
- Parameters:
c (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)
- Return type:
t3toolbox.backend.common.typ.Tuple[t3toolbox.backend.common.typ.Tuple[NDArray, …], t3toolbox.backend.common.typ.Tuple[NDArray, …]]