-
Notifications
You must be signed in to change notification settings - Fork 36
/
RepLearnFINAL.m
90 lines (71 loc) · 2.5 KB
/
RepLearnFINAL.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
function [Zexact, Ztop5, Ztop10, Ztop20, Z99per, Z98per, Z97per, Z95per, Z90per, Z85per, Z80per, DistComp, RuntimeNystrom, RuntimeFD]=RepLearnFINAL(X, Dictionary, gamma)
% Input
% X: original data (nxm)
% Dictionary: kShape's centroids (cxm) or randomly chosen time series
% Dim: Dimensions to keep in the end over the learned representation
% gamma: kernel's parameter
% Output
% Ktilde: Approximated kernel matrix (nxn)
% Z: New learned representation (nxDim)
tic;
DistComp = 0;
[nrowsX, ncolumnsX] = size(X);
[nrowsDic, ncolumnsDic] = size(Dictionary);
W = zeros(nrowsDic,nrowsDic);
for i=1:nrowsDic
for j=1:nrowsDic
W(i,j) = SINK(Dictionary(i,:),Dictionary(j,:),gamma);
DistComp = DistComp 1;
end
end
E = zeros(nrowsX,nrowsDic);
for i=1:nrowsX
disp(i);
for j=1:nrowsDic
E(i,j) = SINK(X(i,:),Dictionary(j,:),gamma);
DistComp = DistComp 1;
end
end
[Ve, Va] = eig(W);
va = diag(Va);
inVa = diag(va.^(-0.5));
Zexact = E * Ve * inVa;
RuntimeNystrom = toc;
Zexact = CheckNaNInfComplex(Zexact);
tic;
[BSketch, ~] = FrequentDirections(Zexact, ceil(0.5*size(Zexact,2)));
[V2, L2] = eig(BSketch'*BSketch);
%[V2, L2] = eig(Zexact'*Zexact);
eigvalue = diag(L2);
[dump, index] = sort(-eigvalue);
eigvalue = eigvalue(index);
V2 = V2(:, index);
RuntimeFD = toc;
VarExplainedCumSum = cumsum(eigvalue)/sum(eigvalue);
DimFor99 = find(VarExplainedCumSum>=0.99,1);
DimFor98 = find(VarExplainedCumSum>=0.98,1);
DimFor97 = find(VarExplainedCumSum>=0.97,1);
DimFor95 = find(VarExplainedCumSum>=0.95,1);
DimFor90 = find(VarExplainedCumSum>=0.90,1);
DimFor85 = find(VarExplainedCumSum>=0.85,1);
DimFor80 = find(VarExplainedCumSum>=0.80,1);
Ztop5 = CheckNaNInfComplex( Zexact*V2(:,1:5) );
Ztop10 = CheckNaNInfComplex( Zexact*V2(:,1:10) );
Ztop20 = CheckNaNInfComplex( Zexact*V2(:,1:20) );
Z99per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor99) );
Z98per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor98) );
Z97per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor97) );
Z95per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor95) );
Z90per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor90) );
Z85per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor85) );
Z80per = CheckNaNInfComplex( Zexact*V2(:,1:DimFor80) );
end
function Z = CheckNaNInfComplex(Z)
for i=1:size(Z,1)
for j=1:size(Z,2)
if (isnan(Z(i,j)) || isinf(Z(i,j)) || ~isreal(Z(i,j)))
Z(i,j)=0;
end
end
end
end