-
Notifications
You must be signed in to change notification settings - Fork 3
/
tensor.m
561 lines (471 loc) · 15.5 KB
/
tensor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
%% Tensor Algebra Using MATLAB
classdef tensor
methods(Static)
%% Vectorize
% This function computes the vec operator of a given matrix or tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: May 2022
function Y = vec(X)
Y = reshape(X,[],1);
end
%% Hadamard Product
% This function computes the Hadarmard Product of two given matrices.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: January 2022
function C = mtx_prod_had(A,B)
[ia,ja] = size(A);
[ib,jb] = size(B);
if (ia ~= ib) || (ja~=jb)
disp('Invalid Matrices!')
return;
else
C = A.*B;
%C = zeros(ia,ja);
%for i = 1:ia
%for j = 1:ja
%C(i,j) = A(i,j)*B(i,j);
%end
%end
end
end
%% Kronecker Product
% This function computes the Kronecker Product of two given matrices.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: January 2022
function C = mtx_prod_kron(A,B)
[ia,ja] = size(A);
[ib,jb] = size(B);
A = repelem(A,ib,jb);
B = repmat(B,[ia ja]);
C = A.*B;
end
%% Khatri-Rao Product
% This function computes the Khatri-Rao Product of two given matrices.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: January 2022
function C = mtx_prod_kr(A,B)
[ia,ja] = size(A);
[ib,jb] = size(B);
if (ja~=jb)
disp('Invalid Matrices!')
return;
else
C = zeros(ia*ib,ja);
for j = 1:ja
C(:,j) = tensor.mtx_prod_kron(A(:,j),B(:,j));
end
end
end
%% Least-Squares Khatri-Rao Factorization (LSKRF)
% This function computes the LSKRF of a given matrix.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: February 2022
function [Ahat,Bhat] = LSKRF(C,ia,ib)
[~, jc] = size(C);
Ahat = complex(zeros(ia,jc),0);
Bhat = complex(zeros(ib,jc),0);
for j = 1:jc
Cp = C(:,j);
Cp = reshape(Cp, [ib ia]);
[U,S,V] = svd(Cp);
Ahat(:,j) = sqrt(S(1,1)).*conj(V(:,1));
Bhat(:,j) = sqrt(S(1,1)).*U(:,1);
end
end
%% Least-Square Kronecker Product Factorization (LSKronF)
% This function computes the LSKronF of a given matrix.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: February 2022
function [Ahat,Bhat] = LSKronF(C,ia,ja,ib,jb)
[ic,jc] = size(C);
I = (ic/ia) + zeros(1,ia);
J = (jc/ja) + zeros(1,ja);
blocks_of_C = mat2cell(C,I,J);
k = 1;
Chat = complex(zeros(ib*jb,ia*ja),0);
for j = 1:ja
for i = 1:ia
vec_of_block = cell2mat(blocks_of_C(i,j));
vec_of_block = vec_of_block(:);
Chat(:,k) = vec_of_block;
k = k + 1;
end
end
[U,S,V] = svd(Chat);
ahat = sqrt(S(1,1)).*conj(V(:,1));
bhat = sqrt(S(1,1)).*U(:,1);
Ahat = reshape(ahat,[ia ja]);
Bhat = reshape(bhat, [ib jb]);
end
%% Kronecker Product Single Value Decomposition (KPSVD)
% This function computes the LSKRF of a given matrix.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: March 2022
function [U,S,V,rkp] = KPSVD(X,ia,ja,ib,jb)
[ix,jx] = size(X);
I = (ix/ia) + zeros(1,ia);
J = (jx/ja) + zeros(1,ja);
blocks_of_X = mat2cell(X,I,J);
k = 1;
Xhat = complex(zeros(ib*jb,ia*ja),0);
for j = 1:ja
for i = 1:ia
vec_of_block = cell2mat(blocks_of_X(i,j));
vec_of_block = vec_of_block(:);
Xhat(:,k) = vec_of_block;
k = k + 1;
end
end
[U,S,V] = svd(Xhat');
rkp = sum(sum(S>0));
end
%% Unfolding
% This function computes the unfolding of a given tensor in its matrix.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: April 2022
function [A] = unfold(ten,mode)
dim = size(ten);
order = 1:numel(dim);
order(mode) = [];
order = [mode order];
A = reshape(permute(ten,order), dim(mode), prod(dim)/dim(mode));
end
%% Folding
% This function computes the folding of a given matrix into its tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: April 2022
% It's interesting to see how the dimmensions get swapped by the unfolding
% so the understanding of the code is clear.
function [ten] = fold(A,dim,mode)
order = 1:numel(dim);
order(mode) = [];
order = [mode order];
dim = dim(order);
ten = reshape(A,dim);
if mode == 1
ten = permute(ten,order);
else
order = 1:numel(dim);
for i = 2:mode
order([i-1 i]) = order([i i-1]);
end
ten = permute(ten,order);
end
end
%% N-mode Product
% This function computes n-mode product of a set of matrices and a tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: April 2022
function [ten] = n_mod_prod(ten,matrices,modes)
dim = size(ten);
number = numel(matrices);
if nargin < 3
modes = 1:number;
end
for i = modes
ten = cell2mat(matrices(i))*tensor.unfold(ten,i);
[aux,~] = size(cell2mat(matrices(i)));
dim(i) = aux;
ten = tensor.fold(ten,[dim],i);
end
end
%% Full High Order Single Value Decomposition (HOSVD)
% This function computes the Truncated HOSVD of a given tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: April 2022
function [S,U] = HOSVD_full(ten)
number = numel(size(ten));
for i = 1:number
[aux,~,~] = svd(tensor.unfold(ten,i));
%[aux,~,~] = svd(tens2mat(ten,i));
U{i} = aux;
end
% Core tensor uses the hermitian operator.
Ut = cellfun(@(x) x', U,'UniformOutput',false);
S = tensor.n_mod_prod(ten,Ut);
% The normal factors should be transposed.
U = cellfun(@(x) x, U,'UniformOutput',false);
end
%% Truncated High Order Single Value Decomposition (HOSVD)
% This function computes the Truncated HOSVD of a given tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: April 2022
function [S,U] = HOSVD_truncated(ten,ranks)
if nargin < 2
number = numel(size(ten));
for i = 1:number
[aux,eig,~] = svd(tensor.unfold(ten,i));
[aa,~] = size(eig(eig > eps));
aux = aux(:,1:aa);
U{i} = aux;
end
% Core tensor uses the hermitian operator.
Ut = cellfun(@(x) x', U,'UniformOutput',false);
S = tensor.n_mod_prod(ten,Ut);
% The normal factors should be transposed.
U = cellfun(@(x) x, U,'UniformOutput',false);
else
number = numel(size(ten));
for i = 1:number
[aux,~,~] = svd(tensor.unfold(ten,i));
aux = aux(:,1:ranks(i));
U{i} = aux;
end
% Core tensor uses the hermitian operator.
Ut = cellfun(@(x) x',U,'UniformOutput',false);
S = tensor.n_mod_prod(ten,Ut);
% The normal factors should be transposed.
U = cellfun(@(x) x,U,'UniformOutput',false);
end
end
%% High Order Orthogonal Iteration (HOOI)
% This function computes the HOOI of a given tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: May 2022
function [S,U,k] = HOOI_full(ten)
max_iter = 10;
[~, U] = tensor.HOSVD_full(ten);
number = numel(size(ten));
for k = 1:max_iter
for i = 1:number
modes = 1:number;
modes(i) = []; % It will skip this mode in the n_mod_prod.
Un = tensor.n_mod_prod(ten,U,modes);
[aux,~,~] = svd(tensor.unfold(Un,i));
U{i} = aux;
end
end
% The conjugate transpose
Ut = cellfun(@(x) x', U ,'UniformOutput',false);
S = tensor.n_mod_prod(ten,Ut);
end
%% Truncated High Order Orthogonal Iteration (HOOI)
% This function computes the Truncated HOOI of a given tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: May 2022
function [S,U] = HOOI_truncated(ten,ranks)
if nargin < 2
max_iter = 10;
[~, U] = tensor.HOSVD_full(ten);
number = numel(size(ten));
for k = 1:max_iter
for i = 1:number
modes = 1:number;
modes(i) = []; % It will skip this mode in the n_mod_prod.
Un = tensor.n_mod_prod(ten,U,modes);
[aux,eig,~] = svd(tensor.unfold(Un,i));
[aa,~] = size(eig(eig > eps));
U{i} = aux(:,1:aa);
end
end
% The conjugate transpose
Ut = cellfun(@(x) x', U ,'UniformOutput',false);
S = tensor.n_mod_prod(ten,Ut);
else
max_iter = 10;
[~, U] = tensor.HOSVD_full(ten);
number = numel(size(ten));
for k = 1:max_iter
for i = 1:number
modes = 1:number;
modes(i) = []; % It will skip this mode in the n_mod_prod.
Un = tensor.n_mod_prod(ten,U,modes);
[aux,~,~] = svd(tensor.unfold(Un,i));
U{i} = aux(:,1:ranks(i));
end
end
% The conjugate transpose
Ut = cellfun(@(x) x', U ,'UniformOutput',false);
S = tensor.n_mod_prod(ten,Ut);
end
end
%% Multidimensional Least-Squares Khatri-Rao Factorization (MLS-KRF)
% This function computes the MLS-KRF of a given matrix.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: March 2022
% The dimensions should be inserted in the order that the products are
% performed.
function [A] = MLSKRF(X,N,dim)
[~,R] = size(X);
for r = 1:R
xr = X(:,r);
tenXr = reshape(xr,flip(dim));
% Aplicar SVDs consecutivas em estrategia recursiva? Como lidar com
% o nd array nesse caso?
[Sr,Ur] = tensor.HOSVD_full(tenXr);
for n = 1:N
Ar{r,n} = (Sr(1)^(1/N))*Ur{N - n + 1}(:,1);
end
end
for n = 1:N
aux = cell2mat(Ar(:,n));
A{n} = reshape(aux,[dim(n) R]);
end
end
%% Multidimensional Least-Squares Kronecker Factorization (MLS-KronF)
% This function computes the MLS-KronF of a given matrix.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: March 2022
% It is interesting to note that this process could easily be applied to a
% random number of matrices in a iterative form considering groups of 3 objects.
% init controls the initialization: 1 for HOSVD and 2 for HOOI.
function [Ahat] = MLSKronF(X,rows,columns,init)
% 3rd structure
%[ix,jx] = size(X);
% 2nd structure
I = rows(2)*rows(3) + zeros(1,rows(1));
J = columns(2)*columns(3) + zeros(1,columns(1));
blocks_of_X = mat2cell(X,I,J);
% 1st structure
Z = 1;
for j = 1:columns(1)
for i = 1:rows(1)
aux = cell2mat(blocks_of_X(i,j));
I = rows(3) + zeros(1,rows(2));
J = columns(3) + zeros(1,columns(2));
blocks_of_aux = mat2cell(aux,I,J);
for jj = 1:columns(2)
for ii = 1:rows(2)
vec_of_block = cell2mat(blocks_of_aux(ii,jj));
vec_of_block = vec_of_block(:);
mtx_1st(:,ii,jj) = vec_of_block;
end
end
Xhat(:,Z) = reshape(mtx_1st,[],1);
Z = Z + 1;
end
end
tenXhat = reshape(Xhat,[rows(3)*columns(3), rows(2)*columns(2), rows(1)*columns(1)]);
if init == '1'
[S,U] = tensor.HOSVD_full(tenXhat);
elseif init == '2'
[S,U] = tensor.HOOI_full(tenXhat);
end
rows = flip(rows);
columns = flip(columns);
for u = 1:length(U)
index = length(U) - u + 1;
aux = (S(1)^(1/length(U)))*U{u}(:,1);
Ahat{index} = reshape(aux,[rows(u) columns(u)]);
end
end
%% Alternate Least-Square (ALS)
% This function computes the ALS of a given tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: April 2022
function [Ahat,Bhat,Chat,error] = ALS(X,R)
I = zeros(R,R,R);
for i = 1:R
I(i,i,i) = 1;
end
[ia,ib,ic] = size(X);
mode_1 = tensor.unfold(X,1);
mode_2 = tensor.unfold(X,2);
mode_3 = tensor.unfold(X,3);
Ahat = randn(ia,R) + 1j*randn(ia,R);
Bhat = randn(ib,R) + 1j*randn(ib,R);
Chat = randn(ic,R) + 1j*randn(ic,R);
aux = 1000;
error = zeros(1,aux);
error(1) = ((norm((mode_1 - Ahat*(tensor.mtx_prod_kr(Chat,Bhat).')),'fro'))^2)/((norm(mode_1,'fro')^2));
for i = 2:aux
Bhat = mode_2*pinv((tensor.mtx_prod_kr(Chat,Ahat)).');
Chat = mode_3*pinv((tensor.mtx_prod_kr(Bhat,Ahat)).');
Ahat = mode_1*pinv((tensor.mtx_prod_kr(Chat,Bhat)).');
error(i) = ((norm((mode_1 - Ahat*(tensor.mtx_prod_kr(Chat,Bhat).')),'fro'))^2)/((norm(mode_1,'fro')^2));
if abs(error(i) - error(i-1)) < eps
error = error(1:i);
break;
else
continue;
end
end
end
%% Tensor Kronecker Product
% This function computes the Tensor Kronecker Product of two given tensors.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: June 2022
function C = ten_prod_kron(A,B)
aux1 = num2cell([size(A)]);
aux2 = num2cell([size(B)]);
A = repelem(A,aux2{:});
B = repmat(B,aux1{:});
C = A.*B;
end
%% Tensor Kronecker Product Single Value Decomposition (TKPSVD)
% This function computes the TKPSVD of a tensor.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: June 2022
function [Ahat,Bhat,Chat] = TKPSVD(tenX,tenSize,tenDim,N,R)
% First reorder
var = 1:length(tenSize);
for n = 1:N
tenXhatreorder{n} = cell2mat(tenSize(var(n:N:length(tenSize))));
end
tenXhatreorder = cell2mat(tenXhatreorder);
tenXhat = reshape(tenX,tenXhatreorder);
% Second reorder
var = 1:length(tenSize);
for n = 1:N
tenXhatpermute{n} = var(n:N:length(tenSize));
end
tenXhatpermute = cell2mat(tenXhatpermute);
tenXhat = permute(tenXhat,tenXhatpermute);
% Third reorder
tenXhat = reshape(tenXhat,tenDim{:});
% ALS estimation
[Ahat,Bhat,Chat,~] = tensor.ALS(tenXhat,R);
end
%% Tensor Contraction
% This function computes the tensor contraction between a tensor and a matrix
% or between two tensors.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: July 2022
function [tenZ] = contraction(tenX,n1,tenY,n2)
% Obtaining the unfolds
tenX_n = tensor.unfold(tenX,n1);
tenY_n = tensor.unfold(tenY,n2);
% Obtaining the unfold of the contracted tensor
tenZ_n = (tenX_n.')*tenY_n;
% Obtaining the fold of the contracted tensor
dim_x = size(tenX);
dim_x(n1) = [];
dim_y = size(tenY);
dim_y(n2) = [];
dim_z = [dim_x dim_y];
tenZ = tensor.fold(tenZ_n,dim_z,1);
end
%% Tensor Train Single Value Decomposition (TT-SVD)
% This function computes the TT-SVD of a fourth order tensor but can be extended later.
% Author: Kenneth B. dos A. Benicio <kenneth@gtel.ufc.br>
% Created: July 2022
function [G] = TTSVD(tenX,Ranks)
X_size = size(tenX);
% Step 1
X1 = tensor.unfold(tenX,1);
[U1,S1,V1] = svd(X1,'econ');
G1 = U1*sqrt(S1);
G1 = G1(:,1:Ranks(1));
G{1} = G1;
% Step 2
V1 = sqrt(S1)*V1';
V1 = V1(1:Ranks(1),:);
X2 = reshape(V1,[Ranks(1)*X_size(2) X_size(3)*X_size(4)]);
[U2,S2,V2] = svd(X2,'econ');
G2 = U2*sqrt(S2);
G2 = G2(:,1:Ranks(2));
G{2} = reshape(G2, [Ranks(1) X_size(2) Ranks(2)]);
% Step 3
V2 = sqrt(S2)*V2';
V2 = V2(1:Ranks(2),:);
X3 = reshape(V2,[Ranks(2)*X_size(3) X_size(4)]);
[U3,S3,V3] = svd(X3,'econ');
G3 = U3*sqrt(S3);
G3 = G3(:,1:Ranks(3));
G{3} = reshape(G3, [Ranks(2) X_size(3) Ranks(3)]);
V3 = sqrt(S3)*V3';
V3 = V3(1:Ranks(3),:);
G{4} = V3;
end
end
end