TensorFox 1.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
TensorFox/TensorFox.py ADDED
@@ -0,0 +1,1194 @@
1
+ """
2
+ General Description
3
+ ===================
4
+ Tensor Fox is a vast library of routines related to tensor problems. Since most tensor problems fall in the category
5
+ of NP-hard problems, a great effort was made to make this library as efficient as possible. Some relevant routines and
6
+ features of Tensor Fox are the following:
7
+
8
+ - Canonical polyadic decomposition (CPD)
9
+
10
+ - Multilinear singular value decomposition (MLSVD)
11
+
12
+ - Multilinear rank
13
+
14
+ - Rank estimate
15
+
16
+ - Rank related statistics, including histograms
17
+
18
+ - Rank related information about tensors and tensor spaces
19
+
20
+ - CPD tensor train
21
+
22
+ - High performance with parallelism
23
+
24
+ References
25
+ ==========
26
+
27
+ - C. J. Hillar, and L.-H. Lim. Most tensor problems are NP-hard, Journal of the ACM, 60(6):45:1-45:39, November 2013.
28
+ ISSN 0004-5411. doi: 10.1145/2512329.
29
+
30
+ - T. G. Kolda and B. W. Bader, Tensor Decompositions and Applications, SIAM Review, 51:3, in press (2009).
31
+
32
+ - P. Comon, X. Luciani, and A. L. F. de Almeida, Tensor Decompositions, Alternating Least Squares and other Tales,
33
+ Journal of Chemometrics, Wiley, 2009.
34
+ """
35
+
36
+ # Python modules
37
+ import numpy as np
38
+ from numpy import inf, dot, empty, array, nanargmin, log10, arange, prod, ndarray, uint64, float64
39
+ from numpy.linalg import norm
40
+ import sys
41
+ import time
42
+ from copy import deepcopy
43
+ from decimal import Decimal
44
+ import matplotlib.pyplot as plt
45
+ import numba
46
+ import multiprocessing
47
+ import warnings
48
+
49
+ # Tensor Fox modules
50
+ from TensorFox.Alternating_Least_Squares import *
51
+ from TensorFox.Auxiliar import *
52
+ from TensorFox.Compression import *
53
+ from TensorFox.Conversion import *
54
+ from TensorFox.Critical import *
55
+ from TensorFox.Display import *
56
+ from TensorFox.GaussNewton import *
57
+ from TensorFox.Initialization import *
58
+ from TensorFox.MultilinearAlgebra import *
59
+
60
+
61
+ NumbaDeprecationWarning = numba.errors.NumbaDeprecationWarning
62
+ NumbaPendingDeprecationWarning = numba.errors.NumbaPendingDeprecationWarning
63
+ NumbaPerformanceWarning = numba.errors.NumbaPerformanceWarning
64
+ warnings.simplefilter('ignore', category=NumbaDeprecationWarning)
65
+ warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning)
66
+ warnings.simplefilter('ignore', category=NumbaPerformanceWarning)
67
+ warnings.simplefilter("ignore", RuntimeWarning)
68
+
69
+
70
+ def cpd(T, R, options=False):
71
+ """
72
+ Given a tensor T and a rank R, this function computes an approximated CPD of T with rank r. The factors matrices are
73
+ given in the form of a list [W^(1),...,W^(L)]. They are such that sum_(r=1)^R W[:,r]^(1) ⊗ ... ⊗ W[:,r]^(L) is an
74
+ approximation for T, where W[:,r]^(l) denotes the r-th column of W^(l). The same goes for the other factor matrices.
75
+
76
+ Inputs
77
+ ------
78
+ T: float array or list
79
+ Objective tensor in coordinates. If T is a sparse tensor, give it as a list T = [data, idxs, dims], where dims
80
+ are the dimensions of T (it can be a list, tuple or array). We remark that each idxs[i] is a tuple (or list or
81
+ array) of the coordinates of T such that T[idxs[i]] = data[i]. If idxs is given as an array, note that each row
82
+ of idxs is taken as a set of coordinates.
83
+ R: int
84
+ The desired rank of the approximating tensor.
85
+ options: class with the following parameters
86
+ method: str
87
+ By default Tensor Fox uses method='dGN', which means the program will use the damped Gauss-Newton method.
88
+ For higher order tensors a interesting method to try is the Tensor Train CPD method ('ttcpd'). Is is also
89
+ possible to set method='ttcpd' for third order tensors, then the program uses the Tensor Train CPD for a
90
+ third order tensor. Other possibilitie is method='als' (Alternating Least Squares).
91
+ maxiter: int
92
+ Number of maximum iterations allowed for the dGN function. Default is 200.
93
+ tol, tol_step, tol_improv, tol_grad: float
94
+ Tolerance criterion to stop the iteration process of the dGN function. Let T^(k) be the approximation at
95
+ the k-th iteration, with corresponding CPD w^(k) in vectorized form. The program stops if
96
+ 1) |T - T^(k)| / |T| < tol
97
+ 2) | w^(k-1) - w^(k) | < tol_step
98
+ 3) | |T - T^(k-1)| / |T| - |T - T^(k)| / |T| | < tol_improv
99
+ 4) | grad F(w^(k)) | < tol_grad, where F(w^(k)) = 1/2 |T - T^(k)|^2
100
+ tol_jump: float
101
+ Let eps be the relative error at some iteration and eps_new be the next error. Before accepting the step
102
+ associated to this error, the program verifies if it satisfies the condition eps_new <= tol_jump * eps. If
103
+ this condition is not met, the step is discarded and the dogleg method is used to compute the next step. The
104
+ default is tol_jump = 10.
105
+ inner_method, cg_tol, cg_maxiter, cg_factor: str (or list), float, int and float, respectively
106
+ At each step of the dGN method the program needs to solve an equation. To solve this equation the user can
107
+ use the conjugate gradient with inner_method='cg' (default) or a direct method using matrix factorization
108
+ with inner_method='direct'. Other possibilities are inner_method='cg_static' and inner_method='als'. The
109
+ difference between static and non-static cg versions are the way the program deals with the maximum number
110
+ of iterations.
111
+ The static algorithm have a certain maximum number of iterations cg_maxiter fixed during all the program.
112
+ The non-static versions uses the parameter cg_factor to control the number of iterations in a different way.
113
+ If the program is at the k-th iteration of the dGN, then the maximum number of iterations permitted for the
114
+ cg method is 1 + int( cg_factor * randint( 1 + k^(0.4), 2 + k^(0.9) ) ). This strange interval of random
115
+ integers were obtained after a lot of tests, a lot! This seems to be a robust choice, but since we can't be
116
+ right all the time, the parameter cg_factor comes to the rescue. Finally, the parameter cg_tol, as the name
117
+ suggests, is the tolerance parameter for the cg method. The cg iterations stops when the (absolute) residual
118
+ is less than cg_tol.
119
+ We remark that it is also possible a list to the parameter innver_method. The list should be a sequence of
120
+ the inner method described here. The the program will use the prescribed sequence given by the user.
121
+ bi_parameters: list
122
+ When computing the CPD with the Tensor Train method, several thir order CPDs must be computed. The first CPD
123
+ can be computed as any CPD, but the other always depends on some previous computed factor, which is always
124
+ used to fix one factor of the next CPD. This means each CPD, except the first, is actually only computing
125
+ two factors, so there is a difference in how the program computes the first CPD and the remaining ones.
126
+ The previous parameters are used for the first CPD and the parameters, and the parameters
127
+ bi_parameters=[bi_method, bi_method_maxiter, bi_method_tol] are used for all the remaining CPD. They work
128
+ similarly as the parameters method, maxiter and tol, respectively.
129
+ Defaults are bi_method='als', bi_maxiter=500, bi_tol=1e-6.
130
+ initialization: string or list
131
+ This function generates a starting point to begin the iterations. There are four options:
132
+ 1) list: the user may give a list with the factor matrices to be used as starting point.
133
+ 2) 'random': the entries the factor matrices are generated by the normal distribution with mean 0 and
134
+ variance 1. This is the default option.
135
+ 3) 'smart_random': generates a random starting point with a method based on the MLSVD which always
136
+ guarantee a small relative error. Check the function 'smart_random' for more details about this method.
137
+ 4) 'smart': works similar as the smart_random method, but this one is deterministic and generates the
138
+ best rank-R approximation based on the MLSVD.
139
+ mlsvd_method: str
140
+ The possibilities are 'seq' (default), 'classic', 'sparse' and 'gpu'. We describe then briefly below:
141
+ 1) 'seq': Sequentially truncated method, based on N. Vannieuwenhoven, R. Vandebril, and K. Meerbergen,
142
+ A new truncation strategy for the higher-order singular value decomposition, SIAM J. Sci. Comput. 34
143
+ (2012), no. 2, A1027-A1052. This is the default method.
144
+ 2) The naive method, with amount to compute the SVD of all unfoldings.
145
+ 3) The sparse method computes the SVDs of the unfoldings times their transposes. This is well suited for
146
+ sparse matrices.
147
+ 4) Is the same as the sparse method but it uses GPU to compute the SVDs. Since the matrix obtained by
148
+ the propduct of the unfolding by their transpose is small, this method requires less memory.
149
+ tol_mlsvd: float
150
+ Tolerance criterion for the truncation. The idea is to obtain a truncation (U_1,...,U_L)*S such that
151
+ |T - (U_1,...,U_L)*S| / |T| < tol_mlsvd. Default is 1e-16. If tol_mlsvd = -1 the program uses the original
152
+ tensor, so the computation of the MLSVD is not performed.
153
+ trunc_dims: 0 or list of ints
154
+ force the compressed tensor (that is, the core tensor of the MLSVD) to have a specified shape. Consider a
155
+ third order tensor T of shape (m, n, p). If trunc_dims is not 0, then it should be a list withthree integers
156
+ [R1,R2,R3] such that 1 <= R1 <= m, 1 <= R2 <= n, 1 <= R3 <= p. The compressed tensor will have dimensions
157
+ (R1,R2,R3). Default is 0, which means 'automatic' truncation (recommended).
158
+ init_damp: float
159
+ To solve the equation associated to each iteration of the dGN method (only valid when using conjugate
160
+ gradient) we need to deal with the lack of full rank we introduce regularization. Thus obtaining the new
161
+ set of equations (J^T * J + mu * D) * x = J^T * b, where mu > 0 is the damping parameter and D is a
162
+ diagonal matrix. At each iteration the damping parameter is updated following a certain rule, and the user
163
+ doesn't have influence over this. On the other hand, the user can choose the initial damping parameter
164
+ factor. More precisely, the first damping parameter is mu = init_damp * E[T], where init_damp is the damping
165
+ parameter factor and E[T] is the mean of the values of T (if there is compression, S is used instead of T).
166
+ The default value used is init_damp=1.
167
+ Experience shows that this value has little influence on the overall process, but sometimes it has a
168
+ noticeable influence, so be aware of that. Finally, we remark that it is possible to pass this parameter as
169
+ a list of values, such that init_damp[k] will be the damping parameter used at k-th iteration.
170
+ refine: bool
171
+ If True, after the dGN iterations the program uses the solution to repeat the dGN over the original space
172
+ using the solution as starting point. Default is False.
173
+ symm: bool
174
+ The user should set symm to True if the objective tensor is symmetric, otherwise symm is False. Default is
175
+ False.
176
+ factors_norm: float
177
+ This parameter is used to fix the norm of the factor matrices of the CPD. Suppose T is a third tensor and
178
+ X^(k), Y^(k), Z^(k) the approximated CPD at iteration k. If one set factors_norm=2, for example, then
179
+ |X^(k)| = |Y^(k)| = |Z^(k)| = 2 for all k.
180
+ trials: int
181
+ This parameter is only used for tensor with order higher than 3. The computation of the tensor train CPD
182
+ requires the computation of several CPD of third order tensors. If only one of these CPD's is of low
183
+ quality (divergence or local minimum) then all effort is in vain. One work around is to compute several
184
+ CPD'd and keep the best, for third order tensor. The parameter trials defines the maximum number of
185
+ times we repeat the computation of each third order CPD. These trials stops when the relative error is
186
+ less than 1e-4 or when the maximum number of trials is reached. Default is trials=1.
187
+ display: -2, -1, 0, 1, 2, 3 or 4
188
+ This options is used to control how information about the computations are displayed on the screen. The
189
+ possible values are -1, 0, 1 (default), 2, 3, 4. Notice that display=3 makes the overall running time large
190
+ since it will force the program to show intermediate errors which are computationally costly. -1 is a
191
+ special option for displaying minimal relevant information for tensors with order higher then 3. We
192
+ summarize the display options below.
193
+ -2: display same as options -1 plus the Tensor Train error
194
+ -1: display only the errors of each CPD computation and the final relevant information
195
+ 0: no information is printed
196
+ 1: partial information is printed
197
+ 2: full information is printed
198
+ 3: full information + errors of truncation and starting point are printed
199
+ 4: almost equal to display = 3 but now there are more digits displayed on the screen (display = 3 is a
200
+ "cleaner" version of display = 4, with less information).
201
+ epochs: int
202
+ Number of Tensor Train CPD cycles. Use only for tensor with order higher than 3. Default is epochs=1.
203
+ gpu: bool
204
+ If True, the program uses GPU to compute the MLSVD. Default is False.
205
+ mkl_dot: bool
206
+ For sparse tensors, this options tells the program to perform matrix-matrix multiplication using specific
207
+ multithread MKL routines (mkl_sparse_spmm). When mkl_dot=False, the program uses the standard scipy dot
208
+ function, which is single thread but requires much less memory. Default is True.
209
+ svd_n_components: int
210
+ Number of singular values and vectors to extract when computing the randomized SVD. The default is set to
211
+ None, which means the program will use min(R, dims[l]) for dense tensors and L * min(R, dims[l]) for sparse
212
+ tensors, where L is the number of dimensions (modes), R is the rank and dims[l] is the l-th dimension of
213
+ the tensors. The number of components for each dimension is an upper bound for the dimension of the
214
+ compressed tensor. Higher values may lead to better approximations with a cost on computational cost.
215
+ conditioning: list
216
+ Save condition numbers of intermediate matrices in the CG method. Only for display = 4 and it is not applied
217
+ to the refine stage, since these are heavy computations.
218
+
219
+ It is not necessary to create 'options' with all parameters described above. Any missing parameter is assigned to
220
+ its default value automatically. For a tutorial about the options, check the Tensor Fox tutorial at
221
+
222
+ https://github.com/felipebottega/Tensor-Fox/tree/master/tutorial
223
+
224
+ Outputs
225
+ -------
226
+ factors: list of float 2D arrays with shape (dims[i], R) each
227
+ The factors matrices which corresponds to an approximate CPD for T.
228
+ final_outputs: list of classes
229
+ Each tricpd and bicpd call gives a output class with all sort of information about the computations. The list
230
+ 'final_outputs' contains all these classes.
231
+ """
232
+
233
+ # INITIAL PREPARATIONS
234
+
235
+ # Extract the dimensions and order to generate options, test consistency and check if T is a third order tensor.
236
+ if type(T) == list:
237
+ _, _, dims_orig = T
238
+ numba.set_num_threads(int(0.8 * multiprocessing.cpu_count())) # if T is sparse, the recommended is to use 80% of the threads
239
+ else:
240
+ dims_orig = T.shape
241
+ numba.set_num_threads(multiprocessing.cpu_count()) # if T is dense, we can use all of the threads
242
+ L = len(dims_orig)
243
+
244
+ # Set options.
245
+ options = make_options(options)
246
+ method = options.method
247
+ display = options.display
248
+ tol_mlsvd = options.tol_mlsvd
249
+ if type(tol_mlsvd) == list:
250
+ if L > 3:
251
+ tol_mlsvd = tol_mlsvd[0]
252
+ else:
253
+ tol_mlsvd = tol_mlsvd[1]
254
+
255
+ # Test consistency of dimensions and rank.
256
+ consistency(R, dims_orig, options)
257
+
258
+ # Verify method.
259
+ if method == 'dGN' or method == 'als':
260
+ factors, output = tricpd(T, R, options)
261
+ return factors, output
262
+
263
+ # Verify if T is sparse, in which case it will be given as a list with the data.
264
+ if type(T) == list:
265
+ T_orig = T
266
+ data_orig, idxs_orig, dims_orig = T_orig
267
+ data_orig = array(data_orig, dtype=float64)
268
+ idxs_orig = array(idxs_orig, dtype=uint64)
269
+ dims_orig = array(dims_orig)
270
+ if display != 0:
271
+ print('Sparse tensor detected')
272
+ print(' nnz =', len(data_orig))
273
+ print(' Sparsity level =', round(100*(1 - len(data_orig)/multiply_dims(dims_orig)), 12), '%')
274
+
275
+ # Change ordering of indexes to speed-up MLSVD.
276
+ T, ordering = sort_dims(T)
277
+
278
+ if type(T) == list:
279
+ Tsize = norm(T[0])
280
+ dims = T[2]
281
+ # If T is sparse, we must use the classic method, and tol_mlsvd is set to the default 1e-16 in the case the
282
+ # user requested -1.
283
+ if tol_mlsvd < 0:
284
+ print('Using class MLSVD with tol_mlsvd = 1e-16.')
285
+ options.tol_mlsvd = 1e-16
286
+ tol_mlsvd = 1e-16
287
+ else:
288
+ Tsize = norm(T)
289
+ dims = T.shape
290
+
291
+ # COMPRESSION STAGE
292
+
293
+ if display != 0:
294
+ print('-----------------------------------------------------------------------------------------------')
295
+ print('Computing MLSVD')
296
+
297
+ # Compute compressed version of T with the MLSVD. We have that T = (U_1,...,U_L)*S.
298
+ if display > 2 or display < -1:
299
+ S, U, T1, sigmas, best_error = mlsvd(T, Tsize, R, options)
300
+ else:
301
+ S, U, T1, sigmas = mlsvd(T, Tsize, R, options)
302
+
303
+ if display != 0:
304
+ if prod(array(S.shape) == array(dims)):
305
+ if tol_mlsvd == -1:
306
+ print(' No compression and no truncation requested by user')
307
+ print(' Working with dimensions', tuple(map(int, dims)))
308
+ else:
309
+ print(' No compression detected')
310
+ print(' Working with dimensions', tuple(map(int, dims)))
311
+ else:
312
+ print(' Compression detected')
313
+ print(' Compressing from', tuple(map(int, dims)), 'to', S.shape)
314
+ if display > 2 or display < -1:
315
+ print(' Compression relative error = {:7e}'.format(best_error))
316
+ print()
317
+
318
+ # Increase dimensions if r > min(S.shape).
319
+ S_orig_dims = S.shape
320
+ if R > min(S_orig_dims):
321
+ inflate_status = True
322
+ S = inflate(S, R, S_orig_dims)
323
+ else:
324
+ inflate_status = False
325
+
326
+ # For higher order tensors the trunc_dims options is only valid for the original tensor and its MLSVD.
327
+ options.trunc_dims = 0
328
+
329
+ # TENSOR TRAIN AND DAMPED GAUSS-NEWTON STAGE
330
+
331
+ factors, outputs = highcpd(S, R, options)
332
+ factors = deflate(factors, S_orig_dims, inflate_status)
333
+
334
+ # Use the orthogonal transformations to work in the original space.
335
+ for l in range(L):
336
+ factors[l] = dot(U[l], factors[l])
337
+
338
+ # FINAL WORKS
339
+
340
+ # Compute error.
341
+ if type(T1) == ndarray:
342
+ T1_approx = empty(T1.shape)
343
+ T1_approx = cpd2unfold1(T1_approx, factors)
344
+ rel_error = fastnorm(T1, T1_approx)/Tsize
345
+
346
+ # Go back to the original dimension ordering.
347
+ factors = unsort_dims(factors, ordering)
348
+
349
+ else:
350
+ # Go back to the original dimension ordering (sparse case).
351
+ factors = unsort_dims(factors, ordering)
352
+ rel_error = sparse_fastnorm(data_orig, idxs_orig, dims_orig, factors)/Tsize
353
+
354
+ num_steps = 0
355
+ for output in outputs:
356
+ num_steps += output.num_steps
357
+ accuracy = max(0, 100*(1 - rel_error))
358
+
359
+ if options.display != 0:
360
+ print()
361
+ print('===============================================================================================')
362
+ print('===============================================================================================')
363
+ print('Final results')
364
+ print(' Number of steps =', num_steps)
365
+ print(' Relative error =', rel_error)
366
+ acc = float( '%.6e' % Decimal(accuracy) )
367
+ print(' Accuracy = ', acc, '%')
368
+
369
+ final_outputs = make_final_outputs(num_steps, rel_error, accuracy, outputs, options)
370
+
371
+ return factors, final_outputs
372
+
373
+
374
+ def highcpd(T, R, options):
375
+ """
376
+ This function makes the calls in order to compute the tensor train of T and obtain the final CPD from it. It is
377
+ important to realize that this function is limited to tensor where each one of its factors is a full rank matrix.
378
+ In particular, the rank R must be smaller than all dimensions of T.
379
+ """
380
+
381
+ # Create relevant values.
382
+ dims = T.shape
383
+ L = len(dims)
384
+ display = options.display
385
+ max_trials = options.trials
386
+ options.refine = False
387
+ epochs = options.epochs
388
+
389
+ # Compute cores of the tensor train of T.
390
+ G = cpdtt(T, R)
391
+ if display > 2 or display < -1:
392
+ print('===============================================================================================')
393
+ print('SVD Tensor train error = ', tt_error(T, G, dims, L))
394
+ print('===============================================================================================')
395
+ print()
396
+
397
+ if display != 0:
398
+ print('Total of', L-2, 'third order CPDs to be computed:')
399
+ print('===============================================================================================')
400
+
401
+ cpd_list, outputs, best_Z = cpd_cores(G, max_trials, epochs, R, display, options)
402
+
403
+ # Compute of factors of T.
404
+
405
+ # First factor
406
+ factors = [dot(G[0], cpd_list[0][0])]
407
+ # Factors 2 to L-2.
408
+ for l in range(0, L-2):
409
+ factors.append(cpd_list[l][1])
410
+ B = dot(G[-1].T, best_Z)
411
+ factors.append(B)
412
+ factors = equalize(factors, R)
413
+
414
+ if display > 2 or display < -1:
415
+ G_approx = [G[0]]
416
+ for l in range(1, L-1):
417
+ temp_factors = cpd_list[l-1]
418
+ temp_dims = temp_factors[0].shape[0], temp_factors[1].shape[0], temp_factors[2].shape[0],
419
+ T_approx = cpd2tens(temp_factors)
420
+ G_approx.append(T_approx)
421
+ G_approx.append(G[-1])
422
+ print()
423
+ print('===============================================================================================')
424
+ print('CPD Tensor train error = ', tt_error(T, G_approx, dims, L))
425
+ print('===============================================================================================')
426
+
427
+ return factors, outputs
428
+
429
+
430
+ def tricpd(T, R, options):
431
+ """
432
+ Given a tensor T and a rank R, this function computes an approximated CPD of T with rank R. This function is called
433
+ when the user sets method = 'dGN' or 'als'. The name 'tricpd' is just a remnant of older versions of Tensor Fox
434
+ which were limited to third order tensors. This function is able to handle high order tensors.
435
+
436
+ Inputs
437
+ ------
438
+ T: float array
439
+ R: int
440
+ options: class
441
+
442
+ Outputs
443
+ -------
444
+ factors: list of float 2D arrays
445
+ output: class
446
+ This class contains all information needed about the computations made. We summarize these information below.
447
+ num_steps: the total number of steps (iterations) the dGN function used at the two runs.
448
+ accuracy: the accuracy of the solution, which is defined by the formula 100*(1 - rel_error). 0 means 0% of
449
+ accuracy (worst case) and 100 means 100% of accuracy (best case).
450
+ rel_error: relative error |T - T_approx|/|T| of the approximation computed.
451
+ step_sizes: array with the distances between consecutive computed points at each iteration.
452
+ errors: array with the absolute errors of the approximating tensor at each iteration.
453
+ improv: array with the differences between consecutive absolute errors.
454
+ gradients: array with the gradient of the error function at each iteration. We expect that these gradients
455
+ converges to zero as we keep iterating since the objective point is a local minimum.
456
+ stop: it is a list of two integers. The first integer indicates why the dGN stopped at the first run, and
457
+ the second integer indicates why the dGN stopped at the second run (refinement stage). Check the
458
+ functions mlsvd and dGN for more information.
459
+ """
460
+
461
+ # INITIALIZE RELEVANT VARIABLES
462
+
463
+ # Verify if T is sparse, in which case it will be given as a list with the data.
464
+ if type(T) == list:
465
+ T_orig = T
466
+ dims_orig = array(T_orig[2])
467
+ else:
468
+ dims_orig = T.shape
469
+ L = len(dims_orig)
470
+
471
+ # Set options.
472
+ initialization = options.initialization
473
+ refine = options.refine
474
+ symm = options.symm
475
+ display = options.display
476
+ tol_mlsvd = options.tol_mlsvd
477
+ method = options.method
478
+ if type(tol_mlsvd) == list:
479
+ tol_mlsvd = tol_mlsvd[0]
480
+
481
+ # Verify if T is sparse, in which case it will be given as a list with the data.
482
+ if type(T) == list:
483
+ T_orig = T
484
+ data_orig, idxs_orig, dims_orig = T_orig
485
+ data_orig = array(data_orig, dtype=float64)
486
+ idxs_orig = array(idxs_orig, dtype=uint64)
487
+ dims_orig = array(dims_orig)
488
+ if display != 0:
489
+ print('Sparse tensor detected')
490
+ print(' nnz =', len(data_orig))
491
+ print(' Sparsity level =', round(100*(1 - len(data_orig)/multiply_dims(dims_orig)), 12), '%')
492
+
493
+ # Change ordering of indexes to speed-up MLSVD.
494
+ T, ordering = sort_dims(T)
495
+
496
+ if type(T) == list:
497
+ Tsize = norm(T[0])
498
+ dims = T[2]
499
+ # If T is sparse, we must use the classic method, and tol_mlsvd is set to the default 1e-16 in the case the
500
+ # user requested -1.
501
+ if tol_mlsvd < 0:
502
+ print('Using class MLSVD with tol_mlsvd = 1e-16.')
503
+ tol_mlsvd = 1e-16
504
+ if type(tol_mlsvd) == list:
505
+ options.tol_mlsvd[0] = 1e-16
506
+ else:
507
+ options.tol_mlsvd = 1e-16
508
+ else:
509
+ Tsize = norm(T)
510
+ dims = T.shape
511
+
512
+ # COMPRESSION STAGE
513
+
514
+ if display > 0:
515
+ print('-----------------------------------------------------------------------------------------------')
516
+ print('Computing MLSVD')
517
+
518
+ # Compute compressed version of T with the MLSVD. We have that T = (U_1, ..., U_L)*S.
519
+ if display > 2 or display < -1:
520
+ S, U, T1, sigmas, best_error = mlsvd(T, Tsize, R, options)
521
+ else:
522
+ S, U, T1, sigmas = mlsvd(T, Tsize, R, options)
523
+ dims_cmpr = S.shape
524
+
525
+ # When the tensor is symmetric we want S to have equal dimensions.
526
+ if symm:
527
+ R_min = min(dims_cmpr)
528
+ dims_cmpr = [R_min for l in range(L)]
529
+ dims_cmpr_slices = tuple(slice(R_min) for l in range(L))
530
+ S = S[dims_cmpr_slices]
531
+ U = [U[l][:, :R_min] for l in range(L)]
532
+
533
+ if display > 0:
534
+ if prod(array(dims_cmpr) == array(dims)):
535
+ if tol_mlsvd == -1:
536
+ print(' No compression and no truncation requested by user')
537
+ print(' Working with dimensions', tuple(map(int, dims)))
538
+ else:
539
+ print(' No compression detected')
540
+ print(' Working with dimensions', tuple(map(int, dims)))
541
+ else:
542
+ print(' Compression detected')
543
+ print(' Compressing from', tuple(map(int, dims)), 'to', S.shape)
544
+ if display > 2:
545
+ print(' Compression relative error = {:7e}'.format(best_error))
546
+
547
+ # GENERATION OF STARTING POINT STAGE
548
+
549
+ # Generate initial to start d
550
+ if display > 2 or display < -1:
551
+ init_factors, init_error = starting_point(T, Tsize, S, U, R, ordering, options)
552
+ else:
553
+ init_factors = starting_point(T, Tsize, S, U, R, ordering, options)
554
+
555
+ if display > 0:
556
+ print('-----------------------------------------------------------------------------------------------')
557
+ if type(initialization) == list:
558
+ print('Type of initialization: user')
559
+ else:
560
+ print('Type of initialization:', initialization)
561
+ if display > 2:
562
+ print(' Initial guess relative error = {:5e}'.format(init_error))
563
+
564
+ # DAMPED GAUSS-NEWTON STAGE
565
+
566
+ if display > 0:
567
+ print('-----------------------------------------------------------------------------------------------')
568
+ print('Computing CPD')
569
+
570
+ # Compute the approximated tensor in coordinates with dGN or ALS.
571
+ if method == 'als':
572
+ factors, step_sizes_main, errors_main, improv_main, gradients_main, stop_main = \
573
+ als(S, init_factors, R, options)
574
+ else:
575
+ factors, step_sizes_main, errors_main, improv_main, gradients_main, stop_main = \
576
+ dGN(S, init_factors, R, options)
577
+
578
+ # Use the orthogonal transformations to work in the original space.
579
+ for l in range(L):
580
+ factors[l] = dot(U[l], factors[l])
581
+
582
+ # REFINEMENT STAGE
583
+
584
+ # If T is sparse, no refinement is made.
585
+ if type(T) == list:
586
+ options.refine = False
587
+ refine = False
588
+
589
+ if refine:
590
+ if display > 0:
591
+ print()
592
+ print('===============================================================================================')
593
+ print('Computing refinement of solution')
594
+
595
+ if display > 2:
596
+ T1_approx = empty(T1.shape)
597
+ T1_approx = cpd2unfold1(T1_approx, factors)
598
+ init_error = fastnorm(T1, T1_approx)/Tsize
599
+ print(' Initial guess relative error = {:5e}'.format(init_error))
600
+
601
+ if display > 0:
602
+ print('-----------------------------------------------------------------------------------------------')
603
+ print('Computing CPD')
604
+
605
+ if method == 'als':
606
+ factors, step_sizes_refine, errors_refine, improv_refine, gradients_refine, stop_refine = \
607
+ als(T, factors, R, options)
608
+ else:
609
+ factors, step_sizes_refine, errors_refine, improv_refine, gradients_refine, stop_refine = \
610
+ dGN(T, factors, R, options)
611
+
612
+ else:
613
+ step_sizes_refine = array([0])
614
+ errors_refine = array([0])
615
+ improv_refine = array([0])
616
+ gradients_refine = array([0])
617
+ stop_refine = 8
618
+
619
+ # FINAL WORKS
620
+
621
+ # Compute error.
622
+ if type(T1) == ndarray:
623
+ T1_approx = empty(T1.shape)
624
+ T1_approx = cpd2unfold1(T1_approx, factors)
625
+
626
+ # Go back to the original dimension ordering.
627
+ factors = unsort_dims(factors, ordering)
628
+
629
+ # Save and display final informations.
630
+ output = output_info(T1, Tsize, T1_approx,
631
+ step_sizes_main, step_sizes_refine,
632
+ errors_main, errors_refine,
633
+ improv_main, improv_refine,
634
+ gradients_main, gradients_refine,
635
+ stop_main, stop_refine,
636
+ options)
637
+ else:
638
+ # Go back to the original dimension ordering (sparse case).
639
+ factors = unsort_dims(factors, ordering)
640
+
641
+ # Save and display final informations.
642
+ output = output_info(T_orig, Tsize, factors,
643
+ step_sizes_main, step_sizes_refine,
644
+ errors_main, errors_refine,
645
+ improv_main, improv_refine,
646
+ gradients_main, gradients_refine,
647
+ stop_main, stop_refine,
648
+ options)
649
+
650
+ if display > 0:
651
+ print('===============================================================================================')
652
+ print('Final results')
653
+ if refine:
654
+ print(' Number of steps =', output.num_steps)
655
+ else:
656
+ print(' Number of steps =', output.num_steps)
657
+ print(' Relative error =', output.rel_error)
658
+ acc = float( '%.6e' % Decimal(output.accuracy) )
659
+ print(' Accuracy = ', acc, '%')
660
+
661
+ return factors, output
662
+
663
+
664
+ def bicpd(T, R, fixed_factor, options):
665
+ """
666
+ Practically the same as tricpd, but this function keeps the some factor fixed during all the computations. This
667
+ function is to be used as part of the tensor train cpd.
668
+ """
669
+
670
+ # INITIALIZE RELEVANT VARIABLES
671
+
672
+ # Extract all variable from the class of options.
673
+ initialization = options.initialization
674
+ refine = options.refine
675
+ symm = options.symm
676
+ display = options.display
677
+ tol_mlsvd = options.tol_mlsvd
678
+ bi_method = options.bi_method_parameters[0]
679
+ if type(tol_mlsvd) == list:
680
+ tol_mlsvd = tol_mlsvd[1]
681
+
682
+ # Set the other variables.
683
+ m, n, p = T.shape
684
+ Tsize = norm(T)
685
+ ordering = [0, 1, 2]
686
+
687
+ # Test consistency of dimensions and rank.
688
+ consistency(R, (m, n, p), options)
689
+
690
+ # COMPRESSION STAGE
691
+
692
+ if display > 0:
693
+ print('-----------------------------------------------------------------------------------------------')
694
+ print('Computing MLSVD of T')
695
+
696
+ # Compute compressed version of T with the MLSVD. We have that T = (U1, U2, U3)*S.
697
+ if display > 2 or display < -1:
698
+ S, U, T1, sigmas, best_error = mlsvd(T, Tsize, R, options)
699
+ else:
700
+ S, U, T1, sigmas = mlsvd(T, Tsize, R, options)
701
+ R1, R2, R3 = S.shape
702
+ U1, U2, U3 = U
703
+
704
+ # When the tensor is symmetric we want S to have equal dimensions.
705
+ if symm:
706
+ R_min = min(R1, R2, R3)
707
+ R1, R2, R3 = R_min, R_min, R_min
708
+ S = S[:R_min, :R_min, :R_min]
709
+ U1, U2, U3 = U1[:, :R_min], U2[:, :R_min], U3[:, :R_min]
710
+
711
+ if display > 0:
712
+ if (R1, R2, R3) == (m, n, p):
713
+ if tol_mlsvd == -1:
714
+ print(' No compression and no truncation requested by user')
715
+ print(' Working with dimensions', T.shape)
716
+ else:
717
+ print(' No compression detected')
718
+ print(' Working with dimensions', T.shape)
719
+ else:
720
+ print(' Compression detected')
721
+ print(' Compressing from', T.shape, 'to', S.shape)
722
+ if display > 2:
723
+ print(' Compression relative error = {:7e}'.format(best_error))
724
+
725
+ # GENERATION OF STARTING POINT STAGE
726
+
727
+ # Generate initial to start d
728
+ if display > 2 or display < -1:
729
+ [X, Y, Z], init_error = starting_point(T, Tsize, S, U, R, ordering, options)
730
+ else:
731
+ [X, Y, Z] = starting_point(T, Tsize, S, U, R, ordering, options)
732
+
733
+ # Discard the factor computed in start_point and use the previous one. Then project it on the compressed space.
734
+ if fixed_factor[1] == 0:
735
+ X = dot(U1.T, fixed_factor[0])
736
+ X = [X, 0]
737
+ elif fixed_factor[1] == 1:
738
+ Y = dot(U2.T, fixed_factor[0])
739
+ Y = [Y, 1]
740
+ elif fixed_factor[1] == 2:
741
+ Z = dot(U3.T, fixed_factor[0])
742
+ Z = [Z, 2]
743
+
744
+ if display > 0:
745
+ print('-----------------------------------------------------------------------------------------------')
746
+ if type(initialization) == list:
747
+ print('Type of initialization: fixed + user')
748
+ else:
749
+ print('Type of initialization: fixed +', initialization)
750
+ if display > 2:
751
+ if fixed_factor[1] == 0:
752
+ S_init = cpd2tens([X[0], Y, Z])
753
+ elif fixed_factor[1] == 1:
754
+ S_init = cpd2tens([X, Y[0], Z])
755
+ elif fixed_factor[1] == 2:
756
+ S_init = cpd2tens([X, Y, Z[0]])
757
+ S1_init = unfold(S_init, 1)
758
+ init_error = compute_error(T, Tsize, S1_init, [U1, U2, U3], (R1, R2, R3))
759
+ print(' Initial guess relative error = {:5e}'.format(init_error))
760
+
761
+ # DAMPED GAUSS-NEWTON STAGE
762
+
763
+ if display > 0:
764
+ print('-----------------------------------------------------------------------------------------------')
765
+ print('Computing CPD of T')
766
+
767
+ # Compute the approximated tensor in coordinates with dGN or ALS.
768
+ if bi_method == 'als':
769
+ factors, step_sizes_main, errors_main, improv_main, gradients_main, stop_main = \
770
+ als(S, [X, Y, Z], R, options)
771
+ else:
772
+ factors, step_sizes_main, errors_main, improv_main, gradients_main, stop_main = \
773
+ dGN(S, [X, Y, Z], R, options)
774
+ X, Y, Z = factors
775
+
776
+ # FINAL WORKS
777
+
778
+ # Use the orthogonal transformations to obtain the CPD of T.
779
+ if fixed_factor[1] == 0:
780
+ Y = dot(U2, Y)
781
+ Z = dot(U3, Z)
782
+ elif fixed_factor[1] == 1:
783
+ X = dot(U1, X)
784
+ Z = dot(U3, Z)
785
+ elif fixed_factor[1] == 2:
786
+ X = dot(U1, X)
787
+ Y = dot(U2, Y)
788
+
789
+ # Compute error.
790
+ T1_approx = empty(T1.shape)
791
+ if fixed_factor[1] == 0:
792
+ T1_approx = cpd2unfold1(T1_approx, [fixed_factor[0], Y, Z])
793
+ elif fixed_factor[1] == 1:
794
+ T1_approx = cpd2unfold1(T1_approx, [X, fixed_factor[0], Z])
795
+ elif fixed_factor[1] == 2:
796
+ T1_approx = cpd2unfold1(T1_approx, [X, Y, fixed_factor[0]])
797
+
798
+ # Save and display final information.
799
+ step_sizes_refine = array([0])
800
+ errors_refine = array([0])
801
+ improv_refine = array([0])
802
+ gradients_refine = array([0])
803
+ stop_refine = 5
804
+ output = output_info(T1, Tsize, T1_approx,
805
+ step_sizes_main, step_sizes_refine,
806
+ errors_main, errors_refine,
807
+ improv_main, improv_refine,
808
+ gradients_main, gradients_refine,
809
+ stop_main, stop_refine,
810
+ options)
811
+
812
+ if display > 0:
813
+ print('===============================================================================================')
814
+ print('Final results of bicpd')
815
+ if refine:
816
+ print(' Number of steps =', output.num_steps)
817
+ else:
818
+ print(' Number of steps =', output.num_steps)
819
+ print(' Relative error =', output.rel_error)
820
+ acc = float( '%.6e' % Decimal(output.accuracy) )
821
+ print(' Accuracy = ', acc, '%')
822
+
823
+ return X, Y, Z, output
824
+
825
+
826
+ def rank(T, options=False, plot=True, trials=3):
827
+ """
828
+ This function computes several approximations of T for r = 1...max rank. These computations will be used to
829
+ determine the (most probable) rank of T. The function also returns an array `errors_per_rank` with the relative
830
+ errors for each rank computed. It is relevant to say that the rank r computed can also be the `border rank` of T,
831
+ not the actual rank. The idea is that the minimum of |T - T_approx|, for each rank r, stabilizes when T_approx has
832
+ the same rank as T. This function also plots the graph of the errors so the user are able to visualize the moment
833
+ when the error stabilizes.
834
+
835
+ Inputs
836
+ ------
837
+ T: float array or list
838
+ options: class or bool
839
+ The user can give the options to the cpd function if necessary.
840
+ plot: bool
841
+ If True (default), the program creates a plot with the relation between the error and rank.
842
+ trial: int
843
+ Number of times the program will compute a CPD for the same rank. The idea is to retain only the best one.
844
+
845
+ Outputs
846
+ -------
847
+ final_rank: int
848
+ The computed rank of T.
849
+ errors_per_rank: float 1-D array
850
+ The error |T - T_approx| computed for each rank.
851
+ """
852
+
853
+ # Verify if T is sparse, in which case it will be given as a list with the data.
854
+ if type(T) == list:
855
+ data, idxs, dims = T
856
+ data = array(data, dtype=float64)
857
+ idxs = array(idxs, dtype=uint64)
858
+ dims = array(dims)
859
+ else:
860
+ dims = T.shape
861
+ L = len(dims)
862
+
863
+ # Set options
864
+ options = make_options(options)
865
+
866
+ # START THE PROCESS OF FINDING THE RANK
867
+
868
+ if L > 3:
869
+ if options.method == 'dGN':
870
+ Rmin = 1
871
+ sorted_dims = sort(array(dims))
872
+ Rmax = multiply_dims(sorted_dims[1:])
873
+ else:
874
+ Rmin, Rmax = 2, np.min(dims)
875
+ else:
876
+ m, n, p = dims
877
+ Rmin, Rmax = 1, min(m*n, m*p, n*p)
878
+
879
+ # error_per_rank saves the relative error of the CPD for each rank r.
880
+ error_per_rank = empty(Rmax)
881
+
882
+ print('Start searching for rank')
883
+ print('Stops at R =', Rmax, ' or less')
884
+ print('-----------------------------')
885
+
886
+ for r in range(1, Rmax+1):
887
+ s = "Testing R = " + str(r)
888
+ sys.stdout.write('\r'+s)
889
+
890
+ best_error = inf
891
+ for t in range(trials):
892
+ if L > 3 and r == 1:
893
+ rel_error = 1
894
+ else:
895
+ factors, outputs = cpd(T, r, options)
896
+ rel_error = outputs.rel_error
897
+ if rel_error < best_error:
898
+ best_error = rel_error
899
+
900
+ # Save relative error of this approximation.
901
+ error_per_rank[r-1] = best_error
902
+
903
+ # STOPPING CONDITIONS
904
+
905
+ # Error small enough.
906
+ if best_error < 1e-14:
907
+ final_rank = r
908
+ final_error = error_per_rank[r-1]
909
+ break
910
+ # Difference between errors small enough.
911
+ if r > Rmin:
912
+ if np.abs(error_per_rank[r-1] - error_per_rank[r-2]) < 1e-5:
913
+ if best_error < 0.5:
914
+ final_rank = nanargmin(error_per_rank[0:r])+1
915
+ final_error = error_per_rank[final_rank-1]
916
+ break
917
+ # Error decreased orders of magnitude abruptly.
918
+ if r > 2:
919
+ previous_diff = np.abs(error_per_rank[r-2] - error_per_rank[r-3])
920
+ current_diff = np.abs(error_per_rank[r-1] - error_per_rank[r-2])
921
+ if previous_diff / current_diff > 100:
922
+ if best_error < 0.5:
923
+ final_rank = r-1
924
+ final_error = error_per_rank[r-2]
925
+ break
926
+
927
+ # SAVE LAST INFORMATION
928
+
929
+ error_per_rank = error_per_rank[0:r]
930
+
931
+ # DISPLAY AND PLOT ALL RESULTS
932
+ try:
933
+ print('\nrank(T) =', final_rank)
934
+ print('|T - T_approx|/|T| =', final_error)
935
+ except:
936
+ final_rank = nanargmin(error_per_rank[0:r])+1
937
+ final_error = error_per_rank[final_rank-1]
938
+ print('\nrank(T) =', final_rank)
939
+ print('|T - T_approx|/|T| =', final_error)
940
+
941
+ if plot:
942
+ plt.figure(figsize=[14, 4])
943
+ plt.plot(range(1, r+1), error_per_rank, color='blue')
944
+ plt.plot(range(1, r+1), error_per_rank, 's', color='blue')
945
+ plt.plot(final_rank, final_error, marker='s', color='red')
946
+ plt.xlabel('Rank')
947
+ plt.ylabel('Relative error')
948
+ plt.yscale('log')
949
+ if r > 20:
950
+ plt.xticks(range(1, r+1, int((r+1)/20)))
951
+ else:
952
+ plt.xticks(range(1, r+1))
953
+ plt.grid()
954
+ plt.show()
955
+
956
+ return int(final_rank), error_per_rank
957
+
958
+
959
+ def stats(T, R, options=False, num_samples=100):
960
+ """
961
+ This function makes several calls of the Gauss-Newton function with random initial points. Each call turns into a
962
+ sample to recorded so we can make statistics estimate. By default this functions takes 100 samples to analyze. The
963
+ user may choose the number of samples the program makes, but the computational time may be very costly. Also, the
964
+ user may choose the maximum number of iterations and the tolerance to be used in each Gauss-Newton function. The
965
+ outputs plots with general information about all the trials. These information are the following:
966
+ - The total time spent in each trial.
967
+ - The number of steps used in each trial.
968
+ - The relative error |T - T_approx|/|T| obtained in each trial.
969
+
970
+ Inputs
971
+ ------
972
+ T: float array or list
973
+ R: int
974
+ The desired rank of the approximating tensor.
975
+ options: class or bool
976
+ The user can give the options to the cpd function if necessary.
977
+ num_samples: int
978
+ Total of CPD's we want to compute to make statistics. Default is 100.
979
+
980
+ Outputs
981
+ -------
982
+ times, steps, errors: arrays
983
+ Each times[i], steps[i], errors[i] correspond to the time spent, number of steps and error of the i-th trial.
984
+ """
985
+
986
+ # Verify if T is sparse, in which case it will be given as a list with the data.
987
+ if type(T) == list:
988
+ data, idxs, dims = T
989
+ data = array(data, dtype=float64)
990
+ idxs = array(idxs, dtype=uint64)
991
+ dims = array(dims)
992
+ else:
993
+ dims = T.shape
994
+ L = len(dims)
995
+
996
+ # Set options
997
+ options = make_options(options)
998
+
999
+ # INITIALIZE RELEVANT ARRAYS
1000
+
1001
+ times = empty(num_samples)
1002
+ steps = empty(num_samples)
1003
+ errors = empty(num_samples)
1004
+
1005
+ # BEGINNING OF SAMPLING AND COMPUTING
1006
+
1007
+ # At each run, the program computes a CPD for T with random guess for initial point.
1008
+ for trial in range(1, num_samples+1):
1009
+ start = time.time()
1010
+ factors, outputs = cpd(T, R, options)
1011
+ end = time.time()
1012
+
1013
+ # Update info.
1014
+ rel_error = outputs.rel_error
1015
+ num_steps = outputs.num_steps
1016
+ times[trial-1] = end - start
1017
+ steps[trial-1] = num_steps
1018
+ errors[trial-1] = rel_error
1019
+
1020
+ # Display progress bar.
1021
+ x = 100*trial//num_samples
1022
+ s = "[" + x*"=" + (100-x)*" " + "]" + " " + str( np.round(100*trial/num_samples, 2) ) + "%"
1023
+ sys.stdout.write('\r'+s)
1024
+
1025
+ # PLOT HISTOGRAMS
1026
+
1027
+ [array, bins, patches] = plt.hist(times, 50, edgecolor='black')
1028
+ plt.xlabel('Seconds')
1029
+ plt.ylabel('Quantity')
1030
+ plt.title('Histogram of the total time of each trial')
1031
+ plt.show()
1032
+
1033
+ [array, bins, patches] = plt.hist(steps, 50, edgecolor='black')
1034
+ plt.xlabel('Number of steps')
1035
+ plt.ylabel('Quantity')
1036
+ plt.title('Histogram of the number of steps of each trial')
1037
+ plt.show()
1038
+
1039
+ [array, bins, patches] = plt.hist(log10(errors), 50, edgecolor='black')
1040
+ plt.xlabel(r'$\log_{10} \|T - \tilde{T}\|/\|T\|$')
1041
+ plt.ylabel('Quantity')
1042
+ plt.title('Histogram of the log10 of the relative error of each trial')
1043
+ plt.show()
1044
+
1045
+ return times, steps, errors
1046
+
1047
+
1048
+ def cpdtt(T, R):
1049
+ """
1050
+ Function to compute the tensor train cores of T with specific format to obtain the CPD of T. This tensor train
1051
+ follows the format dims[0] x R -> R x dims[1] x R -> ... -> R x dims[L-2] x R -> R x dims[L-1].
1052
+ """
1053
+
1054
+ # Compute dimensions and norm of T.
1055
+ dims = array(T.shape)
1056
+ L = len(dims)
1057
+
1058
+ # List of cores.
1059
+ G = []
1060
+
1061
+ # Compute remaining cores, except for the last one.
1062
+ r1, r2 = 1, R
1063
+ V = T
1064
+ for l in range(0, L-1):
1065
+ V, g = tt_core(V, dims, r1, r2, l)
1066
+ r1, r2 = R, R
1067
+ G.append(g)
1068
+
1069
+ # Last core.
1070
+ G.append(V)
1071
+
1072
+ return G
1073
+
1074
+
1075
+ def foxit(T, R, options=False, bestof=1):
1076
+ """
1077
+ This is a special function made for the convenience of the user, i.e., this function makes the following:
1078
+ 1) computes the desired CPD with the requested options
1079
+ 2) prints the relevant results on the screen
1080
+ 3) prints the parameters used
1081
+ 4) plots the evolution of the step sizes, errors, improvements and gradients
1082
+
1083
+ Additionally, the extra option 'bestof' tells the program to compute a certain number of CPD's and retain only the
1084
+ best one.
1085
+ """
1086
+
1087
+ best_error = inf
1088
+ if type(T) == list:
1089
+ data, idxs, dims = T
1090
+ data = array(data, dtype=float64)
1091
+ idxs = array(idxs, dtype=uint64)
1092
+ dims = array(dims)
1093
+ else:
1094
+ dims = T.shape
1095
+ L = len(dims)
1096
+ options = make_options(options)
1097
+
1098
+ for i in range(bestof):
1099
+ factors, outputs = cpd(T, R, options)
1100
+ if outputs.rel_error < best_error:
1101
+ best_error = outputs.rel_error
1102
+ best_factors = deepcopy(factors)
1103
+ best_outputs = deepcopy(outputs)
1104
+
1105
+ print('Final results')
1106
+ print(' Number of steps =', best_outputs.num_steps)
1107
+ print(' Relative error =', best_outputs.rel_error)
1108
+ acc = float( '%.6e' % Decimal(best_outputs.accuracy) )
1109
+ print(' Accuracy = ', acc, '%')
1110
+ print()
1111
+ print('==========================================================================')
1112
+ print()
1113
+ print('Parameters used')
1114
+ print(' initialization:', options.initialization)
1115
+ print(' maximum of iterations:', options.maxiter)
1116
+ print(' error tolerance:', options.tol)
1117
+ print(' steps size tolerance:', options.tol_step)
1118
+ print(' improvement tolerance:', options.tol_improv)
1119
+ print(' gradient norm tolerance:', options.tol_grad)
1120
+ print(' inner algorithm parameters:')
1121
+ if options.method == 'als':
1122
+ print(' method: alternating least squares')
1123
+ elif options.method == 'ttcpd':
1124
+ print(' method: tensor train cpd')
1125
+ elif options.inner_method == 'cg_static':
1126
+ print(' method: conjugate gradient static')
1127
+ print(' cg maximum of iterations:', options.cg_maxiter)
1128
+ print(' cg tolerance:', options.cg_tol)
1129
+ elif options.inner_method == 'cg':
1130
+ print(' method: conjugate gradient dynamic/random')
1131
+ print(' cg factor:', options.cg_factor)
1132
+ print(' cg tolerance:', options.cg_tol)
1133
+ elif options.inner_method == 'direct':
1134
+ print(' method: direct solver')
1135
+ elif type(options.inner_method) == list:
1136
+ print(' method: hybrid strategy')
1137
+ print()
1138
+
1139
+ plt.figure(figsize=[9, 6])
1140
+ if options.refine:
1141
+
1142
+ # sz1 is the size of the arrays of the main stage.
1143
+ sz1 = best_outputs.step_sizes[0].size
1144
+ x1 = arange(sz1)
1145
+ # sz2 is the size of the arrays of the refinement stage.
1146
+ sz2 = best_outputs.step_sizes[1].size
1147
+ x2 = arange(sz1-1, sz1 + sz2 - 1)
1148
+
1149
+ # Step sizes
1150
+ plt.plot(x1, best_outputs.step_sizes[0], 'k-', markersize=2, label='Step sizes - Main')
1151
+ plt.plot(x2, best_outputs.step_size[1], 'k--', markersize=2, label='Step sizes - Refinement')
1152
+
1153
+ # Errors
1154
+ plt.plot(x1, best_outputs.errors[0], 'b-', markersize=2, label='Relative errors - Main')
1155
+ plt.plot(x2, best_outputs.errors[1], 'b--', markersize=2, label='Relative errors - Refinement')
1156
+
1157
+ # Improvements
1158
+ plt.plot(x1, best_outputs.improv[0], 'g-', markersize=2, label='Improvements - Main')
1159
+ plt.plot(x2, best_outputs.improv[1], 'g--', markersize=2, label='Improvements - Refinement')
1160
+
1161
+ # Gradients
1162
+ plt.plot(x1, best_outputs.gradients[0], 'r-', markersize=2, label='Gradients - Main')
1163
+ plt.plot(x2, best_outputs.gradients[1], 'r--', markersize=2, label='Gradients - Refinement')
1164
+
1165
+ plt.xlabel('iteration')
1166
+ plt.yscale('log')
1167
+ plt.grid()
1168
+ plt.legend()
1169
+ plt.show()
1170
+
1171
+ else:
1172
+ # sz1 is the size of the arrays of the main stage.
1173
+ sz1 = best_outputs.step_sizes[0].size
1174
+ x1 = arange(sz1)
1175
+
1176
+ # Step sizes
1177
+ plt.plot(x1, best_outputs.step_sizes[0], 'k-', markersize=2, label='Step sizes - Main')
1178
+
1179
+ # Errors
1180
+ plt.plot(x1, best_outputs.errors[0], 'b-', markersize=2, label='Relative errors - Main')
1181
+
1182
+ # Improvements
1183
+ plt.plot(x1, best_outputs.improv[0], 'g-', markersize=2, label='Improvements - Main')
1184
+
1185
+ # Gradients
1186
+ plt.plot(x1, best_outputs.gradients[0], 'r-', markersize=2, label='Gradients - Main')
1187
+
1188
+ plt.xlabel('iteration')
1189
+ plt.yscale('log')
1190
+ plt.grid()
1191
+ plt.legend()
1192
+ plt.show()
1193
+
1194
+ return best_factors, best_outputs