-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
363 lines (301 loc) · 12.3 KB
/
main.py
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
"""
This is the main script, which us functions from modules.
Theta (global param) is a list of dictionnaries.
"""
import random
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestNeighbors
from modules import *
from config import PATH_TO_DATA, MU, MAX_STEPS, NUMBER_OF_RUNS #import the configuration variables from config.py
def train(data, W, agents_data_idx, privacy, mu, locL, max_steps, eps, logErrors=False, verbose=False):
"""
data : list of 100'000 datapoints [x, y] from MovieLens 100K Dataset (https://grouplens.org/datasets/movielens/100k/)
W : list of n lists with n float each, nonnegative weight matrix for n agents;
agents_data_idx : list of n lists with n float each, list of neighbors for each agent;
privacy : boolean, is True for the private case, False else;
mu : float, trade-off parameter between having similar models for strongly connected agents
and models that are accurate on their respective local datasets;
locL : list of n float, Lipschitz constants L for localLossGrad, L_i^{loc} for Lipschitz continuous gradien localLossGrad for each agent;
max_steps : maximum number of training steps;
logErrors : for loss vizualisation
verbose : print infos on what the code is doing
random initialization
for each step in range nb_steps :
for each agent:
if agent wakes up
update local theta_i (4)
broadcast step
calculate time before next wake up (random.poisson(lam=1.0, size=None))
eps : list of ints (privacy level)
"""
n = len(W) #W is a list of lists
d = len(data[0][0])
model = []
clocks = [] #n times (int) wher the agent will wake up
neighbors = [] #list of the indexs (int) of the neighbors for each agent
C = [] #list of n float, confidence coeff for each agent
alpha = [] #list of n float, alpha for each agent
D = [] #list of n float, D for each agent
lambd = [] #list of n float, lambd for each agent
#random init of the model
for i in range(0, n):
submodel = []
for j in range(0, n):
theta = []
for k in range (0,d):
theta.append(2*random.random() - 1) #TODO change init
submodel.append(theta)
model.append(submodel)
#set the first wakeup times
for i in range(0, n):
clocks.append(np.random.poisson(lam=1.0, size=None))
#calculate neighbors
for agent in range(0, n):
neighbors.append(getNeighbors(W, agent))
#calculate confidence coeff
for agent in range(0, n):
C.append(len(agents_data_idx[agent]))
m_max = max(C)
for agent in range(0, n):
C[agent] = C[agent]/m_max
#calculate alphas
for agent in range(0, n):
alpha.append(1 / (1 + mu * C[agent] * locL[agent]))
#calculate D
for agent in range(0, n):
D.append(sum(W[agent]))
#calculate lmbda
for agent in range(0, n):
lambd.append(1 / len(agents_data_idx[agent]))
#log the RMSEs
RMSEsLog = []
if privacy:
if verbose:
print('computing with privacy...')
for step in range(0, max_steps):
if verbose:
print(step)
for agent in range (0, n):
if step >= clocks[agent] : #agent wakes up
model = updateStep_private(data, model, W, agent, agents_data_idx, C, mu, alpha, lambd, locL, eps)
model = broadcastStep(model, neighbors, agent)
clocks[agent] = step + np.random.poisson(lam=1.0, size=None)
if logErrors:
RMSEsLog = logRMSE(data, agents_data_idx, model, RMSEsLog)
else:
if verbose:
print('computing without privacy...')
#run of the algo for each step
for step in range(0, max_steps):
if verbose:
print(step)
for agent in range (0, n):
if step >= clocks[agent] : #agent wakes up
model = updateStep(data, model, W, agent, agents_data_idx, C, mu, alpha, lambd)
model = broadcastStep(model, neighbors, agent)
clocks[agent] = step + np.random.poisson(lam=1.0, size=None)
if logErrors:
RMSEsLog = logRMSE(data, agents_data_idx, model, RMSEsLog)
return model, RMSEsLog
def evaluate(data, model, agents_data_idx): #makes predictions using a model on the data provided
n = len(agents_data_idx)
user_RMSEs = []
for user in range(0,n):
user_RMSE = []
for data_idx in agents_data_idx[user]:
user_RMSE.append(loss(model[user][user], data[data_idx][0], data[data_idx][1]))
user_RMSEs.append(sum(user_RMSE)/len(user_RMSE))
return user_RMSEs
#backtracking of RMSE
def logRMSE(data, agents_data_idx, model, RMSEs):
for user in range(0,n):
user_RMSE = []
for data_idx in agents_data_idx[user]:
user_RMSE.append(loss(model[user][user], data[data_idx][0], data[data_idx][1]))
RMSEs.append(sum(user_RMSE)/len(user_RMSE))
return RMSEs
### RUN ###
#import from config file (more info there)
path = PATH_TO_DATA
mu = MU
max_steps = MAX_STEPS
number_of_runs = NUMBER_OF_RUNS
#load dataset
train_data, train_agents_data_idx, test_data, test_agents_data_idx = load_ml100k(path)
print("Dataset size is: ", len(train_data + test_data))
n = len(train_agents_data_idx)
locL = []
for i in range(0, n):
locL.append(1)
X_mean = []
for i in range(0, n):
xy = []
for data_ind in train_agents_data_idx[i]:
xy.append(train_data[data_ind][0] + [train_data[data_ind][1]])
X_mean.append(np.mean(xy, axis=0))
#generate privacy epsilons
eps = [1.0]*n
#nbrs = NearestNeighbors(n_neighbors=10, algorithm='auto', metric=smp.cosine_similarity).fit(test)
nbrs = NearestNeighbors(n_neighbors=10, algorithm='auto', metric='cosine').fit(X_mean)
_, ratingsNeighbors = nbrs.kneighbors(X_mean)
#W initialisation (as described in the paper)
W = []
for i in range(0, n):
neighborsVec = np.zeros(n)
for j in range(len(neighborsVec)):
if j in ratingsNeighbors[i]:
neighborsVec[j] = 1.0
W.append(neighborsVec)
### Compute scores ###
public_RMSEs = []
private_RMSEs = []
#Number of steps in time
max_steps = 50
#mu, 1000 by default
mu=1000
"""
#Purely local models; W = np.identity(n)
RMSEs = []
RMSEsLog = []
for i in range(0,5):
if i==0:
model, RMSEsLog = train(train_data, np.identity(n), train_agents_data_idx, False, mu, locL, max_steps, eps, logErrors=True)
else:
model, RMSEsLogTemp = train(train_data, np.identity(n), train_agents_data_idx, False, mu, locL, max_steps, eps, logErrors=True)
RMSEsLog = [sum(x) for x in zip(RMSEsLog, RMSEsLogTemp)]
print('trained a model for {} steps'.format(max_steps))
user_RMSEs = evaluate(test_data, model, test_agents_data_idx)
print('Without privacy :', sum(user_RMSEs)/len(user_RMSEs))
RMSEs.append(sum(user_RMSEs)/len(user_RMSEs))
if i==4:
RMSEsLog[:] = [x / 5 for x in RMSEsLog]
print(RMSEsLog)
plt.plot([j + 1 for j in range(len(RMSEsLog))], RMSEsLog)
plt.xlabel("steps")
plt.ylabel("RMSE")
plt.title("Purely local : RMSEs for each step, max_steps = {}".format(max_steps))
plt.savefig("Purely local.jpg")
plt.show()
print('')
print('Purely local models RMSE : {}'.format(sum(RMSEs)/len(RMSEs)))
print('')
print('######################')
print('')
#Non-priv. CD
RMSEs = []
RMSEsLog = []
for i in range(0,number_of_runs):
if i==0:
model, RMSEsLog = train(train_data, W, train_agents_data_idx, False, mu, locL, max_steps, eps, logErrors=True)
else:
model, RMSEsLogTemp = train(train_data, W, train_agents_data_idx, False, mu, locL, max_steps, eps, logErrors=True)
RMSEsLog = [sum(x) for x in zip(RMSEsLog, RMSEsLogTemp)]
print('trained a model for {} steps'.format(max_steps))
user_RMSEs = evaluate(test_data, model, test_agents_data_idx)
print('Without privacy :', sum(user_RMSEs)/len(user_RMSEs))
RMSEs.append(sum(user_RMSEs)/len(user_RMSEs))
if i==4:
RMSEsLog[:] = [x / 5 for x in RMSEsLog]
print(RMSEsLog)
plt.plot([j + 1 for j in range(len(RMSEsLog))], RMSEsLog)
plt.xlabel("steps")
plt.ylabel("RMSE")
plt.title("Non-private : RMSEs for each step, max_steps = {}".format(max_steps))
plt.savefig("Non-private.jpg")
plt.show()
print('')
print('Non-priv. CD RMSE : {}'.format(sum(RMSEs)/len(RMSEs)))
print('')
print('######################')
print('')
#Private, eps = 1.0
RMSEs = []
RMSEsLog = []
eps = [1.0]*n
for i in range(0,number_of_runs):
if i==0:
model, RMSEsLog = train(train_data, W, train_agents_data_idx, True, mu, locL, max_steps, eps, logErrors=True)
else:
model, RMSEsLogTemp = train(train_data, W, train_agents_data_idx, True, mu, locL, max_steps, eps, logErrors=True)
RMSEsLog = [sum(x) for x in zip(RMSEsLog, RMSEsLogTemp)]
print('trained a model for {} steps'.format(max_steps))
user_RMSEs = evaluate(test_data, model, test_agents_data_idx)
print('With privacy :', sum(user_RMSEs)/len(user_RMSEs))
RMSEs.append(sum(user_RMSEs)/len(user_RMSEs))
if i==4:
RMSEsLog[:] = [x / 5 for x in RMSEsLog]
print(RMSEsLog)
plt.plot([j + 1 for j in range(len(RMSEsLog))], RMSEsLog)
plt.xlabel("steps")
plt.ylabel("RMSE")
plt.title("Private, eps = 1.0 : RMSEs for each step, max_steps = {}".format(max_steps))
plt.savefig("Private, eps = 1.jpg")
plt.show()
print('')
print('Private RMSE with eps={} : {}'.format(eps[0],sum(RMSEs)/len(RMSEs)))
print('######################')
print('')
print('######################')
print('')
#Private, eps = 0.5
RMSEs = []
RMSEsLog = []
eps = [0.5]*n
for i in range(0,number_of_runs):
if i==0:
model, RMSEsLog = train(train_data, W, train_agents_data_idx, True, mu, locL, max_steps, eps, logErrors=True)
else:
model, RMSEsLogTemp = train(train_data, W, train_agents_data_idx, True, mu, locL, max_steps, eps, logErrors=True)
RMSEsLog = [sum(x) for x in zip(RMSEsLog, RMSEsLogTemp)]
print('trained a model for {} steps'.format(max_steps))
user_RMSEs = evaluate(test_data, model, test_agents_data_idx)
print('With privacy :', sum(user_RMSEs)/len(user_RMSEs))
RMSEs.append(sum(user_RMSEs)/len(user_RMSEs))
if i==4:
RMSEsLog[:] = [x / 5 for x in RMSEsLog]
print(RMSEsLog)
plt.plot([j + 1 for j in range(len(RMSEsLog))], RMSEsLog)
plt.xlabel("steps")
plt.ylabel("RMSE")
plt.title("Private, eps = 0.5 : RMSEs for each step, max_steps = {}".format(max_steps))
plt.savefig("Private, eps = 0.5.jpg")
plt.show()
print('')
print('Private RMSE with eps={} : {}'.format(eps[0],sum(RMSEs)/len(RMSEs)))
print('######################')
"""
#Private, eps = 0.1
RMSEs = []
RMSEsLog = []
eps = [0.1]*n
for i in range(0,number_of_runs):
if i==0:
model, RMSEsLog = train(train_data, W, train_agents_data_idx, True, mu, locL, max_steps, eps, logErrors=True)
else:
model, RMSEsLogTemp = train(train_data, W, train_agents_data_idx, True, mu, locL, max_steps, eps, logErrors=True)
RMSEsLog = [sum(x) for x in zip(RMSEsLog, RMSEsLogTemp)]
print('trained a model for {} steps'.format(max_steps))
user_RMSEs = evaluate(test_data, model, test_agents_data_idx)
if i==0:
plt.clf()
plt.plot([j + 1 for j in range(len(RMSEsLog))], RMSEsLog)
plt.xlabel("steps")
plt.ylabel("RMSE")
plt.title("Private, eps = {} : RMSEs for each step, max_steps = {}".format(eps[0], max_steps))
plt.savefig("logRMSEs_private_eps{}.jpg".format(eps[0]))
print('Saved RMSEs graph')
print('With privacy : {:.2f} \n'.format(sum(user_RMSEs)/len(user_RMSEs)))
RMSEs.append(sum(user_RMSEs)/len(user_RMSEs))
if i==4:
RMSEsLog[:] = [x / 5 for x in RMSEsLog]
print(RMSEsLog)
plt.plot([j + 1 for j in range(len(RMSEsLog))], RMSEsLog)
plt.xlabel("steps")
plt.ylabel("RMSE")
plt.title("Private, eps = 0.1 : RMSEs for each step, max_steps = {}".format(max_steps))
plt.savefig("Private, eps = 0.1.jpg")
plt.show()
print('')
print('Private RMSE with eps={} : {}'.format(eps[0],sum(RMSEs)/len(RMSEs)))
print('######################')