-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexampleDesai.m
132 lines (117 loc) · 3.38 KB
/
exampleDesai.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
% Reproduce Figure 5 Desai et al. using Caltech Birds dataset
%
% Desai, Ghashami, & Phillips (2016). Improved practical matrix sketching
% with guarantees. IEEE Transactions on Knowledge & Data Engineering,
% 28(7), 1678-1690
clear
% Check that Birds data exists somewhere
if ~exist('image_attribute_labels.txt','file')
help('BirdsReader');
error('Birds data must be downloaded first');
end
% Reader for Birds data
BR = BirdsReader('filename','image_attribute_labels.txt');
% Load entire data set into memory
BR.blockSize = inf;
A = BR();
% Desai et al. demean for this dataset
A = bsxfun(@minus,A,mean(A));
k = 20:10:100;
alpha = [1 1 0 0.2 0.2];
fast = [false true false false true];
id = {'FD' 'FastFD' 'iSVD' '0.2FD' 'Fast 0.2FD'};
symbol = ['s' 'h' '*' 'p' 'd'];
color = ['b' 'g' 'r' 'c' 'm'];
tic;
[U,S,V] = svd(A);
bruteRuntime = toc;
m = 10;
Am = U(:,1:m)*S(1:m,1:m)*V(:,1:m)'; % For projection error
%% This can take a little time
count = 1;
for kk = k
for m = 1:numel(alpha)
[kk m]
if fast(m)
% In Desai et al, fast variants did not double the rows of B, and
% returned B. Their fast variants can yield sketch that sometimes
% only using half of its rows (i.e., the lower half of B may contain
% all zeroes or actual data samples). To replicate this, we halve
% k and keep the 'full' sketch.
k2 = kk/2;
fullsize = true;
else
k2 = kk;
fullsize = false;
end
sketcher = FrequentDirections(k2,'fast',fast(m),'alpha',alpha(m));
tic;
sketcher(A);
runtime(count,m) = toc;
coverr(count,m) = sketcher.coverr(A,fullsize);
projerr(count,m) = sketcher.projerr(A,Am,10,fullsize);
end
count = count + 1;
end
if 1 % Include randomized sparse variant
rng(1234);
n = size(A,1);
nbetak = 50;
reps = 10;
for r = 1:reps
count = 1;
for kk = k
k2 = kk/2;
sketcher = FrequentDirections(k2,'sparse',true);
sketcher.beta = n/nbetak/k2;
tic;
sketcher(A);
temp_runtime(count,r) = toc;
temp_coverr(count,r) = sketcher.coverr(A,true);
temp_projerr(count,r) = sketcher.projerr(A,Am,10,true);
nSVD(count,r) = sketcher.nSVD;
nSparseEmbed(count,r) = sketcher.nSparseEmbed;
count = count + 1;
end
end
id{end+1} = 'SpFD50';
symbol(end+1) = 'v';
color(end+1) = 'k';
runtime = [runtime , mean(temp_runtime,2)];
coverr = [coverr , mean(temp_coverr,2)];
projerr = [projerr , mean(temp_projerr,2)];
end
%% Plot
figure;
subplot(131);
hold on;
for i = 1:numel(id)
g1(i) = plot(k',coverr(:,i),[symbol(i) '-'],'color',color(i),...
'Markerfacecolor',color(i));
end
legend(id);
axis([min(k) max(k) 0 0.07]);
xlabel('Sketch size');
ylabel('Covariance error');
subplot(132);
hold on;
for i = 1:numel(id)
g1(i) = plot(k',projerr(:,i),[symbol(i) '-'],'color',color(i),...
'Markerfacecolor',color(i));
end
axis tight
axis([min(k) max(k) 0.975 1.175])
xlabel('Sketch size');
ylabel('Projection error');
subplot(133);
hold on;
for i = 1:numel(id)
g1(i) = plot(k',runtime(:,i),[symbol(i) '-'],'color',color(i),...
'Markerfacecolor',color(i));
end
plot([k(1) k(end)],[bruteRuntime bruteRuntime],'k--');
axis tight
yy = get(gca,'ylim');
axis([min(k) max(k) 0 yy(2)]);
xlabel('Sketch size');
ylabel('Runtime (seconds)');