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/Auxiliar.py ADDED
@@ -0,0 +1,686 @@
1
+ """
2
+ Auxiliar Module
3
+ ===============
4
+ This module is composed by minor functions, designed to work on very specific tasks. Some of them may be useful for the
5
+ user to use directly, but most of them are just some piece of another (and more important) function.
6
+ """
7
+
8
+ # Python modules
9
+ import sys
10
+ import time
11
+ from numpy import diag, dot, argsort, array, size, inf, moveaxis, arange, ndarray, unique, prod, uint64
12
+ from numpy.linalg import norm, pinv
13
+ from numpy.random import randn, randint
14
+ import scipy.io
15
+ from sklearn.utils.extmath import randomized_svd as rand_svd
16
+ from itertools import product
17
+
18
+ # Tensor Fox modules
19
+ import TensorFox.Critical as crt
20
+ import TensorFox.MultilinearAlgebra as mlinalg
21
+ import TensorFox.TensorFox as tfx
22
+
23
+
24
+ def consistency(R, dims, options):
25
+ """
26
+ This function checks the validity of rank and dimensions before anything is done in the program.
27
+ """
28
+
29
+ L = len(dims)
30
+
31
+ # Check if order is not higher than 12.
32
+ if L > 12:
33
+ sys.exit('Tensor Fox does not work with tensors of order higher than 12.')
34
+
35
+ # If some dimension is equal to 1, the user may just use classical SVD with numpy.
36
+ # We won't address this situation here.
37
+ for l in range(L):
38
+ if dims[l] == 1:
39
+ sys.exit('At least one dimension is equal to 1. This situation not supported by Tensor Fox.')
40
+
41
+ # Consistency of rank value.
42
+ if (type(R) != int) or (R < 1):
43
+ sys.exit('Rank must be a positive integer.')
44
+
45
+ # Check if rank is well defined in the third order case.
46
+ if L == 3:
47
+ m, n, p = int(dims[0]), int(dims[1]), int(dims[2])
48
+ if R > min(m*n, m*p, n*p):
49
+ msg = 'Rank must satisfy 1 <= rank <= min(m*n, m*p, n*p) = ' + str(min(m*n, m*p, n*p)) + '.'
50
+ sys.exit(msg)
51
+
52
+ if L > 3 and R == 1:
53
+ msg = 'Rank must be greater than 1 for tensor with order greater than 3.'
54
+ sys.exit(msg)
55
+
56
+ if L > 3 and R > min(dims) and options.method == 'ttcpd':
57
+ print("-> The CPD should not be computed with the Tensor Train method when the order is higher than 3 and the rank is greater than any dimension of the tensor.\n"
58
+ "The computations can be slower and the program may not converge to a good solution.\n"
59
+ "We recommend to use the parameter method='dGN' (within the class of options, see the cpd docstring).",
60
+ file=sys.stderr)
61
+
62
+ if options.symm:
63
+ for i in range(L):
64
+ for j in range(L):
65
+ if dims[i] != dims[j]:
66
+ msg = 'Symmetric tensors must have equal dimensions.'
67
+ sys.exit(msg)
68
+
69
+ if options.method != 'dGN' and options.method != 'als' and options.method != 'ttcpd':
70
+ msg = "Wrong method name. Must be 'dGN', 'als' or 'ttcpd'."
71
+ sys.exit(msg)
72
+
73
+ if options.display > 2:
74
+ print('-> With display > 2 Tensor Fox will compute the error of the MLSVD. For large tensors this routine is very clostly.', file=sys.stderr)
75
+
76
+ time.sleep(1)
77
+
78
+ return
79
+
80
+
81
+ def tens2matlab(T, filename):
82
+ """
83
+ This function creates a matlab file containing the tensor T. The parameter filename should be a string.
84
+ Note that it is not necessary to include the extension of the file.
85
+ """
86
+
87
+ # Save the tensor in matlab format.
88
+ scipy.io.savemat(filename + '.mat', {filename: T})
89
+
90
+ return
91
+
92
+
93
+ def sort_dims(T):
94
+ """
95
+ Change the axis of T in decreasing order. This can speed up the mlsvd function.
96
+ """
97
+
98
+ # Sparse tensor.
99
+ if type(T) == list:
100
+ data, idxs, dims = T
101
+ idxs = array(idxs, dtype=uint64)
102
+ dims = array(dims)
103
+ ordering = argsort(-dims)
104
+ dims = tuple(dims[ordering])
105
+ idxs = idxs[:, ordering]
106
+ T_sorted = [data, idxs, dims]
107
+
108
+ # Dense tensor.
109
+ else:
110
+ dims = array(T.shape)
111
+ L = len(dims)
112
+ ordering = argsort(-dims)
113
+ T_sorted = moveaxis(T, ordering, arange(-L, 0))
114
+
115
+ return T_sorted, ordering
116
+
117
+
118
+ def unsort_dims(factors, ordering):
119
+ """
120
+ Put the CPD factors to their original dimension ordering.
121
+ """
122
+
123
+ L = len(factors)
124
+ new_factors = [[] for l in range(L)]
125
+ for l in range(L):
126
+ new_factors[ordering[l]] = factors[l]
127
+
128
+ return new_factors
129
+
130
+
131
+ def output_info(T1, Tsize, T1_approx,
132
+ step_sizes_main, step_sizes_refine,
133
+ errors_main, errors_refine,
134
+ improv_main, improv_refine,
135
+ gradients_main, gradients_refine,
136
+ stop_main, stop_refine,
137
+ options):
138
+ """
139
+ Constructs the class containing the information of all relevant outputs relative to the computation of a third order
140
+ CPD.
141
+ """
142
+
143
+ if options.refine:
144
+ num_steps = size(step_sizes_main) + size(step_sizes_refine)
145
+ else:
146
+ num_steps = size(step_sizes_main)
147
+
148
+ if options.refine:
149
+ rel_error = errors_refine[-1]
150
+ elif type(T1) == ndarray:
151
+ rel_error = crt.fastnorm(T1, T1_approx)/Tsize
152
+
153
+ # In the sparse case, the variable T1 is the triple T = [data, idxs, dims] and T1_approx is the variable factors.
154
+ # We keep the original variable names used for the dense case but this distinction is important to know.
155
+ else:
156
+ data, idxs, dims = T1
157
+ idxs = array(idxs)
158
+ factors = T1_approx
159
+ rel_error = crt.sparse_fastnorm(data, idxs, dims, factors)/Tsize
160
+
161
+ class output:
162
+ def __init__(self):
163
+ self.num_steps = num_steps
164
+ self.rel_error = rel_error
165
+ self.accuracy = max(0, 100*(1 - rel_error))
166
+ self.step_sizes = [step_sizes_main, step_sizes_refine]
167
+ self.errors = [errors_main, errors_refine]
168
+ self.improv = [improv_main, improv_refine]
169
+ self.gradients = [gradients_main, gradients_refine]
170
+ self.stop = [stop_main, stop_refine]
171
+ self.conditioning = options.conditioning
172
+ self.options = options
173
+
174
+ def stop_msg(self):
175
+ # stop_main message
176
+ print()
177
+ print('Main stop:')
178
+ if self.stop[0] == 0:
179
+ print('0 - Relative error is small enough.')
180
+ if self.stop[0] == 1:
181
+ print('1 - Steps are small enough.')
182
+ if self.stop[0] == 2:
183
+ print('2 - Improvement in the relative error is small enough.')
184
+ if self.stop[0] == 3:
185
+ print('3 - Gradient is small enough.')
186
+ if self.stop[0] == 4:
187
+ print('4 - Average error increased.')
188
+ if self.stop[0] == 5:
189
+ print('5 - Limit of iterations was reached.')
190
+ if self.stop[0] == 6:
191
+ print('6 - dGN diverged.')
192
+ if self.stop[0] == 7:
193
+ print('7 - Average improvement is too small compared to the average error.')
194
+
195
+ # stop_refine message
196
+ print()
197
+ print('Refinement stop:')
198
+ if self.stop[1] == 0:
199
+ print('0 - Relative error is small enough.')
200
+ if self.stop[1] == 1:
201
+ print('1 - Steps are small enough.')
202
+ if self.stop[1] == 2:
203
+ print('2 - Improvement in the relative error is small enough.')
204
+ if self.stop[1] == 3:
205
+ print('3 - Gradient is small enough.')
206
+ if self.stop[1] == 4:
207
+ print('4 - Average error increased.')
208
+ if self.stop[1] == 5:
209
+ print('5 - Limit of iterations was reached.')
210
+ if self.stop[1] == 6:
211
+ print('6 - dGN diverged.')
212
+ if self.stop[1] == 7:
213
+ print('7 - Average improvement is too small compared to the average error.')
214
+ if self.stop[1] == 8:
215
+ print('8 - No refinement was performed.')
216
+
217
+ return ''
218
+
219
+ output = output()
220
+
221
+ return output
222
+
223
+
224
+ def make_final_outputs(num_steps, rel_error, accuracy, outputs, options):
225
+ """
226
+ Constructs the class containing the information of all relevant outputs relative to the computation of a high order
227
+ CPD.
228
+ """
229
+
230
+ class temp_outputs:
231
+ def __init__(self):
232
+ self.num_steps = num_steps
233
+ self.rel_error = rel_error
234
+ self.accuracy = accuracy
235
+ self.cpd_output = outputs
236
+ self.options = options
237
+
238
+ final_outputs = temp_outputs()
239
+
240
+ return final_outputs
241
+
242
+
243
+ def make_options(options):
244
+ """
245
+ This function constructs the whole class of options based on the options the user requested.
246
+ This is the format read by the program.
247
+
248
+ Some observations about the CG parameters:
249
+ - inner_method is the name of the method used to compute each iteration, the choices are 'cg', 'cg_static',
250
+ 'als' and 'direct'.
251
+ - cg_maxiter is the maximum number of iterations for 'cg_static'.
252
+ - cg_factor is the multiplying factor for the 'cg' method.
253
+ - cg_tol is the tolerance error to stop the iterations of the inner method.
254
+ """
255
+
256
+ # Initialize default options.
257
+ class temp_options:
258
+ def __init__(self):
259
+ self.maxiter = 200
260
+ self.tol = 1e-16
261
+ self.tol_step = 1e-6
262
+ self.tol_improv = 1e-6
263
+ self.tol_grad = 1e-6
264
+ self.tol_jump = 10
265
+ self.method = 'dGN'
266
+ self.inner_method = 'cg'
267
+ self.cg_maxiter = 100
268
+ self.cg_factor = 1
269
+ self.cg_tol = 1e-16
270
+ self.bi_method_parameters = ['als', 500, 1e-6]
271
+ self.initialization = 'random'
272
+ self.trunc_dims = 0
273
+ self.mlsvd_method = 'seq'
274
+ self.tol_mlsvd = 1e-16
275
+ self.init_damp = 1
276
+ self.refine = False
277
+ self.symm = False
278
+ self.factors_norm = 0
279
+ self.trials = 3
280
+ self.display = 0
281
+ self.epochs = 1
282
+ self.gpu = False
283
+ self.mkl_dot = True
284
+ self.svd_n_components = None
285
+ self.conditioning = []
286
+
287
+ temp_options = temp_options()
288
+
289
+ # User defined options.
290
+ if 'maxiter' in dir(options):
291
+ temp_options.maxiter = options.maxiter
292
+ if 'tol' in dir(options):
293
+ temp_options.tol = options.tol
294
+ if 'tol_step' in dir(options):
295
+ temp_options.tol_step = options.tol_step
296
+ if 'tol_improv' in dir(options):
297
+ temp_options.tol_improv = options.tol_improv
298
+ if 'tol_grad' in dir(options):
299
+ temp_options.tol_grad = options.tol_grad
300
+ if 'tol_jump' in dir(options):
301
+ temp_options.tol_jump = options.tol_jump
302
+ if 'method' in dir(options):
303
+ temp_options.method = options.method
304
+
305
+ if 'inner_method' in dir(options):
306
+ temp_options.inner_method = options.inner_method
307
+ if 'cg_maxiter' in dir(options):
308
+ temp_options.cg_maxiter = options.cg_maxiter
309
+ if 'cg_factor' in dir(options):
310
+ temp_options.cg_factor = options.cg_factor
311
+ if 'cg_tol' in dir(options):
312
+ temp_options.cg_tol = options.cg_tol
313
+
314
+ if 'bi_method' in dir(options):
315
+ temp_options.bi_method_parameters[0] = options.bi_method
316
+ # Set default maxiter for each possible algorithm (bicpd).
317
+ if options.bi_method == 'cg':
318
+ temp_options.bi_method_parameters[1] = 1
319
+ elif options.bi_method == 'cg_static':
320
+ temp_options.bi_method_parameters[1] = 100
321
+ elif options.bi_method == 'als':
322
+ temp_options.bi_method_parameters[1] = 500
323
+ if 'bi_method_maxiter' in dir(options):
324
+ temp_options.bi_method_parameters[1] = options.bi_method_maxiter
325
+ if 'bi_method_tol' in dir(options):
326
+ temp_options.bi_method_parameters[2] = options.bi_method_tol
327
+
328
+ if 'initialization' in dir(options):
329
+ temp_options.initialization = options.initialization
330
+ if 'trunc_dims' in dir(options):
331
+ temp_options.trunc_dims = options.trunc_dims
332
+ if 'mlsvd_method' in dir(options):
333
+ temp_options.mlsvd_method = options.mlsvd_method
334
+ if 'tol_mlsvd' in dir(options):
335
+ temp_options.tol_mlsvd = options.tol_mlsvd
336
+ if 'init_damp' in dir(options):
337
+ temp_options.init_damp = options.init_damp
338
+ if 'refine' in dir(options):
339
+ temp_options.refine = options.refine
340
+ if 'symm' in dir(options):
341
+ temp_options.symm = options.symm
342
+ if 'factors_norm' in dir(options):
343
+ temp_options.factors_norm = options.factors_norm
344
+ if 'trials' in dir(options):
345
+ temp_options.trials = options.trials
346
+ if 'display' in dir(options):
347
+ temp_options.display = options.display
348
+ if 'epochs' in dir(options):
349
+ temp_options.epochs = options.epochs
350
+ if 'gpu' in dir(options):
351
+ temp_options.gpu = options.gpu
352
+ if 'mkl_dot' in dir(options):
353
+ temp_options.mkl_dot = options.mkl_dot
354
+ if 'svd_n_components' in dir(options):
355
+ temp_options.svd_n_components = options.svd_n_components
356
+ if 'conditioning' in dir(options):
357
+ temp_options.conditioning = options.conditioning
358
+
359
+ # If gpu is True, the variable mlsvd_method is set to 'gpu', which is a special strategy aiming to minimize the
360
+ # memory size of the data passed to the GPU. This strategy is based on the classic MLSVD method. In the case the
361
+ # user wants to use a specific MLSVD method, then the variable mlsvd_method should be passed to the function cpd.
362
+ # This is only valid for dense tensors. Sparse tensors has its own strategy which isn't affect by mlsvd_method.
363
+ if temp_options.gpu and 'mlsvd_method' not in dir(options):
364
+ temp_options.mlsvd_method = 'gpu'
365
+
366
+ return temp_options
367
+
368
+
369
+ def tt_core(V, dims, r1, r2, l):
370
+ """
371
+ Computation of one core of the CPD Tensor Train function (cpdtt).
372
+ """
373
+
374
+ V = V.reshape(r1*dims[l], mlinalg.multiply_dims(dims[l+1:]), order='F')
375
+ low_rank = min(V.shape[0], V.shape[1])
376
+ U, S, V = rand_svd(V, low_rank, n_iter=0)
377
+ U = U[:, :r2]
378
+ S = diag(S)
379
+ V = dot(S, V)
380
+ V = V[:r2, :]
381
+ if r1 == 1:
382
+ g = U.reshape(dims[l], r2, order='F')
383
+ else:
384
+ g = U.reshape(r1, dims[l], r2, order='F')
385
+ return V, g
386
+
387
+
388
+ def tt_error(T, G, dims, L):
389
+ """
390
+ Given a tensor T and a computed CPD Tensor Train G = (G1,...,GL), this function computes the error between T and the
391
+ tensor associated to G.
392
+ """
393
+
394
+ if L == 3:
395
+ G0, G1, G2 = G
396
+ T_approx = crt.tt_error_order3(T, G0, G1, G2, dims, L)
397
+ if L == 4:
398
+ G0, G1, G2, G3 = G
399
+ T_approx = crt.tt_error_order4(T, G0, G1, G2, G3, dims, L)
400
+ if L == 5:
401
+ G0, G1, G2, G3, G4 = G
402
+ T_approx = crt.tt_error_order5(T, G0, G1, G2, G3, G4, dims, L)
403
+ if L == 6:
404
+ G0, G1, G2, G3, G4, G5 = G
405
+ T_approx = crt.tt_error_order6(T, G0, G1, G2, G3, G4, G5, dims, L)
406
+ if L == 7:
407
+ G0, G1, G2, G3, G4, G5, G6 = G
408
+ T_approx = crt.tt_error_order7(T, G0, G1, G2, G3, G4, G5, G6, dims, L)
409
+ if L == 8:
410
+ G0, G1, G2, G3, G4, G5, G6, G7 = G
411
+ T_approx = crt.tt_error_order8(T, G0, G1, G2, G3, G4, G5, G6, G7, dims, L)
412
+ if L == 9:
413
+ G0, G1, G2, G3, G4, G5, G6, G7, G8 = G
414
+ T_approx = crt.tt_error_order9(T, G0, G1, G2, G3, G4, G5, G6, G7, G8, dims, L)
415
+ if L == 10:
416
+ G0, G1, G2, G3, G4, G5, G6, G7, G8, G9 = G
417
+ T_approx = crt.tt_error_order10(T, G0, G1, G2, G3, G4, G5, G6, G7, G8, G9, dims, L)
418
+ if L == 11:
419
+ G0, G1, G2, G3, G4, G5, G6, G7, G8, G9, G10 = G
420
+ T_approx = crt.tt_error_order11(T, G0, G1, G2, G3, G4, G5, G6, G7, G8, G9, G10, dims, L)
421
+ if L == 12:
422
+ G0, G1, G2, G3, G4, G5, G6, G7, G8, G9, G10, G11 = G
423
+ T_approx = crt.tt_error_order12(T, G0, G1, G2, G3, G4, G5, G6, G7, G8, G9, G10, G11, dims, L)
424
+
425
+ error = norm(T - T_approx)/norm(T)
426
+ return error
427
+
428
+
429
+ def cpd_cores(G, max_trials, epochs, R, display, options):
430
+ """
431
+ Routines to compute the cores of the CPD tensor train.
432
+ """
433
+
434
+ L = len(G)
435
+
436
+ # The number of epochs is increased in 1 if necessary to be odd.
437
+ if epochs % 2 == 0:
438
+ epochs += 1
439
+
440
+ # List of CPD's.
441
+ cpd_list = [l for l in range(L-2)]
442
+
443
+ # Outputs is a list containing the output class of each CPD.
444
+ outputs = [l for l in range(L-2)]
445
+
446
+ if display < 0 and epochs > 1:
447
+ print('Epoch ', 1)
448
+
449
+ # Compute cpd of second core.
450
+ best_error = inf
451
+ for trial in range(max_trials):
452
+ if display > 0:
453
+ print()
454
+ print('CPD 1')
455
+ factors, output = tfx.tricpd(G[1], R, options)
456
+ X, Y, Z = factors
457
+ if output.rel_error < best_error:
458
+ best_output = output
459
+ best_error = output.rel_error
460
+ best_X, best_Y, best_Z = X, Y, Z
461
+ if best_error < 1e-4:
462
+ break
463
+
464
+ outputs[0] = best_output
465
+ cpd_list[0] = [best_X, best_Y, best_Z]
466
+
467
+ if display < 0:
468
+ print('CPD 1 error =', best_error)
469
+
470
+ low = 2
471
+ upp = L - 2
472
+
473
+ for epoch in range(epochs):
474
+
475
+ if display < 0 < epoch:
476
+ print()
477
+ print('Epoch ', epoch+1)
478
+
479
+ # Following the tensor train from G[1] to G[L-2].
480
+ if epoch % 2 == 0:
481
+ for l in range(low, L-1):
482
+ best_error = inf
483
+ fixed_X = pinv(best_Z.T)
484
+ for trial in range(max_trials):
485
+ if display > 0:
486
+ print()
487
+ print('CPD', l)
488
+ X, Y, Z, output = tfx.bicpd(G[l], R, [fixed_X, 0], options)
489
+ if output.rel_error < best_error:
490
+ best_output = output
491
+ best_error = output.rel_error
492
+ best_X, best_Y, best_Z = fixed_X, Y, Z
493
+ if best_error < 1e-4:
494
+ break
495
+
496
+ if epoch == epochs-1:
497
+ outputs[l-1] = best_output
498
+ cpd_list[l-1] = [fixed_X, best_Y, best_Z]
499
+
500
+ if display < 0:
501
+ print('CPD', l, 'error =', best_error)
502
+
503
+ # Following the tensor train backwards, from G[L-2] to G[L].
504
+ else:
505
+ # low and upp must be different for the third order case.
506
+ if L == 3:
507
+ low = 1
508
+ upp = L - 1
509
+ for l in reversed(range(1, upp)):
510
+ best_error = inf
511
+ fixed_Z = pinv(best_X.T)
512
+ for trial in range(max_trials):
513
+ if display > 0:
514
+ print()
515
+ print('CPD', l)
516
+ X, Y, Z, output = tfx.bicpd(G[l], R, [fixed_Z, 2], options)
517
+ if output.rel_error < best_error:
518
+ best_output = output
519
+ best_error = output.rel_error
520
+ best_X, best_Y, best_Z = X, Y, fixed_Z
521
+ if best_error < 1e-4:
522
+ break
523
+
524
+ if epoch == epochs-2:
525
+ outputs[l-1] = best_output
526
+ cpd_list[l-1] = [best_X, best_Y, fixed_Z]
527
+
528
+ if display < 0:
529
+ print('CPD', l, 'error =', best_error)
530
+
531
+ return cpd_list, outputs, best_Z
532
+
533
+
534
+ def gen_rand_tensor(dims, R, noise=0):
535
+ """
536
+ This function generates a random rank-R tensor T of shape (dims[0], dims[1], ..., dims[L-1]), where L is the order
537
+ of T. Each factor matrix of T is a matrix of shape (dims[l], R) with its entries drawn from the standard Gaussian
538
+ distribution (mean zero and variance one).
539
+ Let W[l] be the l-th factor matrix of T, then T = (W[0], W[1], ..., W[L-1])*I, where I is a diagonal tensor of shape
540
+ R x R x... x R (L times).
541
+
542
+ Input
543
+ -----
544
+ dims: tuple or list of ints
545
+ The dimensions of the tensor
546
+ R: int
547
+ The rank of the tensor (must satisfy R < min(dims))
548
+ noise: float
549
+ Size of the noise added to the original tensor. Default is without noise.
550
+
551
+ Output
552
+ ------
553
+ T: float L-D array
554
+ The tensor in coordinate format.
555
+ T_noise: float L-D array
556
+ Noisy tensor in coordinate format.
557
+ orig_factors: list
558
+ List of the factor matrices of T. We have that orig_factors[l] = W[l], as described above.
559
+ """
560
+
561
+ L = len(dims)
562
+ orig_factors = []
563
+
564
+ for l in range(L):
565
+ M = randn(dims[l], R)
566
+ orig_factors.append(M)
567
+
568
+ T = tfx.cnv.cpd2tens(orig_factors)
569
+
570
+ if noise != 0:
571
+ E = noise * randn(*dims)
572
+ T_noise = T + E
573
+ return T, T_noise, orig_factors
574
+
575
+ else:
576
+ return T, orig_factors
577
+
578
+
579
+ def gen_rand_sparse_tensor(dims, R, nnz):
580
+ """
581
+ This function generates a sparse random rank-R tensor T of shape (dims[0], dims[1], ..., dims[L-1]), where L is the
582
+ order of T. Each factor matrix of T is a sparse matrix of shape (dims[l], R) such that its entries are drawn from
583
+ the standard Gaussian distribution (mean zero and variance one). The sparse representation of the factors matrices
584
+ (orig_factors) is given by a dictionary where the keys are the coordinates associated to the nonzero values of the
585
+ matrix. T is given in the sparse format [data, idxs, dims] as described in the cpd function.
586
+
587
+ Input
588
+ -----
589
+ dims: tuple or list of ints
590
+ The dimensions of the tensor
591
+ R: int
592
+ The rank of the tensor (must satisfy R < min(dims))
593
+ nnz: int
594
+ Number of nonzero entries of each factor matrix. Usually the tensor will have more nonzero entries.
595
+
596
+ Output
597
+ ------
598
+ data: float 1-D array
599
+ data[i] is the nonzero value of the tensor at index idxs[i, :].
600
+ idxs: int 2-D array
601
+ Let nnz be the number of nonzero entries of the tensor. Then idxs is an array
602
+ of shape (nnz, L) such that idxs[i, :] is the index of the i-th nonzero entry.
603
+ dims: list or tuple
604
+ The dimensions (shape) of the tensor.
605
+ orig_factors: list
606
+ List of the factor matrices of the tensor. Each matrix is sparse and defined by a dictionary such that each key
607
+ is a tuple with a nonzero coordinate and the corresponding value is the nonzero value of the matrix. Along with
608
+ the dictionary there is a tuple with the dimensions of the matrix.
609
+ """
610
+
611
+ L = len(dims)
612
+
613
+ # Generate factors.
614
+ orig_factors = []
615
+ for l in range(L):
616
+ # Create the nonzero values of each factor.
617
+ data = randn(nnz)
618
+
619
+ # Create indexes.
620
+ rows = randint(0, dims[l], size=nnz)
621
+ cols = randint(0, R, size=nnz)
622
+ idxs = array([[rows[i], cols[i]] for i in range(nnz)])
623
+
624
+ # Remove duplicates and update data accordingly.
625
+ idxs = unique(idxs, axis=0)
626
+ data = data[:idxs.shape[0]]
627
+
628
+ # Define sparse factors.
629
+ orig_factors.append({tuple(idxs[i, :]): d for i, d in enumerate(data)})
630
+
631
+ # Define sparse representation of the tensor in the format data, idxs, dims.
632
+ idxs = []
633
+ for l in range(L):
634
+ idxs.append([])
635
+ for idx in orig_factors[l].keys():
636
+ idxs[l].append(idx[0])
637
+
638
+ idxs = list(set(product(*(idx for idx in idxs))))
639
+
640
+ data = []
641
+ for j in idxs:
642
+ tmp = 0
643
+ for r in range(R):
644
+ p = 1
645
+ for l in range(L):
646
+ if (j[l], r) in orig_factors[l].keys():
647
+ p *= orig_factors[l][j[l], r]
648
+ else:
649
+ p = 0
650
+ break
651
+ tmp += p
652
+ data.append(tmp)
653
+
654
+ # Examine the values to remove possible remaining zeros.
655
+ for i in reversed(range(len(data))):
656
+ if data[i] == 0:
657
+ del data[i]
658
+ del idxs[i]
659
+
660
+ return array(data), array(idxs), array(dims), orig_factors
661
+
662
+
663
+ def sparse2txt(idxs, data, filename, start=1):
664
+ """
665
+ Converts a sparse tensor in text format and saves it. The format is such that
666
+ each row contains a sequence of the index values and the tensor value.
667
+
668
+ Input
669
+ -----
670
+ idxs, data: float 1-D arrays
671
+ The same arrays of the sparse tensor used in Tensor Fox.
672
+ filename: str
673
+ The name of the text file where the tensor wil be saved.
674
+ start: int
675
+ In Tensor Fox the indexes starts at zero, but the same may not be true for
676
+ other applications where the text file will be used. This parameter is a
677
+ value to be added on the indexes.
678
+ """
679
+
680
+ with open(filename, 'w') as f:
681
+ for i, d in zip(idxs, data):
682
+ i = i + start
683
+ line = ''.join([f'{x} ' for x in (i + 1)]) + str(d) + '\n'
684
+ f.write(line)
685
+
686
+ return