least_squares_problem#
- t3toolbox.optimizers.least_squares_problem(geometry, kind, sample, data, x0, order=None, weight=None, regularizer=None, chunk_size='auto', batch=None, hessian='gauss_newton')#
def least_squares_problem( geometry, # MANIFOLD / COREWISE / UNIFORM_* / a SharedGeometry over one kind: str, # 'apply' / 'entries' / 'probe' (+ '_derivatives'), case-insensitive sample: typ.Any, # ww / index, or (ww, pp) / (index, pp) for derivatives data: typ.Any, # observed S(x_true) (+ noise) x0: Point, # ragged or uniform: picks the representation order: typ.Optional[int] = None, # derivative kinds only: highest order (required) weight: typ.Optional[typ.Any] = None, # residual weight ω: per-mode (probe) / ω[mode,order] regularizer: typ.Any = None, # optional, e.g. optimizers.IdentityRegularizer(λ) chunk_size: typ.Any = 'auto', # probe_derivatives 𝒥ᵀ memory chunk (docs/chunking.md) batch: typ.Optional[int] = None, # minibatch W (mc_sgd / adam), for chunk_size='auto' hessian: str = 'gauss_newton', # the solve's mode, for chunk_size='auto': or 'exact' | 'switch' ) -> LeastSquaresSetup: # .problem, .x0_cores, .rewrap
Build the backend least-squares problem from the frontend vocabulary – the constructor the four optimizers use, public for scripts that drive the backend directly.
The frontend twin of
t3toolbox.backend.optimizers.least_squares_problem(): that one takes backend objects (a backend geometry, aSamplingKind) and returns a bareProblem; this one takes what the frontend optimizers take – a geometry singleton (or aSharedGeometry), the kind’s name,order/weight– and returns aLeastSquaresSetup(problem, x0_cores, rewrap).problembuilds local models and evaluates the objective at backend cores, and can be handed withx0_coresto a backend optimizer (t3toolbox.backend.optimizers.newton_cg()and the others);rewrapturns the backend result back into a frontend tensor.The representation is inferred from
x0’s type (like the library-wide ragged/uniform dispatch); the geometry must match – aUniformTuckerTensorTrainx0 pairs with a uniform geometry, aTuckerTensorTrainx0 with a ragged one – else a structural error.x0_coresmay have smaller ranks thanx0(the same tensor): on a manifold geometry a raggedx0is reduced to minimal ranks, and a uniformx0always is (uniform_minimal(); the uniform path needs a minimal-rank frame), so a frontend user never meets that requirement. A uniformrewrapcarries the reduced point’sshape+masks.chunk_sizeandbatchare as in the optimizers (Memory chunking of the derivative transpose (chunk_size)).hessian('gauss_newton','exact'or'switch', case-insensitive) is the mode the problem will be solved in. It does not fix the mode – that is the local model’s and the solver’s ownhessianargument – it sizeschunk_size='auto': the chunk is a field of the sampling kind, fixed here, and with the exact Hessian the same chunk also drives its two-hole assemblies, so it is sized for those too.'switch'is sized as'exact'(its tracking products and its exact phase run the two-hole assemblies). Pass the solver’s value (newton_cg()does).Examples
Build the problem of a ragged
probefit, take the backend local model at the start, and read off its gradient – raw(tucker_variations, tt_variations)tuples, shaped like the frame’s cores:>>> import numpy as np >>> import t3toolbox.tucker_tensor_train as t3 >>> import t3toolbox.manifold as t3m >>> import t3toolbox.optimizers as optimizers >>> np.random.seed(0) >>> A = t3.TuckerTensorTrain.randn((4, 5, 6), (2, 2, 2), (1, 2, 2, 1)) >>> ww = [np.random.randn(30, N) for N in (4, 5, 6)] >>> x0 = t3.TuckerTensorTrain.randn((4, 5, 6), (2, 2, 2), (1, 2, 2, 1)) >>> setup = optimizers.least_squares_problem(t3m.MANIFOLD, 'probe', ww, A.probe(ww), x0) >>> type(setup.problem).__name__ 'Problem' >>> g_tucker, g_tt = setup.problem.local_model(setup.x0_cores).gradient >>> [V.shape for V in g_tucker], [H.shape for H in g_tt] ([(2, 4), (2, 5), (2, 6)], [(1, 2, 2), (2, 2, 2), (2, 2, 1)])
The same
problemdrives a backend optimizer, andrewrapreturns its result as a frontend tensor:>>> import t3toolbox.backend.optimizers as bopt >>> x_cores, stats = bopt.newton_cg(setup.problem, setup.x0_cores, max_newton=20) >>> x_opt = setup.rewrap(x_cores) >>> type(x_opt).__name__, stats['stop_reason'] ('TuckerTensorTrain', 'gradient') >>> bool(np.linalg.norm(x_opt.to_dense() - A.to_dense()) < 1e-6 * np.linalg.norm(A.to_dense())) True
For exact Newton pass the mode to both – here on
probe_derivatives(the solver’s trust region is the exact Hessian’s default step control):>>> pp = [np.random.randn(30, N) for N in (4, 5, 6)] >>> esetup = optimizers.least_squares_problem(t3m.MANIFOLD, 'probe_derivatives', (ww, pp), ... A.probe_derivatives(ww, pp, 1), x0, order=1, hessian='exact') >>> x_cores, stats = bopt.newton_cg(esetup.problem, esetup.x0_cores, max_newton=40, hessian='exact') >>> stats['history'][0]['step_control'], stats['stop_reason'] ('trust_region', 'gradient')
A uniform
x0(with a uniform geometry) gives the packed problem;x0_coresis then the bare supercore pair:>>> import t3toolbox.uniform_tucker_tensor_train as ut3 >>> import t3toolbox.uniform_manifold as ut3m >>> usetup = optimizers.least_squares_problem(ut3m.UNIFORM_MANIFOLD, 'probe', ww, A.probe(ww), ... ut3.UniformTuckerTensorTrain.from_t3(x0)) >>> [sc.shape for sc in usetup.x0_cores], type(usetup.rewrap(usetup.x0_cores)).__name__ ([(3, 2, 6), (3, 2, 2, 2)], 'UniformTuckerTensorTrain')
A geometry of the other representation is a structural error:
>>> ux0 = ut3.UniformTuckerTensorTrain.from_t3(x0) >>> optimizers.least_squares_problem(t3m.MANIFOLD, 'probe', ww, A.probe(ww), ux0) Traceback (most recent call last): ValueError: a UniformTuckerTensorTrain x0 requires a uniform geometry
- Parameters:
kind (str)
sample (Any)
data (Any)
x0 (Point)
order (Optional[int])
weight (Optional[Any])
regularizer (Any)
chunk_size (Any)
batch (Optional[int])
hessian (str)
- Return type: