-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_fpca_mask_mnist_wine.m
225 lines (178 loc) · 5.11 KB
/
test_fpca_mask_mnist_wine.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
% The script is designed to evaluate the information preservation of F-PCA
% with and without perturbation masks against the famous MNIST and WINE
% datasets. Of particular note is the information preservation under
% perturbation masks as privacy comes at the cost of accuracy. We evaluate
% across a range of epsilon and delta values which quantatively compare
% the result against the offline, and F-PCA with no mask applied.
%
% In all instances, for fair comparison, the adaptive rank estimation is
% disabled for F-PCA.
%
% Based on work of Grammenos et al.: https://arxiv.org/abs/1907.08059
%
% Author: Andreas Grammenos (ag926@cl.cam.ac.uk)
%
% Last touched date: 02/06/2020
%
% License: GPLv3
%
%% Initialisation
close all; clear; clc;
% the configuration parameters
rank = 6;
fpca_adaptive = 0;
fpca_private = 1;
fpca_blk_size = 25;
fpca_blk_size_no_mask = 25;
% the dp ranges for epsilon and delta
%epsilons = [0.1, .4];
%deltas = [0.1, 1];
epsilons = [0.1]; % 0.1, 0.6, 1, 2
deltas = [0.1];
% find the length of parameter arrays
epsilons_len = size(epsilons, 2);
deltas_len = size(deltas, 2);
% compute the number of plots
plot_num = epsilons_len * deltas_len;
% dat_desc = "Wine";
dat_desc = "MNIST";
type = sprintf("fpca-mask-%s", dat_desc);
% set the real dataset
params.type = type;
% enable printing
params.pflag = 1;
% use block error
params.use_blk_err = 1;
% print pdfs
params.pdf_print = 1;
% setup the environment
params = setup_vars(params);
if dat_desc == "MNIST"
dat = csvread('datasets\mnist.csv', 1)';
elseif dat_desc == "Wine"
dat = csvread('datasets\wine.csv', 1)';
end
% get the colors for the classes
proj_colors = dat(1, :);
% raise if we want to compute the offline PCA
compute_offline = 1;
% raise if we want to compute the MOD-SuLQ
compute_mod_sulq = 1;
%% Offline PCA
% takes a bit of time, skip if we can.
if compute_offline == 1
% get the left principal subspace
[Up, ~, ~] = svd(dat);
% project
Uproj = Up' * dat;
% grab the first PC
first_pc = Uproj(1, :);
% and the second PC
second_pc = Uproj(2, :);
fig = figure;
% now plot them.
gscatter(first_pc, second_pc, proj_colors);
legend('hide');
% titles
tt = sprintf("Offline PCA %s", dat_desc);
title(tt);
ylabel('2nd PC');
xlabel('1st PC');
st = sprintf("offline_pca_%s", dat_desc);
print_fig(fig, st, params);
end
%% F-PCA (no mask)
% set the parameters
fpca_params.adaptive = fpca_adaptive;
fpca_params.blk_size = fpca_blk_size_no_mask;
% run the F-PCA in only adaptive mode (without masks)
[U_fpca, ~, ~] = fpca_edge(dat, rank, fpca_params);
% project
Uproj = U_fpca' * dat;
% grab the first PC
first_pc = Uproj(1, :);
% and the second PC
second_pc = Uproj(2, :);
fig = figure;
% now plot them.
gscatter(first_pc, second_pc, proj_colors);
legend('hide');
% titles
tt = sprintf("F-PCA %s (no mask)", dat_desc);
title(tt);
ylabel('2nd PC');
xlabel('1st PC');
% print the figure
st = sprintf("fpca_no_mask_r_%d_%s", rank, dat_desc);
print_fig(fig, st, params);
%% F-PCA (with perturbation mask)
% handle to the global figure, if needed
fig = figure;
% run the loop to see what we get.
for i = 1:epsilons_len
for j = 1:deltas_len
% dial in the current parameters
fpca_params.private = fpca_private;
fpca_params.adaptive = fpca_adaptive;
fpca_params.blk_size = fpca_blk_size;
fpca_params.holdoff = 5;
fpca_params.e_p = epsilons(i);
fpca_params.delta = deltas(j);
% run the fpca in private, adaptive mode
[U_pfpca, ~] = fpca_edge(dat, rank, fpca_params);
% project
Uproj = U_pfpca' * dat;
% grab the first PC
first_pc = Uproj(1, :);
% and the second PC
second_pc = Uproj(2, :);
% pick the correct subplot in the grid
subplot(epsilons_len, deltas_len, (i-1) * deltas_len + j);
% now plot them.
gscatter(first_pc, second_pc, proj_colors);
legend('hide');
% titles
cap = sprintf('F-PCA %s (\\epsilon: %1.2f, \\delta: %1.2f)', ...
dat_desc, epsilons(i), deltas(j));
title(cap);
ylabel('2nd PC');
xlabel('1st PC');
end
end
% print the figure
st = sprintf("fpca_with_mask_r_%d_%s", rank, dat_desc);
print_fig(fig, st, params);
%% MOD-SuLQ
%
% F-PCA should be a bit worse, but not by much.
if compute_mod_sulq == 1
% handle to the global figure, if needed
fig = figure;
% run the loop to see what we get.
for i = 1:epsilons_len
for j = 1:deltas_len
% run mod-sulq
[U_ms, ~, ~] = svds(mod_sulq(dat, epsilons(i), deltas(j)), rank);
% project
Uproj = U_ms' * dat;
% grab the first PC
first_pc = Uproj(1, :);
% and the second PC
second_pc = Uproj(2, :);
% pick the correct subplot in the grid
subplot(epsilons_len, deltas_len, (i-1) * deltas_len + j);
% now plot them.
gscatter(first_pc, second_pc, proj_colors);
legend('hide');
% titles
cap = sprintf('MOD-SuLQ %s (\\epsilon: %1.2f, \\delta: %1.2f)', ...
dat_desc, epsilons(i), deltas(j));
title(cap);
ylabel('2nd PC');
xlabel('1st PC');
end
end
end
% print the figure
st = sprintf("mod_sulq_r_%d_%s", rank, dat_desc);
print_fig(fig, st, params);