22Coupled Matrix Tensor Factorization
33"""
44
5+ from copy import deepcopy
6+
57import numpy as np
6- from tensorly .tenalg import svd_interface
78import tensorly as tl
8- from tensorly .tenalg import khatri_rao # ty: ignore[unresolved-import] # backend-dispatched at runtime
9- from copy import deepcopy
10- from tensorly .decomposition ._cp import initialize_cp
9+ from tensorly .tenalg import (
10+ khatri_rao , # ty: ignore[unresolved-import] # backend-dispatched at runtime
11+ svd_interface ,
12+ )
1113from tqdm import tqdm
12- from .SVD_impute import IterativeSVD
13- from .linalg import mlstsq , calcR2X_TnB
1414
15+ from .linalg import calcR2X_TnB , mlstsq
16+ from .SVD_impute import IterativeSVD
1517
16- tl .set_backend (' numpy' )
18+ tl .set_backend (" numpy" )
1719
1820
1921def buildMat (tFac ):
20- """ Build the matrix in CMTF from the factors. """
21- if hasattr (tFac , ' mWeights' ):
22+ """Build the matrix in CMTF from the factors."""
23+ if hasattr (tFac , " mWeights" ):
2224 return tFac .factors [0 ] @ (tFac .mFactor * tFac .mWeights ).T
2325 return tFac .factors [0 ] @ tFac .mFactor .T
2426
2527
2628def calcR2X (tFac , tIn = None , mIn = None ):
27- """ Calculate R2X. Optionally it can be calculated for only the tensor or matrix. """
29+ """Calculate R2X. Optionally it can be calculated for only the tensor or matrix."""
2830 assert (tIn is not None ) or (mIn is not None )
2931 vTop , vBottom = 0.0 , 0.0
3032
@@ -41,17 +43,17 @@ def calcR2X(tFac, tIn=None, mIn=None):
4143
4244
4345def tensor_degFreedom (tFac ) -> int :
44- """ Calculate the degrees of freedom within a tensor factorization. """
46+ """Calculate the degrees of freedom within a tensor factorization."""
4547 deg = np .sum ([f .size for f in tFac .factors ])
4648
47- if hasattr (tFac , ' mFactor' ):
49+ if hasattr (tFac , " mFactor" ):
4850 deg += tFac .mFactor .size
4951
5052 return deg
5153
5254
5355def reorient_factors (tFac ):
54- """ This function ensures that factors are negative on at most one direction. """
56+ """This function ensures that factors are negative on at most one direction."""
5557 # Flip the types to be positive
5658 tMeans = np .sign (np .mean (tFac .factors [2 ], axis = 0 ))
5759 tFac .factors [1 ] *= tMeans [np .newaxis , :]
@@ -62,13 +64,13 @@ def reorient_factors(tFac):
6264 tFac .factors [0 ] *= rMeans [np .newaxis , :]
6365 tFac .factors [1 ] *= rMeans [np .newaxis , :]
6466
65- if hasattr (tFac , ' mFactor' ):
67+ if hasattr (tFac , " mFactor" ):
6668 tFac .mFactor *= rMeans [np .newaxis , :]
6769 return tFac
6870
6971
7072def sort_factors (tFac ):
71- """ Sort the components from the largest variance to the smallest. """
73+ """Sort the components from the largest variance to the smallest."""
7274 tensor = deepcopy (tFac )
7375
7476 # Variance separated by component
@@ -77,15 +79,21 @@ def sort_factors(tFac):
7779 norm *= np .sum (np .square (factor ), axis = 0 )
7880
7981 # Add the variance of the matrix
80- if hasattr (tFac , 'mFactor' ):
81- norm += np .sum (np .square (tFac .factors [0 ]), axis = 0 ) * np .sum (np .square (tFac .mFactor ), axis = 0 ) * tFac .mWeights
82+ if hasattr (tFac , "mFactor" ):
83+ norm += (
84+ np .sum (np .square (tFac .factors [0 ]), axis = 0 )
85+ * np .sum (np .square (tFac .mFactor ), axis = 0 )
86+ * tFac .mWeights
87+ )
8288
8389 order = np .flip (np .argsort (norm ))
8490 tensor .weights = tensor .weights [order ]
8591 tensor .factors = [fac [:, order ] for fac in tensor .factors ]
86- np .testing .assert_allclose (tl .cp_to_tensor (tFac ), tl .cp_to_tensor (tensor ), atol = 1e-9 )
92+ np .testing .assert_allclose (
93+ tl .cp_to_tensor (tFac ), tl .cp_to_tensor (tensor ), atol = 1e-9
94+ )
8795
88- if hasattr (tFac , ' mFactor' ):
96+ if hasattr (tFac , " mFactor" ):
8997 tensor .mFactor = tensor .mFactor [:, order ]
9098 tensor .mWeights = tensor .mWeights [order ]
9199 np .testing .assert_allclose (buildMat (tFac ), buildMat (tensor ), atol = 1e-9 )
@@ -94,7 +102,7 @@ def sort_factors(tFac):
94102
95103
96104def delete_component (tFac , compNum ):
97- """ Delete the indicated component. """
105+ """Delete the indicated component."""
98106 tensor = deepcopy (tFac )
99107 compNum = np .array (compNum , dtype = int )
100108
@@ -105,21 +113,20 @@ def delete_component(tFac, compNum):
105113 tensor .rank -= compNum .size
106114 tensor .weights = np .delete (tensor .weights , compNum )
107115
108- if hasattr (tFac , ' mFactor' ):
116+ if hasattr (tFac , " mFactor" ):
109117 tensor .mFactor = np .delete (tensor .mFactor , compNum , axis = 1 )
110118 tensor .mWeights = np .delete (tensor .mWeights , compNum )
111119
112120 tensor .factors = [np .delete (fac , compNum , axis = 1 ) for fac in tensor .factors ]
113121 return tensor
114122
115123
116-
117124def cp_normalize (tFac ):
118- """ Normalize the factors using the inf norm. """
125+ """Normalize the factors using the inf norm."""
119126 for i , factor in enumerate (tFac .factors ):
120127 scales = np .linalg .norm (factor , ord = np .inf , axis = 0 )
121128 tFac .weights *= scales
122- if i == 0 and hasattr (tFac , ' mFactor' ):
129+ if i == 0 and hasattr (tFac , " mFactor" ):
123130 mScales = np .linalg .norm (tFac .mFactor , ord = np .inf , axis = 0 )
124131 tFac .mWeights = scales * mScales
125132 tFac .mFactor /= mScales
@@ -172,7 +179,7 @@ def initialize_cp(tensor: np.ndarray, rank: int):
172179 An initial cp tensor.
173180 """
174181 factors = [np .ones ((tensor .shape [i ], rank )) for i in range (tensor .ndim )]
175- contain_missing = ( np .sum (~ np .isfinite (tensor )) > 0 )
182+ contain_missing = np .sum (~ np .isfinite (tensor )) > 0
176183
177184 # SVD init mode whose size is larger than rank
178185 for mode in range (tensor .ndim ):
@@ -182,25 +189,31 @@ def initialize_cp(tensor: np.ndarray, rank: int):
182189 si = IterativeSVD (rank )
183190 unfold = si .fit_transform (unfold )
184191
185- factors [mode ] = svd_interface (unfold , method = "truncated_svd" , n_eigenvecs = rank , flip_sign = True )[0 ]
192+ factors [mode ] = svd_interface (
193+ unfold , method = "truncated_svd" , n_eigenvecs = rank , flip_sign = True
194+ )[0 ]
186195
187196 return tl .cp_tensor .CPTensor ((None , factors ))
188197
189198
190199def perform_CP (tOrig , r = 6 , tol = 1e-6 , maxiter = 50 , progress = False , callback = None ):
191- """ Perform CP decomposition. """
192- if callback : callback .begin ()
200+ """Perform CP decomposition."""
201+ if callback :
202+ callback .begin ()
193203 tFac = initialize_cp (tOrig , r )
194204
195205 # Pre-unfold
196206 unfolded = [tl .unfold (tOrig , i ) for i in range (tOrig .ndim )]
197207
198208 R2X_last = - np .inf
199209 tFac .R2X = calcR2X (tFac , tOrig )
200- if callback : callback .first_entry (tFac )
210+ if callback :
211+ callback .first_entry (tFac )
201212
202213 # Precalculate the missingness patterns
203- uniqueInfo = [np .unique (np .isfinite (B .T ), axis = 1 , return_inverse = True ) for B in unfolded ]
214+ uniqueInfo = [
215+ np .unique (np .isfinite (B .T ), axis = 1 , return_inverse = True ) for B in unfolded
216+ ]
204217
205218 tq = tqdm (range (maxiter ), disable = (not progress ))
206219 for i in tq :
@@ -213,7 +226,8 @@ def perform_CP(tOrig, r=6, tol=1e-6, maxiter=50, progress=False, callback=None):
213226 tFac .R2X = calcR2X (tFac , tOrig )
214227 tq .set_postfix (R2X = tFac .R2X , delta = tFac .R2X - R2X_last , refresh = False )
215228 assert tFac .R2X > 0.0
216- if callback : callback .update (tFac )
229+ if callback :
230+ callback .update (tFac )
217231
218232 if tFac .R2X - R2X_last < tol :
219233 break
@@ -223,12 +237,12 @@ def perform_CP(tOrig, r=6, tol=1e-6, maxiter=50, progress=False, callback=None):
223237
224238 if r > 1 :
225239 tFac = sort_factors (tFac )
226-
240+
227241 return tFac
228242
229243
230244def perform_CMTF (tOrig , mOrig , r = 9 , tol = 1e-6 , maxiter = 50 , progress = True ):
231- """ Perform CMTF decomposition. """
245+ """Perform CMTF decomposition."""
232246 assert tOrig .dtype == float
233247 assert mOrig .dtype == float
234248 tFac = initialize_cmtf (tOrig , mOrig , r )
@@ -242,7 +256,10 @@ def perform_CMTF(tOrig, mOrig, r=9, tol=1e-6, maxiter=50, progress=True):
242256 # Precalculate the missingness patterns
243257 uniqueInfo = np .unique (np .isfinite (unfolded .T ), axis = 1 , return_inverse = True )
244258 tUnfolded = {m : tl .unfold (tOrig , m ).T for m in [1 , 2 ]}
245- tUniqueInfo = {m : np .unique (np .isfinite (tUnfolded [m ]), axis = 1 , return_inverse = True ) for m in [1 , 2 ]}
259+ tUniqueInfo = {
260+ m : np .unique (np .isfinite (tUnfolded [m ]), axis = 1 , return_inverse = True )
261+ for m in [1 , 2 ]
262+ }
246263
247264 tq = tqdm (range (maxiter ), disable = (not progress ))
248265 for _ in tq :
@@ -272,4 +289,4 @@ def perform_CMTF(tOrig, mOrig, r=9, tol=1e-6, maxiter=50, progress=True):
272289 tFac = sort_factors (tFac )
273290 tFac .R2X = R2X
274291
275- return tFac
292+ return tFac
0 commit comments