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/Display.py ADDED
@@ -0,0 +1,466 @@
1
+ """
2
+ Display Module
3
+ ==============
4
+ This module contains function centered in computing and displaying general information about tensors and tensor spaces.
5
+ """
6
+
7
+ # Python modules
8
+ import numpy as np
9
+ from numpy import mean, var, array, sort, ceil, floor, zeros, uint8
10
+ from numpy.linalg import norm
11
+ import time
12
+ import warnings
13
+ import IPython.display as ipd
14
+ import matplotlib.pyplot as plt
15
+ from numba import njit
16
+ import pandas as pd
17
+
18
+ # Tensor Fox modules
19
+ import TensorFox.Auxiliar as aux
20
+ import TensorFox.Compression as cmpr
21
+ import TensorFox.MultilinearAlgebra as mlinalg
22
+ import TensorFox.TensorFox as tfx
23
+
24
+
25
+ def showtens(T):
26
+ """
27
+ Let T = T_(i,j,k) be a tensor in coordinates. It is usual to consider that i is the row coordinate, j is the column
28
+ coordinate and k is the section coordinate. But if we just run the command print(T) we will see that numpy considers
29
+ i as the slice coordinate, j the row coordinate and k the column coordinate. This is not the usual way to consider
30
+ tensors, and if we want to print T section by section (i.e., each frontal slice separately), this function does the
31
+ job.
32
+ """
33
+
34
+ dims = T.shape
35
+ L = len(dims)
36
+
37
+ if L == 3:
38
+ for k in range(T.shape[2]):
39
+ print(T[:, :, k])
40
+ print()
41
+ else:
42
+ print(T)
43
+
44
+ return
45
+
46
+
47
+ def infotens(T):
48
+ """
49
+ Given a tensor T, this function computes and shows several information about this tensor. There are only print
50
+ outputs. Since this function tries to estimate the rank of T, be aware that it may take a long time to finish all
51
+ computations.
52
+ """
53
+
54
+ # Compute dimensions and norm of T.
55
+ dims = T.shape
56
+ L = len(dims)
57
+ Tsize = norm(T)
58
+
59
+ print('T is a tensor of dimensions', dims)
60
+ print()
61
+ print('|T| =', Tsize)
62
+ print()
63
+
64
+ # Max and min entries of T.
65
+ print('max(T) =', np.max(T))
66
+ print()
67
+ print('min(T) =', np.min(T))
68
+ print()
69
+ print('mean(T) =', mean(T))
70
+ print()
71
+ print('mean(|T|) =', mean(np.abs(T)))
72
+ print()
73
+ print('var(T) =', var(np.abs(T)))
74
+ print()
75
+
76
+ # Histogram of the entries of T.
77
+ plt.hist(T.flatten(), bins=50)
78
+ plt.title('Tensor histogram')
79
+ plt.xlabel('Tensor values')
80
+ plt.ylabel('Quantity')
81
+ plt.show()
82
+ print()
83
+
84
+ # Bounds on rank.
85
+ sorted_dims = sort(array(dims))
86
+ R = mlinalg.multiply_dims(sorted_dims[1:])
87
+ print(1, '<= rank(T) <=', R)
88
+ print()
89
+
90
+ # Show generic rank.
91
+ R_gen = int(ceil( mlinalg.multiply_dims(sorted_dims)/(np.sum(sorted_dims) - L + 1) ))
92
+ print('Generic rank of the tensor space of T =', R_gen)
93
+ print()
94
+
95
+ # Multilinear rank.
96
+ class options:
97
+ display = 3
98
+
99
+ options = aux.make_options(options)
100
+ print('Computing multilinear rank...')
101
+ print('------------------------------------')
102
+ S, U, UT, sigmas, rel_error = cmpr.mlsvd(T, Tsize, R_gen, options)
103
+ print('multirank(T) =', S.shape)
104
+ print('|T - (U_1, ..., U_' + str(L) + ')*S|/|T| =', rel_error)
105
+ print()
106
+
107
+ # Estimate of the rank of T.
108
+ print('Computing rank...')
109
+ r, error_per_rank = tfx.rank(T, plot=False)
110
+ print()
111
+
112
+ return
113
+
114
+
115
+ def rank1_plot(X, Y, Z, m, n, R, k=0, num_rows=5, num_cols=5, greys=True, rgb=False, save=False):
116
+ """
117
+ This function generates an image with the frontal sections of all rank one terms (in coordinates) of some CPD. It
118
+ also saves the image in a file.
119
+ Warning: this functions uses a lot of memory.
120
+
121
+ Inputs
122
+ ------
123
+ X, Y, Z: 2-D arrays
124
+ Their are the CPD of some third order tensor.
125
+ m, n, R: int
126
+ k: int
127
+ Slice we want to visualize.
128
+ num_rows, num_cols: int
129
+ The dimensions of the grid of subplots. We recommend using squares grids in order to maximize the size of each
130
+ subplot. Some blank squares may be left at the end.
131
+ greys: bool
132
+ If True (default), it will show all slices in gray scale. Otherwise it will show the RGB evolution of the
133
+ slices.
134
+ In this case the parameter 'rgb' should be set to True.
135
+ rgb: bool
136
+ If True, it will show all the RGB evolution of the slices. Default is rgb=False.
137
+ """
138
+
139
+ sections = mlinalg.rank1(X, Y, Z, m, n, R, k)
140
+ r = 0
141
+ count = 0
142
+
143
+ while r < R:
144
+ fig, ax = plt.subplots(num_rows, num_cols, figsize=(30, 30), sharex='col', sharey='row')
145
+ for i in range(num_rows):
146
+ for j in range(num_cols):
147
+ ax[i, j].xaxis.set_major_locator(plt.NullLocator())
148
+ ax[i, j].yaxis.set_major_locator(plt.NullLocator())
149
+ if r < R:
150
+ if greys:
151
+ temp = sections[:, :, r]
152
+ ax[i, j].imshow(temp, cmap='gray')
153
+ elif rgb:
154
+ temp = zeros((m, n, 3))
155
+ temp[:, :, k] = sections[:, :, r]
156
+ ax[i, j].imshow(array(temp, dtype=uint8))
157
+ else:
158
+ return
159
+ r += 1
160
+
161
+ if save:
162
+ name = 'fig_' + str(count) + '.png'
163
+ plt.savefig(name)
164
+ count += 1
165
+
166
+ return
167
+
168
+
169
+ def rank_progress(X, Y, Z, m, n, R, k=0, greys=True, rgb=False):
170
+ """
171
+ Plots the partial sums of rank one terms corresponding to the k-th slice of the CPD. The last image should match the
172
+ original CPD. Use rgb=True only for tensors of the form (m, n, 3) encoding RGB format. The program will display the
173
+ red rank one terms, then it will add the green rank one terms and then the blue rank one terms. This ordering may
174
+ cause some distortions on the final image.
175
+
176
+ Inputs
177
+ ------
178
+ X, Y, Z: 2-D arrays
179
+ Their are the CPD of some third order tensor.
180
+ m, n, p, R: int
181
+ k: int
182
+ Slice we want to visualize.
183
+ greys: bool
184
+ If True (default), it will show all slices in gray scale. Otherwise it will show the RGB evolution of the
185
+ slices. In this case the parameter 'rgb' should be set to True.
186
+ rgb: bool
187
+ If True, it will show all the RGB evolution of the slices. False is default.
188
+ """
189
+
190
+ if greys:
191
+ temp = zeros((m, n))
192
+ sections = mlinalg.rank1(X, Y, Z, m, n, R, k)
193
+ for r in range(R):
194
+ temp = temp + sections[:, :, r]
195
+ plt.imshow(temp, cmap='gray')
196
+ name = 'factor_' + str(r+1) + '.png'
197
+ plt.savefig(name)
198
+ plt.show()
199
+
200
+ elif rgb:
201
+ count = 0
202
+ temp = zeros((m, n, 3))
203
+ for color_choice in [0, 1, 2]:
204
+ sections = mlinalg.rank1(X, Y, Z, m, n, R, color_choice)
205
+ for r in range(R):
206
+ temp[:, :, color_choice] = temp[:, :, color_choice] + sections[:, :, r]
207
+ plt.imshow(array(temp, dtype=uint8))
208
+ name = 'factor_' + str(count) + '.png'
209
+ plt.savefig(name)
210
+ plt.show()
211
+ count += 1
212
+
213
+ else:
214
+ return
215
+
216
+ return
217
+
218
+
219
+ @njit(nogil=True)
220
+ def adjust(S, m, n, p):
221
+ """
222
+ A CPD of a rgb image will have approximated values, not integers in the range [0, 255]. This function fix this.
223
+ """
224
+
225
+ for i in range(m):
226
+ for j in range(n):
227
+ for k in range(p):
228
+ S[i, j, k] = floor(S[i, j, k])
229
+ if S[i, j, k] > 255:
230
+ S[i, j, k] = 255
231
+ elif S[i, j, k] < 0:
232
+ S[i, j, k] = 0
233
+
234
+ return S
235
+
236
+
237
+ def test_tensors(tensors_list, options_list, trials, display):
238
+ """
239
+ Each element in the list 'tensors_list' is a tuple (name, T, R, thr) or (name, T, T_noise, R, thr). We have that
240
+ - 'name' is any name for the tensor
241
+ - ' T is the tensor itself
242
+ - R is the rank
243
+ - thr is a threshold for the relative error to be considered a success if smaller than thr.
244
+ In the case we are working with noise, then T_noise is the noisy tensor. The CPD will be computed for T_noise but
245
+ the error will be computed for T.
246
+ Each element of 'options_list' is a class of options to use in the respective CPD computation of the corresponding
247
+ tensor in 'tensors_list'. More precisely, options_list[i] are the options to used for the COD of tensors_list[i].
248
+ If the user want to use default options, just pass options_list as a list of False with same length of tensors_list.
249
+ """
250
+
251
+ warnings.filterwarnings('ignore')
252
+ timings = np.zeros(len(tensors_list))
253
+ errors_per_tensor = np.zeros(trials)
254
+ errors_mean_good = np.zeros(len(tensors_list))
255
+ errors_var_good = np.zeros(len(tensors_list))
256
+ errors_mean_bad = np.zeros(len(tensors_list))
257
+ errors_var_bad = np.zeros(len(tensors_list))
258
+ num_good = np.zeros(len(tensors_list))
259
+ num_bad = np.zeros(len(tensors_list))
260
+ names = []
261
+
262
+ i = 0
263
+ df = pd.DataFrame(columns=['Name',
264
+ 'Method',
265
+ 'Maxiter',
266
+ 'Tol error',
267
+ 'Tol step size',
268
+ 'Tol improvement',
269
+ 'Tol gradient',
270
+ 'Initialization',
271
+ 'Inner algorithm',
272
+ 'Bi-CPD parameters',
273
+ '# Success',
274
+ '# Fail'])
275
+
276
+ for element in tensors_list:
277
+ if len(element) == 4:
278
+ name = element[0]
279
+ T = element[1]
280
+ R = element[2]
281
+ thr = element[3]
282
+ elif len(element) == 5:
283
+ name = element[0]
284
+ T = element[1]
285
+ T_noise = element[2]
286
+ R = element[3]
287
+ thr = element[4]
288
+ else:
289
+ print('Error in list of tensors.')
290
+ break
291
+
292
+ names.append(element[0])
293
+ Tsize = np.linalg.norm(T)
294
+ options = options_list[i]
295
+
296
+ start = time.time()
297
+
298
+ for t in range(trials):
299
+ if len(element) == 4:
300
+ factors, output = tfx.cpd(T, R, options)
301
+ else:
302
+ factors, output = tfx.cpd(T_noise, R, options)
303
+ T_approx = tfx.cnv.cpd2tens(factors)
304
+ errors_per_tensor[t] = np.linalg.norm(T - T_approx)/Tsize
305
+
306
+ end = time.time()
307
+ timings[i] = (end - start)/trials
308
+
309
+ if np.sum(errors_per_tensor < thr) != 0:
310
+ errors_mean_good[i] = errors_per_tensor[errors_per_tensor < thr].mean()
311
+ errors_var_good[i] = errors_per_tensor[errors_per_tensor < thr].var()
312
+ num_good[i] = np.sum(errors_per_tensor < thr)
313
+ else:
314
+ errors_mean_good[i] = None
315
+ errors_var_good[i] = 0
316
+ num_good[i] = None
317
+
318
+ if np.sum(errors_per_tensor >= thr) != 0:
319
+ errors_mean_bad[i] = errors_per_tensor[errors_per_tensor >= thr].mean()
320
+ errors_var_bad[i] = errors_per_tensor[errors_per_tensor >= thr].var()
321
+ num_bad[i] = np.sum(errors_per_tensor >= thr)
322
+ else:
323
+ errors_mean_bad[i] = None
324
+ errors_var_bad[i] = 0
325
+ num_bad[i] = None
326
+
327
+ num_good_print = num_good[i]
328
+ num_bad_print = num_bad[i]
329
+
330
+ if np.isnan(num_good[i]):
331
+ num_bad[i] = trials
332
+ num_good_print = 0
333
+ elif np.isnan(num_bad[i]):
334
+ num_good[i] = trials
335
+ num_bad_print = 0
336
+
337
+ method = output.options.method
338
+ maxiter = output.options.maxiter
339
+ tol = output.options.tol
340
+ tol_step = output.options.tol_step
341
+ tol_improv = output.options.tol_improv
342
+ tol_grad = output.options.tol_grad
343
+ init = output.options.initialization
344
+ if type(init) == list:
345
+ init = 'user'
346
+ if output.options.method == 'als' or output.options.method == 'ttcpd':
347
+ temp1 = ['', '', '']
348
+ elif output.options.inner_method == 'cg' or output.options.inner_method == 'cg_static':
349
+ temp1 = [output.options.inner_method, output.options.cg_factor, output.options.cg_tol]
350
+ elif output.options.inner_method == 'gd':
351
+ temp1 = [output.options.inner_method, '', '']
352
+ else:
353
+ temp1 = ['hybrid strategy', '', '', '']
354
+ if output.options.method == 'ttcpd':
355
+ temp2 = output.options.bi_method_parameters
356
+ else:
357
+ temp2 = ' '
358
+ df.loc[i] = [name,
359
+ method,
360
+ maxiter,
361
+ tol,
362
+ tol_step,
363
+ tol_improv,
364
+ tol_grad,
365
+ init,
366
+ [temp1[0], temp1[1], temp1[2]],
367
+ [temp2[0], temp2[1], temp2[2]],
368
+ int(num_good_print),
369
+ int(num_bad_print)]
370
+ i += 1
371
+
372
+ if display:
373
+ ipd.clear_output()
374
+ ipd.display(df)
375
+ print()
376
+
377
+ make_plots(names,
378
+ trials,
379
+ errors_mean_good, errors_var_good, num_good,
380
+ errors_mean_bad, errors_var_bad, num_bad,
381
+ timings)
382
+
383
+ return errors_mean_good, errors_var_good, num_good, errors_mean_bad, errors_var_bad, num_bad, timings
384
+
385
+
386
+ def make_plots(names,
387
+ trials,
388
+ errors_mean_good, errors_var_good, num_good,
389
+ errors_mean_bad, errors_var_bad, num_bad,
390
+ timings):
391
+ """
392
+ After the function 'test_tensors' is finished, all data is passed to this function, which makes informative plots
393
+ with the percentage of successes and failures, plus a plot with the average timings. These two functions combined
394
+ are a great tool for modelling and hyperparameter grid search.
395
+ """
396
+
397
+ plt.figure(figsize=[16, 6])
398
+
399
+ plt.errorbar(
400
+ names, errors_mean_bad,
401
+ yerr=errors_var_bad,
402
+ label="Fail",
403
+ fmt="rs",
404
+ linewidth=4,
405
+ elinewidth=2,
406
+ ecolor='k',
407
+ capsize=5,
408
+ capthick=1
409
+ )
410
+
411
+ for i in range(len(names)):
412
+ x, y = names[i], errors_mean_bad[i]
413
+ plt.annotate(str(np.round(100*num_bad[i]/trials, 1)) + ' %', # this is the text
414
+ (x, y), # this is the point to label
415
+ textcoords="offset points", # how to position the text
416
+ xytext=(0, 20), # distance from text to points (x,y)
417
+ ha='center')
418
+
419
+ plt.errorbar(
420
+ names, errors_mean_good,
421
+ yerr=errors_var_good,
422
+ label="Success",
423
+ fmt="bs",
424
+ linewidth=4,
425
+ elinewidth=2,
426
+ ecolor='k',
427
+ capsize=5,
428
+ capthick=1
429
+ )
430
+
431
+ for i in range(len(names)):
432
+ x, y = names[i], errors_mean_good[i]
433
+ plt.annotate(str(np.round(100*num_good[i]/trials, 1)) + ' %', # this is the text
434
+ (x, y), # this is the point to label
435
+ textcoords="offset points", # how to position the text
436
+ xytext=(0, -20), # distance from text to points (x,y)
437
+ ha='center')
438
+
439
+ plt.legend(numpoints=1, loc='center left')
440
+ plt.yscale('log')
441
+ plt.ylim( (max(1e-16, np.nanmin(errors_mean_good)/10), max(1, np.nanmax(errors_mean_bad)*10)) )
442
+ plt.grid()
443
+ plt.show()
444
+
445
+ plt.figure(figsize=[16, 6])
446
+ plt.plot(names, timings, '--')
447
+ plt.plot(names, timings, 'ks')
448
+ plt.grid()
449
+ plt.ylabel('Seconds (average)')
450
+ plt.show()
451
+
452
+ return
453
+
454
+
455
+ def show_options(output):
456
+ """
457
+ This function display all parameter options used in a previous call of the cpd function.
458
+ """
459
+
460
+ members_names = [attr for attr in dir(output.options) if not attr.startswith("__")]
461
+ members = [getattr(output.options, attr) for attr in dir(output.options) if not attr.startswith("__")]
462
+ L = len(members)
463
+ for m in range(L):
464
+ print(members_names[m], ':', members[m])
465
+
466
+ return