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.
@@ -0,0 +1,494 @@
1
+ """
2
+ Conversion Module
3
+ =================
4
+ This module cares about converting objects into another objects.
5
+ """
6
+
7
+ # Python modules
8
+ import sys
9
+ import numpy as np
10
+ from numpy import empty, array, zeros, arange, prod, int64, uint64, dot, sign, float64, nonzero
11
+ from numpy.linalg import norm
12
+ from numpy.random import randn
13
+ from numba import njit
14
+ from scipy.sparse import coo_matrix
15
+
16
+ # Tensor Fox modules
17
+ import TensorFox.Critical as crt
18
+ import TensorFox.MultilinearAlgebra as mlinalg
19
+
20
+
21
+ def x2cpd(x, factors, eq=True):
22
+ """
23
+ Given the point x (the flattened CPD), this function breaks it in parts to form the factors of the CPD.
24
+
25
+ Inputs
26
+ ------
27
+ x: float 1-D array
28
+ factors: list of 2-D arrays
29
+ Factors matrices to be updated.
30
+
31
+ Outputs
32
+ -------
33
+ factors: list of 2-D arrays
34
+ """
35
+
36
+ R = factors[0].shape[1]
37
+ L = len(factors)
38
+
39
+ s = 0
40
+ for l in range(L):
41
+ dim = factors[l].shape[0]
42
+ for r in range(R):
43
+ factors[l][:, r] = x[s: s+dim]
44
+ s += dim
45
+
46
+ if eq:
47
+ factors = equalize(factors, R)
48
+
49
+ return factors
50
+
51
+
52
+ def cpd2tens(factors):
53
+ """
54
+ Converts the factor matrices to tensor in coordinate format using a Khatri-Rao product formula.
55
+
56
+ Inputs
57
+ ------
58
+ factors: list of 2-D arrays
59
+ The factor matrices.
60
+
61
+ Outputs
62
+ ------
63
+ T_approx: float L-D array
64
+ Tensor (factors[0],...,factors[L-1])*I in coordinate format.
65
+ """
66
+
67
+ L = len(factors)
68
+ dims = [factors[l].shape[0] for l in range(L)]
69
+ T_approx = empty(dims)
70
+ M = factors[1]
71
+
72
+ for l in range(2, L):
73
+ N = empty((M.shape[0]*factors[l].shape[0], M.shape[1]))
74
+ M = mlinalg.khatri_rao(factors[l], M, N)
75
+
76
+ T1_approx = dot(factors[0], M.T)
77
+ T_approx = foldback(T_approx, T1_approx, 1)
78
+
79
+ return T_approx
80
+
81
+
82
+ def cpd2sparsetens(factors, idxs, dims):
83
+ """
84
+ Converts the factor matrices to tensor in sparse coordinate format.
85
+
86
+ Inputs
87
+ ------
88
+ factors: list of 2-D arrays
89
+ The factor matrices.
90
+ idxs: 2-D array
91
+ For each row idxs[i, :], the program will return the value of T[idxs[i, :]].
92
+ dims: 1-D array
93
+ The dimensions of T.
94
+
95
+ Outputs
96
+ ------
97
+ T_approx: list of float 1-D arrays
98
+ The tensor in sparse coordinate format.
99
+ """
100
+
101
+ R = factors[0].shape[1]
102
+ L = len(dims)
103
+ nnz = len(idxs)
104
+
105
+ if nnz == 0:
106
+ return [], idxs, dims
107
+
108
+ data_approx_cols = zeros((nnz, R))
109
+ data_approx_cols = crt.sparse_fastnorm_computations(data_approx_cols, idxs, factors, R, L, nnz)
110
+ data_approx = np.sum(data_approx_cols, axis=1)
111
+
112
+ T_approx = [data_approx, idxs, dims]
113
+
114
+ return T_approx
115
+
116
+
117
+ def cpd2unfold1(T1_approx, factors):
118
+ """
119
+ Converts the factor matrices to the first unfolding of the corresponding tensor.
120
+
121
+ Inputs
122
+ ------
123
+ factors: list of 2-D arrays
124
+ The factor matrices.
125
+
126
+ Outputs
127
+ ------
128
+ T1_approx: float 2-D array
129
+ First unfolding of T_approx, where T_approx is (factors[0],...,factors[L-1])*I in coordinate format.
130
+ """
131
+
132
+ L = len(factors)
133
+ M = factors[1]
134
+
135
+ for l in range(2, L):
136
+ N = empty((M.shape[0]*factors[l].shape[0], M.shape[1]))
137
+ M = mlinalg.khatri_rao(factors[l], M, N)
138
+
139
+ dot(factors[0], M.T, out=T1_approx)
140
+
141
+ return T1_approx
142
+
143
+
144
+ def sparse2dense(data, idxs, dims):
145
+ """
146
+ Given the variables defining a sparse tensor, this function computes its dense representation.
147
+
148
+ Inputs
149
+ ------
150
+ data: float 1-D arrays
151
+ data[i] is the nonzero value of the tensor at index idxs[i, :].
152
+ idxs: int 2-D array
153
+ Let nnz be the number of nonzero entries of the tensor. Then idxs is an array
154
+ of shape (nnz, L) such that idxs[i, :] is the index of the i-th nonzero entry.
155
+ dims: list or tuple
156
+ The dimensions (shape) of the tensor.
157
+
158
+ Outputs
159
+ -------
160
+ T_dense: L-D array
161
+ Dense representation of the tensor.
162
+ """
163
+
164
+ data = array(data)
165
+ idxs = array(idxs)
166
+ T1 = sparse_unfold(data, idxs, dims, 1)
167
+ T1_dense = T1.toarray()
168
+ T_dense = empty(dims, dtype=float64)
169
+ T_dense = foldback(T_dense, T1_dense, 1)
170
+
171
+ return T_dense
172
+
173
+
174
+ def dense2sparse(T):
175
+ """
176
+ Given a dense tensor, this function computes its sparse representation.
177
+
178
+ Inputs
179
+ -------
180
+ T_dense: L-D array
181
+ Dense representation of the tensor.
182
+
183
+ Outputs
184
+ ------
185
+ data: float 1-D arrays
186
+ data[i] is the nonzero value of the tensor at index idxs[i, :].
187
+ idxs: int 2-D array
188
+ Let nnz be the number of nonzero entries of the tensor. Then idxs is an array
189
+ of shape (nnz, L) such that idxs[i, :] is the index of the i-th nonzero entry.
190
+ dims: list or tuple
191
+ The dimensions (shape) of the tensor.
192
+ """
193
+
194
+ dims = T.shape
195
+ L = len(dims)
196
+ nnz = nonzero(T)
197
+ idxs = []
198
+ data = []
199
+
200
+ for i in range(len(nnz[0])):
201
+ idxs.append([nnz[l][i] for l in range(L)])
202
+ data.append(T[tuple(idxs[i])])
203
+
204
+ return [array(data), array(idxs), list(dims)]
205
+
206
+
207
+ def unfold(T, mode):
208
+ """
209
+ Computes any unfolding of a tensor up to order L = 12.
210
+
211
+ Inputs
212
+ ------
213
+ T: float L-D array
214
+ The mode we are interested in. Note that 1 <= mode <= L.
215
+
216
+ Outputs
217
+ -------
218
+ Tl: 2-D array with Fortran order
219
+ The requested unfolding of T.
220
+ """
221
+
222
+ dims = T.shape
223
+ L = len(dims)
224
+ Tl = empty((dims[mode-1], mlinalg.multiply_dims(dims)//dims[mode-1]), order='F')
225
+ func_name = "unfold" + str(mode) + "_order" + str(L)
226
+ Tl = getattr(crt, func_name)(T, Tl, tuple(dims))
227
+
228
+ return Tl
229
+
230
+
231
+ def unfold_C(T, mode):
232
+ """
233
+ Computes any unfolding of a tensor up to order L = 12.
234
+
235
+ Inputs
236
+ ------
237
+ T: float L-D array
238
+ The mode we are interested in. Note that 1 <= mode <= L.
239
+
240
+ Outputs
241
+ -------
242
+ Tl: 2-D array with C order
243
+ The requested unfolding of T.
244
+ """
245
+
246
+ dims = T.shape
247
+ L = len(dims)
248
+ Tl = empty((dims[mode-1], mlinalg.multiply_dims(dims)//dims[mode-1]))
249
+ func_name = "unfold" + str(mode) + "_order" + str(L)
250
+ Tl = getattr(crt, func_name)(T, Tl, tuple(dims))
251
+
252
+ return Tl
253
+
254
+
255
+ def sparse_unfold(data, idxs, dims, mode):
256
+ """
257
+ Computes any unfolding of a sparse L-th order tensor.
258
+
259
+ Inputs
260
+ ------
261
+ data: float 1-D arrays
262
+ data[i] is the nonzero value of the tensor at index idxs[i, :].
263
+ idxs: int 2-D array
264
+ Let nnz be the number of nonzero entries of the tensor. Then idxs is an array
265
+ of shape (nnz, L) such that idxs[i, :] is the index of the i-th nonzero entry.
266
+ dims: list or tuple
267
+ The dimensions (shape) of the tensor.
268
+ mode: int
269
+ The mode we are interested in. Note that 1 <= mode <= L.
270
+
271
+ Outputs
272
+ -------
273
+ Tl: csr matrix
274
+ Sparse representation (in compressed sparse row format) of the requested unfolding.
275
+ """
276
+
277
+ L = len(dims)
278
+ idx = list(arange(L))
279
+ idx.remove(mode-1)
280
+ K = [0 for l in range(L)]
281
+ dims = [int(dim) for dim in dims]
282
+
283
+ c = 0
284
+ for l in range(L):
285
+ if l == mode-1:
286
+ K[l] = 0
287
+ else:
288
+ s = 1
289
+ for ll in range(c):
290
+ s *= int(dims[idx[ll]])
291
+ K[l] = s
292
+ c += 1
293
+
294
+ rows = idxs[:, mode-1]
295
+ cols = np.sum(array(K, dtype=uint64) * array(idxs, dtype=uint64), axis=1, dtype=uint64)
296
+ try:
297
+ Tl = coo_matrix((data, (rows, cols)), shape=(dims[mode-1], mlinalg.multiply_dims(dims)//dims[mode-1]))
298
+ except ValueError:
299
+ sys.exit('Got too big index when tried to generate the unfold of sparse tensor.')
300
+ Tl = Tl.tocsr()
301
+
302
+ return Tl
303
+
304
+
305
+ def foldback(T, Tl, mode):
306
+ """
307
+ Computes the tensor with dimension dims given an unfolding with its mode.
308
+ """
309
+
310
+ dims = T.shape
311
+ L = len(dims)
312
+ func_name = "foldback" + str(mode) + "_order" + str(L)
313
+ T = getattr(crt, func_name)(T, Tl, tuple(dims))
314
+
315
+ return T
316
+
317
+
318
+ def normalize(factors):
319
+ """
320
+ Normalize the columns of the factors to have unit column norm and scale Lambda accordingly. This function returns
321
+ Lambda and the normalized factors.
322
+ """
323
+
324
+ R = factors[0].shape[1]
325
+ Lambda = zeros(R)
326
+ L = len(factors)
327
+ new_factors = factors.copy()
328
+
329
+ for r in range(R):
330
+ norms = zeros(L)
331
+ for l in range(L):
332
+ W = factors[l]
333
+ norms[l] = norm(W[:, r])
334
+ W[:, r] = W[:, r]/norms[l]
335
+ # Update factor matrix.
336
+ new_factors[l] = W
337
+ Lambda[r] = prod(norms)
338
+
339
+ return Lambda, new_factors
340
+
341
+
342
+ def denormalize(Lambda, factors):
343
+ """
344
+ By undoing the normalization of the factors this function makes it unnecessary the use of the diagonal tensor
345
+ Lambda.
346
+ """
347
+
348
+ R = Lambda.size
349
+ L = len(factors)
350
+ new_factors = [zeros(factors[l].shape) for l in range(L)]
351
+
352
+ for r in range(R):
353
+ a = abs(Lambda[r])**(1/L)
354
+ new_factors[0][:, r] = sign(Lambda[r]) * a * factors[0][:, r]
355
+ for l in range(1, L):
356
+ new_factors[l][:, r] = a * factors[l][:, r]
357
+
358
+ return new_factors
359
+
360
+
361
+ def equalize(factors, R):
362
+ """
363
+ Let W[0], ..., W[L-1] = factors. After a Gauss-Newton iteration we have an approximated CPD with factors
364
+ W[0]_r ⊗ ... ⊗ W[L-1]_r. They may have very different magnitudes and this can have effect on the convergence
365
+ rate. To improve this we try to equalize their magnitudes by introducing scalars a_0, ..., a_{L-1} such that
366
+ W[0]_r ⊗ ... ⊗ W[L-1]_r = (a_0*W[0]_r) ⊗ ... ⊗ (a_{L-1}*W[L-1]_r) and |a_0*W[0]_r| = ... = |a_{L-1}*W[L-1]_r|.
367
+ Note that we must have a_0*...*a_{L-1} = 1.
368
+
369
+ To find good values for the a_l's, we can search for critical points of the function
370
+ f(a_0,...,a_{L-1}) = sum_{i, j=0...L-1} (|a_i*W[i]_r|-|a_j*W[j]_r|)^2 .
371
+ Using Lagrange multipliers we find the solution
372
+ a_0 = (|W[0]_r|*...*|W[L-1]_r|)^(1/L)/|W[0]_r|,
373
+ ...
374
+ a_L = (|W[0]_r|*...*|W[L-1]_r|)^(1/L)/|W[L-1]_r|.
375
+ We can see that this solution satisfy the conditions mentioned.
376
+ """
377
+
378
+ L = len(factors)
379
+
380
+ for r in range(R):
381
+ norm_r = array([norm(factors[l][:, r]) for l in range(L)])
382
+ if prod(norm_r) != 0.0:
383
+ numerator = prod(norm_r)**(1/L)
384
+ for l in range(L):
385
+ factors[l][:, r] = (numerator/norm_r[l]) * factors[l][:, r]
386
+
387
+ return factors
388
+
389
+
390
+ def change_sign(factors):
391
+ """
392
+ After the CPD is computed it may be interesting that each vector of a rank one term is as positive as possible, in
393
+ the sense that its mean is positive. If two vectors in the same rank one term have negative mean, then we can
394
+ multiply both by -1 without changing the tensor.
395
+ """
396
+
397
+ L = len(factors)
398
+ R = factors[0].shape[1]
399
+
400
+ for r in range(R):
401
+ parity = 0
402
+ for l in range(L):
403
+ if factors[l][:, r].mean() < 0:
404
+ factors[l][:, r] *= -1
405
+ parity += 1
406
+ if parity%2 == 1:
407
+ factors[0] *= -1
408
+
409
+ return factors
410
+
411
+
412
+ def transform(factors, symm, factors_norm):
413
+ """
414
+ The parameter symm indicates that the objective tensor is symmetric, so the program forces this symmetry over the
415
+ factor matrices.
416
+ The parameter factors_norm forces the factor matrices to always have the same prescribed norm, which is the value
417
+ factors_norm.
418
+
419
+ Inputs
420
+ ------
421
+ factors: list of 2D arrays
422
+ symm: bool
423
+ factors_norm: float
424
+
425
+ Outputs
426
+ -------
427
+ factors: list of 2D arrays
428
+ """
429
+
430
+ L = len(factors)
431
+
432
+ if symm:
433
+ s = factors[0]
434
+ for l in range(1, L):
435
+ s += factors[l]
436
+ factors[0] = s/L
437
+ for l in range(1, L):
438
+ factors[l] = factors[0]
439
+
440
+ if factors_norm > 0:
441
+ for l in range(L):
442
+ factors[l] = factors_norm * (1/norm(factors[l])) * factors[l]
443
+
444
+ return factors
445
+
446
+
447
+ @njit(nogil=True)
448
+ def vec(M, Bv, num_rows, R):
449
+ """
450
+ Take a matrix M with shape (num_rows, R) and stack vertically its columns to form the matrix Bv = vec(M) with shape
451
+ (num_rows*R,).
452
+ """
453
+
454
+ for r in range(R):
455
+ Bv[r*num_rows:(r+1)*num_rows] = M[:, r]
456
+
457
+ return Bv
458
+
459
+
460
+ def inflate(T, R, dims):
461
+ """
462
+ Let T be a tensor of shape dims. If rank > dims[l], this function increases T dimensions such that each new
463
+ dimension satisfies new_dims[l] = R. The new entries are all random number very close to zero.
464
+ """
465
+
466
+ L = len(dims)
467
+ new_dims = zeros(L, int64)
468
+ slices = []
469
+
470
+ for l in range(L):
471
+ slices.append(slice(dims[l]))
472
+ if dims[l] >= R:
473
+ new_dims[l] = dims[l]
474
+ else:
475
+ new_dims[l] = R
476
+
477
+ new_T = 1e-12*randn(*new_dims)
478
+ new_T[tuple(slices)] = T
479
+
480
+ return new_T
481
+
482
+
483
+ def deflate(factors, orig_dims, inflate_status):
484
+ """
485
+ If the tensor was inflated, this function restores the factors to their original shape by truncating them.
486
+ """
487
+
488
+ if not inflate_status:
489
+ return factors
490
+
491
+ else:
492
+ L = len(orig_dims)
493
+ factors = [ factors[l][:orig_dims[l], :] for l in range(L) ]
494
+ return factors