-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMINSTnonIID.m
387 lines (260 loc) · 10.5 KB
/
MINSTnonIID.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
clear all
datanumber=1000; %% the number of data samples of each user
%%%%%%%%%%%%%%%%%%%%%%%%%%%% data processing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[trainingdata, traingnd] = mnist_parse('train-images-idx3-ubyte', 'train-labels-idx1-ubyte');
trainingdata = double(reshape(trainingdata, size(trainingdata,1)*size(trainingdata,2), []).');
traingnd = double(traingnd);
traingnd(traingnd==0)=10;
traingnd=dummyvar(traingnd);
[testdata, testgnd] = mnist_parse('t10k-images-idx3-ubyte', 't10k-labels-idx1-ubyte');
testdata = double(reshape(testdata, size(testdata,1)*size(testdata,2), []).');
testgnd = double(testgnd);
testgnd(testgnd==0)=10;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numberofneuron=50; % Number of neurons that consists of local FL model of each user
averagenumber=1; % Average number of runing simulations.
iteration=1500; % Total number of global FL iterations.
learningspeed=0.1; % Learning speed of each user
%%%%%%%%%%%%%%%%%%%%%%%% coding setting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v_fQRate = [1, 2];
v_nQuantizaers = [... % Curves
0 ... % Dithered 3-D lattice quantization
1 ... % Dithered 2-D lattice quantization
1 ... % Dithered scalar quantization
1 ... % QSGD
1 ... % Uniform quantization with random unitary rotation
1 ... % Subsampling with 3 bits quantizers
];
global gm_fGenMat2D;
global gm_fLattice2D;
% Clear lattices
gm_fGenMat2D = [];
gm_fLattice2D = [];
% Do full search over the lattice
stSettings.OptSearch = 1;
s_fRate=4;
stSettings.type =4;
stSettings.scale=2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
localiterations=1; % Number of local updates at each iteration.
finalerror=[];
averageerror=[];
kk=0;
proposed=1; % proposed=1 indicates the proposed algorithm
% proposed=0 indicates the comparison algorithm
for userno=15:3:15 % Number of users.
kk=kk+1;
usernumber=userno;
for average=1:1:averagenumber
%%%%%%%%%%%%% local model for each user, which consists of 4 matrices
wupdate=zeros(iteration,usernumber);
w=[];
lw=[];
b=[];
ob=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wnew=zeros(numberofneuron,usernumber);
lwnew=zeros(numberofneuron,usernumber);
bnew=zeros(numberofneuron,usernumber);
obnew=zeros(1,usernumber);
%%%%%%%%%%%%% global model for each user, which consists of 4 matrices
wglobal=[];
lwglobal=[];
bglobal=[];
obglobal=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% gradient of local FL models %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
deviationw=[];
deviationlw=[];
deviationb=[];
deviationob=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Building local FL model for user 1 %%%%%%%%%%%%%%%%%%%%%%%%%
net1 = patternnet(numberofneuron);
% net1.trainFcn = 'trainscg';
% net1.trainFcn = 'traingd';
net1.inputs{1}.processFcns={};
net1.outputs{2}.processFcns={};
% net1.divideFcn = '';
net1.trainParam.epochs = localiterations;
net1.trainParam.showWindow = 0;
%net1.inputs{1}.size=500;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Building local FL model for user 2 %%%%%%%%%%%%%%%%%%%%%%%%%
%net1.trainParam.lr=learningspeed;
net2 = patternnet(numberofneuron);
% net2.trainFcn = 'trainscg';
net2.inputs{1}.processFcns={};
net2.outputs{2}.processFcns={};
%net2.inputs{1}.size=500;
%
% net2.divideFcn = '';
net2.trainParam.epochs = localiterations;
net2.trainParam.showWindow = 0;
%net2.trainParam.lr=learningspeed;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Building local FL model for user 3 %%%%%%%%%%%%%%%%%%%%%%%%%
net3 = patternnet(numberofneuron);
net3.inputs{1}.processFcns={};
net3.outputs{2}.processFcns={};
net3.trainParam.showWindow = 0;
% net3.trainFcn = 'trainscg';
%net3.inputs{1}.size=500;
% net3.divideFcn = '';
net3.trainParam.epochs = localiterations;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if usernumber>3
%net3.trainParam.lr=learningspeed;
net4 = patternnet(numberofneuron);
net4.inputs{1}.processFcns={};
net4.outputs{2}.processFcns={};
% net4.divideFcn = '';
% net4.trainFcn = 'traingd';
net4.trainParam.epochs = localiterations;
net4.trainParam.showWindow = 0;
%net4.trainParam.lr=learningspeed;
net5 = patternnet(numberofneuron);
net5.inputs{1}.processFcns={};
net5.outputs{2}.processFcns={};
net5.trainParam.epochs = localiterations;
net5.trainParam.showWindow = 0;
net6 = patternnet(numberofneuron);
net6.inputs{1}.processFcns={};
net6.outputs{2}.processFcns={};
net6.trainParam.epochs = localiterations;
net6.trainParam.showWindow = 0;
if usernumber>6
net7 = patternnet(numberofneuron);
net7.inputs{1}.processFcns={};
net7.outputs{2}.processFcns={};
net7.trainParam.epochs = localiterations;
net7.trainParam.showWindow = 0;
net8 = patternnet(numberofneuron);
net8.inputs{1}.processFcns={};
net8.outputs{2}.processFcns={};
net8.trainParam.epochs = localiterations;
net8.trainParam.showWindow = 0;
net9 = patternnet(numberofneuron);
net9.inputs{1}.processFcns={};
net9.outputs{2}.processFcns={};
net9.trainParam.epochs = localiterations;
net9.trainParam.showWindow = 0;
if usernumber>9
net10 = patternnet(numberofneuron);
net10.inputs{1}.processFcns={};
net10.outputs{2}.processFcns={};
net10.trainParam.epochs = localiterations;
net10.trainParam.showWindow = 0;
net11 = patternnet(numberofneuron);
net11.inputs{1}.processFcns={};
net11.outputs{2}.processFcns={};
net11.trainParam.epochs = localiterations;
net11.trainParam.showWindow = 0;
net12 = patternnet(numberofneuron);
net12.inputs{1}.processFcns={};
net12.outputs{2}.processFcns={};
net12.trainParam.epochs = localiterations;
net12.trainParam.showWindow = 0;
if usernumber>12
net13 = patternnet(numberofneuron);
net13.inputs{1}.processFcns={};
net13.outputs{2}.processFcns={};
net13.trainParam.epochs = localiterations;
net13.trainParam.showWindow = 0;
net14 = patternnet(numberofneuron);
net14.inputs{1}.processFcns={};
net14.outputs{2}.processFcns={};
net14.trainParam.epochs = localiterations;
net14.trainParam.showWindow = 0;
net15 = patternnet(numberofneuron);
net15.inputs{1}.processFcns={};
net15.outputs{2}.processFcns={};
net15.trainParam.epochs = localiterations;
net15.trainParam.showWindow = 0;
end
end
end
end
for i=1:1:iteration
for user=1:1:usernumber
x1=[trainingdata(1,:);trainingdata(1+(user-1)*datanumber:user*datanumber,:)]; % Input of local FL model
y1=[traingnd(1,:);traingnd(1+(user-1)*datanumber:user*datanumber,:)]; % Output of local FL model
clear netvaluable;
Winstr1=strcat('net',int2str(user));
eval(['netvaluable','=',Winstr1,';']);
if i > 1
% Let global FL model to be the local FL model of each user, which is
% equal to that the BS transmits the global FL model to the users
netvaluable.IW{1,1}=wglobal;
netvaluable.LW{2,1}=lwglobal;
netvaluable.b{1,1}=bglobal;
netvaluable.b{2,1}=obglobal;
end
oldnetvaluable=netvaluable;
[netvaluable,tr] = train(netvaluable,x1',y1'); % Train local FL model.
if i==1
wglobal=zeros(size(netvaluable.IW{1,1}));
lwglobal=zeros(size(netvaluable.LW{2,1}));
bglobal=zeros(size(netvaluable.b{1,1}));
obglobal=zeros(size(netvaluable.b{2,1}));
end
% Record trained local FL model.
w(:,:,user)=netvaluable.IW{1,1};
lw(:,:,user)=netvaluable.LW{2,1};
b(:,:,user)=netvaluable.b{1,1};
ob(:,:,user)=netvaluable.b{2,1};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if proposed==1
%%%%%%%%%%%%% Calculate the gradient of local FL model of each user%%%%%%%
if i==1
deviationw(:,:,user)=netvaluable.IW{1,1};
deviationlw(:,:,user)=netvaluable.LW{2,1};
deviationb(:,:,user)=netvaluable.b{1,1};
deviationob(:,:,user)=netvaluable.b{2,1};
else
deviationw(:,:,user)=netvaluable.IW{1,1}-oldnetvaluable.IW{1,1};
deviationlw(:,:,user)=netvaluable.LW{2,1}-oldnetvaluable.LW{2,1};
deviationb(:,:,user)=netvaluable.b{1,1}-oldnetvaluable.b{1,1};
deviationob(:,:,user)=netvaluable.b{2,1}-oldnetvaluable.b{2,1};
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m_fH1 = [deviationw(:,:,user),deviationb(:,:,user)];
[m_fHhat1, ~] = m_fQuantizeData(m_fH1, s_fRate, stSettings); % coding and decoding
m_fH2 = [deviationlw(:,:,user),deviationob(:,:,user)];
[m_fHhat2, ~] = m_fQuantizeData(m_fH2, s_fRate, stSettings); % coding and decoding
%%%%%%%%%%%%% Calculate the gradient after decoding %%%%%%%
deviationwnew(:,:,user)= m_fHhat1(:,1:length(netvaluable.IW{1,1}));
deviationlwnew(:,:,user)= m_fHhat2(:,1:length(netvaluable.LW{2,1}));
deviationbnew(:,:,user)=m_fHhat1(:,length(netvaluable.IW{1,1})+1);
deviationobnew(:,:,user)=m_fHhat2(:,length(netvaluable.LW{2,1})+1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Calculate the global FL model after decoding %%%%%%%
if i==1
w(:,:,user)=deviationwnew(:,:,user);
lw(:,:,user)=deviationlwnew(:,:,user);
b(:,:,user)=deviationbnew(:,:,user);
ob(:,:,user)=deviationobnew(:,:,user);
else
w(:,:,user)=oldnetvaluable.IW{1,1}+deviationwnew(:,:,user);
lw(:,:,user)=oldnetvaluable.LW{2,1}+deviationlwnew(:,:,user);
b(:,:,user)=oldnetvaluable.b{1,1}+deviationbnew(:,:,user);
ob(:,:,user)=oldnetvaluable.b{2,1}+deviationobnew(:,:,user);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
eval([Winstr1,'=','netvaluable',';']);
end
%%%%%%%% Global FL model update %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wglobal=1/usernumber*sum(w,3); % global training model
lwglobal=1/usernumber*sum(lw,3); % global training model
bglobal=1/usernumber*sum(b,3);
obglobal=1/usernumber*sum(ob,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Calculate identification accuracy %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[nn,mm]=max(net1(testdata(1:1000,:)'));
oo=mm'-testgnd(1:1000,:);
error(i)=length(find(oo~=0))/1000;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
end