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,649 @@
1
+ """
2
+ Multilinear Algebra Module
3
+ ==========================
4
+ All relevant functions of the classical multilinear algebra are described and implemented in this module.
5
+
6
+ References
7
+ ==========
8
+
9
+ - 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.
10
+ ISSN 0004-5411. doi: 10.1145/2512329.
11
+
12
+ - P. Breiding and N. Vannieuwenhoven, A Riemannian Trust Region Method for the Canonical Tensor Rank Approximation
13
+ Problem, SIAM J. Optim., 28(3), 2435-2465.
14
+
15
+ - P. Breiding and N. Vannieuwenhoven, The Condition Number of Join Decompositions, arXiv:1611.08117v3 (2018).
16
+
17
+ - Felipe B. D., Tensor Decompositions and Algorithms, with Applications to Tensor Learning, PdG Thesis, arXiv:2110.05997 (2019).
18
+
19
+ """
20
+
21
+ # Python modules
22
+ import sys
23
+ import time
24
+ import numpy as np
25
+ from numpy import dot, zeros, empty, uint64, float64, array, sort, ceil, identity, argmax, inf, sqrt, arange, linspace
26
+ from numpy.linalg import norm, svd
27
+ import numpy.matlib
28
+ import scipy as scp
29
+ import multiprocessing
30
+ import multiprocessing.pool
31
+ from functools import partial
32
+ from scipy.sparse import coo_matrix
33
+ from numba import njit, prange
34
+
35
+ # Tensor Fox modules
36
+ import TensorFox.Auxiliar as aux
37
+ import TensorFox.Compression as cmpr
38
+ import TensorFox.Conversion as cnv
39
+ import TensorFox.Critical as crt
40
+
41
+
42
+ def multilin_mult_cpd(U, W, dims):
43
+ """
44
+ Performs the multilinear multiplication (U[0],...,U[L-1])*(W[0], ..., W[L-1])*I = (U[0]*W[0],...,U[L-1]*W[L-1])*I,
45
+ where I.shape = dims = (W[0].shape[1],...,W[L-1].shape[1]) are the size of the columns of the W's.
46
+
47
+ Inputs
48
+ ------
49
+ U: list of 2-D arrays
50
+ W: list of 2-D arrays
51
+ dims: list of ints
52
+
53
+ Outputs
54
+ -------
55
+ S: float array
56
+ S is the resulting multidimensional of the mentioned multiplication.
57
+ """
58
+
59
+ L = len(dims)
60
+ # dims_out are the dimensions of the output tensor.
61
+ dims_out = []
62
+ W_new = []
63
+
64
+ for l in range(L):
65
+ W_new.append( dot(U[l], W[l]) )
66
+ dims_out.append(W_new[l].shape[0])
67
+
68
+ S = cnv.cpd2tens(W_new)
69
+ return S
70
+
71
+
72
+ def multilin_mult(UT, T1, dims):
73
+ """
74
+ Performs the multilinear multiplication (U[0]^T,...,U[L-1]^T)*T, where U[i]^T is the transpose of U[i],
75
+ dims = T.shape. We need the first unfolding T1 of T to start the computations. To understand better why
76
+ the method to obtain S works you can read example 2.1.13 in the PhD Thesis mentioned above.
77
+
78
+ Inputs
79
+ ------
80
+ UT: list of 2-D arrays
81
+ T1: 2-D array
82
+ First unfolding of T.
83
+ dims: list of ints
84
+ Dimension of T.
85
+
86
+ Outputs
87
+ -------
88
+ S: float array
89
+ S is the resulting multidimensional of the multilinear multiplication (U[0]^T,...,U[L-1]^T)*T.
90
+ """
91
+
92
+ L = len(dims)
93
+ S_dims = list(dims)
94
+
95
+ unfolding1 = T1
96
+ for l in range(L):
97
+ unfolding2 = dot(UT[l], unfolding1)
98
+ # Update the current dimension of S_dims.
99
+ S_dims[l] = UT[l].shape[0]
100
+ S = empty(S_dims)
101
+ S = cnv.foldback(S, unfolding2, l+1)
102
+ if l < L-1:
103
+ unfolding1 = cnv.unfold(S, l+2)
104
+ else:
105
+ return S
106
+
107
+
108
+ def sparse_multilin_mult_direct(S, U, idxs, dims):
109
+ """
110
+ Performs the multilinear multiplication (U[0],...,U[L-1])*S, where idxs are the indexes of T and S is dense.
111
+
112
+ Inputs
113
+ ------
114
+ S: float array
115
+ S is the resulting compression of T, given by T = (U[0],...,U[L-1])*S.
116
+ U: list of 2-D arrays
117
+ idxs: int 2-D array
118
+ Let nnz be the number of nonzero entries of the tensor T. Then idxs is an array of shape (nnz, L) such that
119
+ idxs[i, :] is the index of the i-th nonzero entry.
120
+ dims: list or tuple
121
+ The dimensions (shape) of the tensor T.
122
+
123
+ Outputs
124
+ -------
125
+ T: sparse tensor
126
+ T is given in the format T = [data_approx, idxs, dims]. The arrays idxs and dims are know in advance, only
127
+ data_approx is computed here.
128
+ """
129
+
130
+ L = len(dims)
131
+ func_name = "sparse_multilin_mult_order" + str(L) + "_direct"
132
+
133
+ # Run the multiplication function.
134
+ try:
135
+ data_approx = getattr(crt, func_name)(S, U, idxs)
136
+ # Change arrays order to be compatible with Numba function.
137
+ except:
138
+ U_tmp = [array(u, order='C') for u in U]
139
+ data_approx = getattr(crt, func_name)(S, U_tmp, idxs)
140
+ T_approx = [data_approx, idxs, dims]
141
+
142
+ return T_approx
143
+
144
+
145
+ def sparse_multilin_mult(UT, data, idxs, dims):
146
+ """
147
+ Performs the multilinear multiplication (U[0]^T,...,U[L-1]^T)*T, where U[i]^T is the transpose of U[i],
148
+ dims = T.shape and T is sparse.
149
+
150
+ Inputs
151
+ ------
152
+ U: list of 2-D arrays
153
+ data: float 1-D arrays
154
+ data[i] is the nonzero value of the tensor T at index idxs[i, :].
155
+ idxs: int 2-D array
156
+ Let nnz be the number of nonzero entries of the tensor T. Then idxs is an array of shape (nnz, L) such that
157
+ idxs[i, :] is the index of the i-th nonzero entry.
158
+ dims: list or tuple
159
+ The dimensions (shape) of the tensor T.
160
+
161
+ Outputs
162
+ -------
163
+ S: float array
164
+ S is the resulting multidimensional array of the multilinear multiplication (U[0]^T,...,U[L-1]^T)*T.
165
+ """
166
+
167
+ L = len(dims)
168
+ # dims_out are the dimensions of the output tensor S.
169
+ dims_out = [UT[l].shape[0] for l in range(L)]
170
+ S = empty(dims_out, dtype=float64)
171
+ func_name = "sparse_multilin_mult_order" + str(L)
172
+
173
+ # Generate a different version of U to deal with sparse index accesses.
174
+ nnz = len(data)
175
+ U_tmp = [array(UT[l][:, idxs[:, l]], order='C') for l in range(L)]
176
+ data_list = [array(data) for i in range(dims_out[0])]
177
+
178
+ # Run the multiplication function.
179
+ try:
180
+ S = getattr(crt, func_name)(U_tmp, data_list, S, dims_out)
181
+ except:
182
+ # Change arrays order to be compatible with Numba function.
183
+ U_tmp = [array(UT[l][:, idxs[:, l]], order='C') for l in range(L)]
184
+ data_list = [array(data, order='A') for i in range(dims_out[0])]
185
+ S = getattr(crt, func_name)(U_tmp, data_list, S, dims_out)
186
+
187
+ # Free memory.
188
+ U_tmp = []
189
+
190
+ return S
191
+
192
+
193
+ def multirank_approx(T, multi_rank, options):
194
+ """
195
+ This function computes an approximation of T with multilinear rank = multi_rank. Truncation the core tensor of the
196
+ MLSVD doesn't gives the best low multirank approximation, but gives very good approximations.
197
+
198
+ Inputs
199
+ ------
200
+ T: float array
201
+ multi_rank: list of int
202
+ The desired low multilinear rank.
203
+
204
+ Outputs
205
+ -------
206
+ T_approx: float array
207
+ The approximating tensor with multilinear rank = multi_rank.
208
+ """
209
+
210
+ # Compute dimensions and norm of T.
211
+ dims = T.shape
212
+ sorted_dims = sort(array(dims))
213
+ L = len(dims)
214
+ Tsize = norm(T)
215
+
216
+ # Compute truncated MLSVD of T.
217
+ options = aux.make_options(options)
218
+ options.display = 0
219
+ options.trunc_dims = multi_rank
220
+ R_gen = int(ceil( multiply_dims(sorted_dims)/(np.sum(sorted_dims) - L + 1) ))
221
+ S, U, UT, sigmas = cmpr.mlsvd(T, Tsize, R_gen, options)
222
+
223
+ # Construct the corresponding tensor T_approx.
224
+ S1 = cnv.unfold(S, 1)
225
+ T_approx = multilin_mult(U, S1, multi_rank)
226
+
227
+ return T_approx
228
+
229
+
230
+ @njit(nogil=True)
231
+ def kronecker(A, B):
232
+ """
233
+ Computes the Kronecker product between A and B. We must have M.shape = (a1*b1, a2*b2), where A.shape = (a1, a2) and
234
+ B.shape = (b1, b2).
235
+ """
236
+
237
+ a1, a2 = A.shape
238
+ b1, b2 = B.shape
239
+ M = empty((a1*b1, a2*b2), dtype=float64)
240
+
241
+ for i in range(a1):
242
+ for j in range(a2):
243
+ M[i*b1: (i+1)*b1, j*b2: (j+1)*b2] = A[i, j]*B
244
+
245
+ return M
246
+
247
+
248
+ @njit(nogil=True, parallel=True)
249
+ def khatri_rao(A, B, M):
250
+ """
251
+ Computes the Khatri-Rao product between A and B. We must have M.shape = (a1*b1, a2), where A.shape = (a1, a2) and
252
+ B.shape = (b1, b2), with a2 == b2. This function makes the computation of A ⊙ B row by row, starting at the top.
253
+ """
254
+
255
+ a1, a2 = A.shape
256
+ b1, b2 = B.shape
257
+
258
+ for i in prange(a1):
259
+ M[i*b1:(i+1)*b1, :] = khatri_rao_inner_computations(A, B, M, i, b1, b2)
260
+
261
+ return M
262
+
263
+
264
+ @njit(nogil=True)
265
+ def khatri_rao_inner_computations(A, B, M, i, b1, b2):
266
+ """
267
+ Computes A[i, :]*B.
268
+ """
269
+
270
+ for k in range(b1):
271
+ for j in range(b2):
272
+ M[i*b1 + k, j] = A[i, j]*B[k, j]
273
+
274
+ return M[i*b1: (i+1)*b1, :]
275
+
276
+
277
+ @njit(nogil=True)
278
+ def hadamard(A, B, M):
279
+ """
280
+ Computes M = A * B, where * is the Hadamard product. Since all Hadamard products in this context are between R x R
281
+ matrices, we assume this without verifications.
282
+ """
283
+
284
+ R = A.shape[0]
285
+
286
+ for r in range(R):
287
+ for rr in range(R):
288
+ M[r, rr] = A[r, rr]*B[r, rr]
289
+
290
+ return M
291
+
292
+ @njit(nogil=True)
293
+ def hadamard_rect(A, B, M):
294
+ """
295
+ Hadamard for rectangular matrices.
296
+ """
297
+
298
+ nrows, ncols = A.shape
299
+
300
+ for i in range(nrows):
301
+ for j in range(ncols):
302
+ M[i, j] = A[i, j] * B[i, j]
303
+
304
+ return M
305
+
306
+
307
+ def cond(factors):
308
+ """
309
+ Computes the geometric condition number of 'factors', where factors is a list with the factor matrices of some CPD.
310
+ Warning: this function requires a lot of memory.
311
+ """
312
+
313
+ def trd_jacobian(A):
314
+ """
315
+ Computes the derivative of the map S^r -> s_r(S).
316
+ """
317
+
318
+ L = len(A)
319
+ dims = array([A[i].shape[0] for i in range(L)])
320
+ N = 1 + np.sum(dims - 1)
321
+ r = A[0].shape[1]
322
+ Pi = multiply_dims(dims)
323
+ Sigma = 1 + np.sum(dims - 1)
324
+
325
+ idxSet = zeros((L - 1, r))
326
+ for k in range(1, L):
327
+ idxSet[k - 1, :] = argmax(np.abs(A[k]), 0)
328
+
329
+ J = zeros((Pi, r * Sigma))
330
+ off = 0
331
+ for i in range(r):
332
+ F = [np.matlib.repmat(scp.linalg.orth(A[j][:, i].reshape(A[j].shape[0], 1)), 1, dims[0]) for j in range(L)]
333
+ F[0] = identity(dims[0])
334
+ F_inverse = [F[i] for i in reversed(range(L))]
335
+ G = khatri_rao_factors(F_inverse)
336
+ J[:, off: off + dims[0]] = G
337
+ off = off + dims[0]
338
+
339
+ for k in range(1, L):
340
+ F = [np.matlib.repmat(scp.linalg.orth(A[j][:, i].reshape(A[j].shape[0], 1)), 1, dims[k] - 1) for j in
341
+ range(L)]
342
+ I = identity(dims[k])
343
+ ri = int(idxSet[k - 1, i])
344
+ cols1 = [i for i in range(ri)]
345
+ cols2 = [i for i in range(ri + 1, dims[k])]
346
+ F[k] = I[:, cols1 + cols2]
347
+ F_inverse = [F[i] for i in reversed(range(L))]
348
+ G = khatri_rao_factors(F_inverse)
349
+ J[:, off: off + dims[k] - 1] = G
350
+ off = off + dims[k] - 1
351
+
352
+ u, s, vt = svd(J[:, off - N + 1:off], full_matrices=False)
353
+ J[:, off - N + 1:off] = u
354
+
355
+ return J
356
+
357
+ L = len(factors)
358
+ dims = array([factors[l].shape[0] for l in range(L)])
359
+ R = factors[0].shape[1]
360
+
361
+ # Verify if current rank is bigger than the generic rank of the space.
362
+ P = multiply_dims(dims)
363
+ S = R * (1 + np.sum(dims - 1))
364
+ if P < S:
365
+ J = []
366
+ cN = inf
367
+ return cN, J
368
+
369
+ J = trd_jacobian(factors)
370
+ u, s, vt = svd(J, full_matrices=False)
371
+ cN = s[-1] ** (-1)
372
+
373
+ return cN
374
+
375
+
376
+ def khatri_rao_factors(factors):
377
+ """
378
+ Computes the Khatri-Rao products W^(1) ⊙ W^(2) ⊙ ... ⊙ W^(L) between the factor matrices.
379
+ """
380
+
381
+ L = len(factors)
382
+ A = factors[0]
383
+ for l in range(1, L):
384
+ m, R = factors[l].shape
385
+ B = zeros((m * A.shape[0], R))
386
+ B = khatri_rao(A, factors[l], B)
387
+ A = B
388
+
389
+ return B
390
+
391
+
392
+ def compute_error(T, Tsize, S1, U, dims):
393
+ """
394
+ Compute relative error between T and (U_1,...,U_L)*S, where dims is the shape of S. In the case T is sparse, we
395
+ should pass S instead of the unfolding S1.
396
+ """
397
+
398
+ # T is sparse.
399
+ if type(T) == list:
400
+ S = S1
401
+ data, idxs, Tdims = T
402
+ T_approx = sparse_multilin_mult_direct(S, U, idxs, Tdims)
403
+ data_approx = T_approx[0]
404
+ error = norm(data - data_approx)/Tsize
405
+ # T is dense.
406
+ else:
407
+ T_compress = multilin_mult(U, S1, dims)
408
+ error = norm(T - T_compress)/Tsize
409
+
410
+ return error
411
+
412
+
413
+ def rank1_terms_list(factors):
414
+ """
415
+ Compute each rank 1 term, as a multidimensional array, of the CPD. Let T be the corresponding the tensor, in
416
+ coordinates, of thr CPD given by factors, and let rank1_terms = [T_1, T_2, ..., T_R] be the output of this function.
417
+ Then we have that T_1 + T_2 + ... + T_R = T.
418
+
419
+ Inputs
420
+ ------
421
+ factors: list of float 2-D ndarrays with shape (dims[i], R) each
422
+ The CPD factors of some tensor.
423
+
424
+ Outputs
425
+ -------
426
+ rank1_terms: list of float ndarrays
427
+ Each tensor rank1_terms[r] is the r-th rank-1 term of the given CPD.
428
+ """
429
+
430
+ R = factors[0].shape[1]
431
+ L = len(factors)
432
+ rank1_terms = []
433
+
434
+ for r in range(R):
435
+ vectors = []
436
+ for l in range(L):
437
+ # vectors[l] = [w_r^(1),w_r^(2),...,w_r^(L)], which represents w_r^(1) ⊗ w_r^(2) ⊗ ... ⊗ w_r^(L).
438
+ v = factors[l][:, r]
439
+ vectors.append(v.reshape(v.size, 1))
440
+ term = cnv.cpd2tens(vectors)
441
+ rank1_terms.append(term)
442
+
443
+ return rank1_terms
444
+
445
+
446
+ @njit(nogil=True, parallel=True)
447
+ def rank1(X, Y, Z, m, n, R, k):
448
+ """
449
+ Computes the k-th slice of each rank 1 term of the CPD given by X, Y, Z. By doing this for all R terms we have a
450
+ tensor with R slices, each one representing a rank-1 term of the original CPD.
451
+
452
+ Inputs
453
+ ------
454
+ X, Y, Z: 2-D float array
455
+ The CPD factors of some third order tensor.
456
+ m, n, p, R: int
457
+ k: int
458
+ Slice we want to compute.
459
+
460
+ Outputs
461
+ -------
462
+ rank1_slices: 3-D float array
463
+ Each matrix rank1_slices[:, :, l] is the k-th slice associated with the l-th factor in the CPD of some tensor.
464
+ """
465
+
466
+ # Each frontal slice of rank1_slices is the coordinate representation of a
467
+ # rank one term of the CPD given by (X,Y,Z)*Lambda.
468
+ rank1_slices = zeros((m, n, R), dtype=float64)
469
+
470
+ for r in prange(R):
471
+ for i in range(m):
472
+ for j in range(n):
473
+ rank1_slices[i, j, r] = X[i, r] * Y[j, r] * Z[k, r]
474
+
475
+ return rank1_slices
476
+
477
+
478
+ def forward_error(orig_factors, approx_factors):
479
+ """
480
+ Let T = T_1 + T_2 + ... + T_R be the decomposition of T as sum of rank-1 terms and let
481
+ T_approx = T_approx_1 + T_approx_2 + ... + T_approx_R be the decomposition of T_approx as sum of R terms. Supposedly
482
+ T_approx is obtained after the cpd function call. The ordering of the rank-1 terms of T_approx can be permuted freely
483
+ without changing the tensor. While |cpd2tens(T) - cpd2tens(T_approx)| is the backward error of the CPD computation
484
+ problem, we have that min_s sqrt( |T_1 - T_approx_s(1)|^2 + ... + |T_R - T_approx_s(R)|^2 ) is the forward error of
485
+ the problem, where s is an element of the permutation group S_R.
486
+
487
+ Inputs
488
+ ------
489
+ orig_factors: list of arrays
490
+ The elements of the list are the factor matrices of the original tensor.
491
+ approx_factors: list of arrays
492
+ The elements of the list are the factor matrices of the approximated tensor.
493
+
494
+ Outputs
495
+ -------
496
+ best_error: float
497
+ Relative error of the best permutation found.
498
+ best_factors: list
499
+ List with the new factor matrices of the best permutation.
500
+ best_s: list
501
+ The indexes of the best permutation found.
502
+ """
503
+
504
+ R = orig_factors[0].shape[1]
505
+ L = len(orig_factors)
506
+ orig_rank1 = rank1_terms_list(orig_factors)
507
+ approx_rank1 = rank1_terms_list(approx_factors)
508
+
509
+ best_forward_error, s = search_forward_error(tuple(orig_rank1), tuple(approx_rank1), R)
510
+
511
+ # Rearrange approx_factors with the best permutation found.
512
+ new_factors = [approx_factors[l][:, s] for l in range(L)]
513
+
514
+ return best_forward_error, new_factors, s
515
+
516
+
517
+ def search_forward_error(orig_rank1, approx_rank1, R):
518
+ """
519
+ Auxiliary function for the function forward_error.
520
+ """
521
+
522
+ best_error = 0
523
+ best_s = arange(R)
524
+ idx = []
525
+ for r in range(R):
526
+ f_error = inf
527
+ for rr in range(len(approx_rank1)):
528
+ if (rr not in idx) and (norm(orig_rank1[r] - approx_rank1[rr]) < f_error):
529
+ best_s[r] = rr
530
+ f_error = norm(orig_rank1[r] - approx_rank1[rr])
531
+ idx.append(best_s[r])
532
+ best_error += f_error**2
533
+ best_error = sqrt(best_error)
534
+
535
+ return best_error, best_s
536
+
537
+
538
+ def sparse_dot(A):
539
+ """
540
+ This function computes dot(A, A.T), where A is a sparse csr matrix. The function compute_svd calls this product when
541
+ the sparse_dot_mkl fails to perform the product, usually due to memory limitations.
542
+ """
543
+
544
+ n = A.shape[0]
545
+
546
+ # Create the list of slices of rows on which each process will work with.
547
+ num_procs = multiprocessing.cpu_count()
548
+ pieces = array([int(n*sqrt(i)/sqrt(num_procs)) for i in range(num_procs)])
549
+ pieces[-1] = n
550
+ pieces = [[pieces[i], pieces[i+1], i] for i in range(len(pieces)-1)]
551
+
552
+ # Compute the matrix values.
553
+ data_tmp = []
554
+ idxs_tmp = []
555
+ results = []
556
+ with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
557
+ partial_inputs = partial(crt.sparse_dot_inner, A=A)
558
+ results = pool.map(partial_inputs, pieces)
559
+ for r in results:
560
+ data_tmp += r[0]
561
+ idxs_tmp += r[1]
562
+
563
+ # Computes the diagonal of the matrix.
564
+ A_row_idxs = [set(A[i, :].indices) for i in range(n)]
565
+ B = A.tocoo()
566
+ A_dct = {(B.row[i], B.col[i]): B.data[i] for i in range(len(B.data))}
567
+ B = []
568
+ for i in range(n):
569
+ idx = A_row_idxs[i].intersection(A_row_idxs[i])
570
+ val = sum([A_dct[(i, k)] * A_dct[(i, k)] for k in idx])
571
+ if val != 0:
572
+ data_tmp.append(val)
573
+ idxs_tmp.append([i, i])
574
+
575
+ # Corner case.
576
+ if len(data_tmp) == 0:
577
+ print(' sparse_dot: The result is a null matrix.', file=sys.stderr)
578
+ data_tmp = [0]
579
+ idxs_tmp = [[0, 0]]
580
+
581
+ # Join the results.
582
+ data_tmp = array(data_tmp, dtype=float64)
583
+ idxs_tmp = array(idxs_tmp, dtype=uint64)
584
+ rows = idxs_tmp[:, 0]
585
+ cols = idxs_tmp[:, 1]
586
+ out_arr = coo_matrix((data_tmp, (rows, cols)), shape=(n, n))
587
+ out_arr = out_arr.tocsr()
588
+
589
+ return out_arr
590
+
591
+
592
+ def dense_sparse_dot(A, B, display):
593
+ """
594
+ This function computes dot(A, B), where A is dense and B is a sparse csr matrix. Although this function is slow,
595
+ it is able to handle huge sparse matrices. No other function in Tensor Fox call this function.
596
+ """
597
+
598
+ if display > 1:
599
+ time.sleep(1)
600
+ print(" -> The % progress can be visualized in the terminal.")
601
+
602
+ m = A.shape[0]
603
+ n = B.shape[1]
604
+
605
+ # Create the list of slices of columns on which each process will work with.
606
+ num_procs = multiprocessing.cpu_count()
607
+ pieces = array([int(x) for x in linspace(0, n, num_procs)])
608
+ pieces[-1] = n
609
+ pieces = [[pieces[i], pieces[i+1], i] for i in range(len(pieces)-1)]
610
+
611
+ # Compute the matrix values.
612
+ data_tmp = []
613
+ idxs_tmp = []
614
+ results = []
615
+ with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
616
+ partial_inputs = partial(crt.dense_sparse_dot_inner, A=A, B=B, display=display)
617
+ results = pool.map(partial_inputs, pieces)
618
+
619
+ for r in results:
620
+ data_tmp += r[0]
621
+ idxs_tmp += r[1]
622
+
623
+ # Corner case.
624
+ if len(data_tmp) == 0:
625
+ print(' dense_sparse_dot: The result is a null matrix.', file=sys.stderr)
626
+ data_tmp = [0]
627
+ idxs_tmp = [[0, 0]]
628
+
629
+ # Join the results.
630
+ data_tmp = array(data_tmp, dtype=float64)
631
+ idxs_tmp = array(idxs_tmp, dtype=uint64)
632
+ rows = idxs_tmp[:, 0]
633
+ cols = idxs_tmp[:, 1]
634
+ out_arr = coo_matrix((data_tmp, (rows, cols)), shape=(m, n))
635
+ out_arr = out_arr.tocsr()
636
+
637
+ return out_arr
638
+
639
+
640
+ def multiply_dims(dims):
641
+ """
642
+ This function returns the product of the dimensions of a tensor.
643
+ """
644
+
645
+ x = 1
646
+ for dim in dims:
647
+ x *= int(dim)
648
+
649
+ return x