44from tensorly .decomposition import tucker
55
66
7+ def _fit_tucker (tensor_filled , mask , rank ):
8+ """Fit one masked Tucker decomposition; return the fit and its squared error."""
9+ fit , errors = tucker (
10+ tensor_filled ,
11+ rank = rank ,
12+ svd = "randomized_svd" ,
13+ tol = 1e-8 ,
14+ mask = mask ,
15+ return_errors = True ,
16+ )
17+ return fit , errors [- 1 ] ** 2.0
18+
19+
720def tucker_decomp (tensor , num_comps : int ):
8- """Performs Tucker decomposition.
21+ """Performs Tucker decomposition, greedily growing the rank of one mode at a time.
22+
23+ Starting from rank 1 along every mode, each step increases the rank of
24+ whichever single mode lowers the reconstruction error the most. A mode is
25+ no longer considered once its rank reaches ``min(num_comps, size of that
26+ mode)``, so no mode is ever given a rank above ``num_comps`` and the search
27+ ends once every mode has reached its limit.
928
1029 Parameters
1130 ----------
1231 tensor : xarray or ndarray
1332 multi-dimensional data input
1433 num_comps : int
15- the number of components .
34+ the maximum rank to test along each mode .
1635
1736 Returns
1837 -------
1938 factors : list of lists
2039 containing tucker factorization object of each rank.
2140 min_err : list
22- list of minimum errors of tensor reconstruction for each sum of components.
23- min_err_rank : list of tuples
24- list of the corresponding ranks combinations for the minimum error.
41+ list of minimum errors of tensor reconstruction for each rank combination.
42+ min_err_rank : list of lists
43+ list of the corresponding rank combinations (one rank per mode) for the
44+ minimum error. The first entry is all ones; each later entry differs
45+ from the previous one by +1 in exactly one mode.
2546 """
2647
2748 # if tensor is xarray...
@@ -30,51 +51,31 @@ def tucker_decomp(tensor, num_comps: int):
3051
3152 mask = np .isfinite (tensor )
3253 tensor_filled = np .nan_to_num (tensor )
54+ max_rank = [min (num_comps , size ) for size in tensor .shape ]
3355
3456 # step 1 with 1 component along every dimension
35- start = [1 ] * tensor .ndim
36- ff , errors = tucker (
37- tensor_filled ,
38- rank = start ,
39- svd = "randomized_svd" ,
40- tol = 1e-8 ,
41- mask = mask ,
42- return_errors = True ,
43- )
44- factors = [ff ]
45- min_err = [errors [- 1 ] ** 2.0 ]
46- min_rank = [start ]
47- ranks = min_rank * tensor .ndim
57+ rank = [1 ] * tensor .ndim
58+ fit , err = _fit_tucker (tensor_filled , mask , rank )
59+ factors = [fit ]
60+ min_err = [err ]
61+ min_rank = [rank ]
4862
49- for _ in range (tensor .ndim * num_comps ):
50- fac = []
51- err = []
52- rnk = []
53- for indx , val in enumerate (ranks ):
54- temp_rank = val .copy ()
55- temp_rank [indx ] = val [indx ] + 1
56-
57- if temp_rank [indx ] > tensor .shape [indx ]:
63+ while True :
64+ candidates = []
65+ for mode in range (tensor .ndim ):
66+ if rank [mode ] >= max_rank [mode ]:
5867 continue
68+ temp_rank = rank .copy ()
69+ temp_rank [mode ] += 1
70+ candidates .append ((temp_rank , * _fit_tucker (tensor_filled , mask , temp_rank )))
5971
60- # calculate error for this rank combination
61- ff , errors = tucker (
62- tensor_filled ,
63- rank = temp_rank ,
64- svd = "randomized_svd" ,
65- tol = 1e-8 ,
66- mask = mask ,
67- return_errors = True ,
68- )
69- fac .append (ff )
70- err .append (errors [- 1 ] ** 2.0 )
71- rnk .append (temp_rank )
72+ if not candidates :
73+ break
7274
7375 # pick the lowest error and continue with that
74- min_err .append (min (err ))
75- min_rank .append (rnk [err .index (min (err ))])
76- factors .append (fac [err .index (min (err ))])
77-
78- ranks = [min_rank [- 1 ]] * tensor .ndim
76+ rank , fit , err = min (candidates , key = lambda c : c [2 ])
77+ factors .append (fit )
78+ min_err .append (err )
79+ min_rank .append (rank )
7980
8081 return factors , min_err , min_rank
0 commit comments