-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsers.py
More file actions
120 lines (105 loc) · 5.39 KB
/
Users.py
File metadata and controls
120 lines (105 loc) · 5.39 KB
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
import numpy as np
from util_functions import featureUniform, gaussianFeature, fileOverWriteWarning
import json
from random import choice, randint
class User():
def __init__(self, id, theta = None, CoTheta = None):
self.id = id
self.theta = theta
class UserManager():
def __init__(self, dimension, userNum, thetaFunc, gamma=None, UserGroups=1, argv = None):
self.dimension = dimension
self.thetaFunc = thetaFunc
self.userNum = userNum
self.gamma = gamma
self.UserGroups = UserGroups
self.argv = argv
self.signature = "A-"+"+PA"+"+TF-"+self.thetaFunc.__name__
def generateMasks(self):
mask = {}
for i in range(self.UserGroups):
mask[i] = np.random.randint(2, size = self.dimension)
return mask
# def simulateThetafromUsers_original(self):
# usersids = {}
# users = []
# mask = self.generateMasks()
# global_parameter_set = []
# if (self.UserGroups == 0):
# for key in range(self.userNum):
# thetaVector = self.thetaFunc(self.dimension, argv = self.argv)
# l2_norm = np.linalg.norm(thetaVector, ord =2)
# new_theta = thetaVector / l2_norm
# if global_parameter_set == []:
# global_parameter_set.append(new_theta)
# else:
# dist_to_all_existing_big = all([np.linalg.norm(new_theta - existing_theta) >= 0.7 for existing_theta in global_parameter_set])
# while (not dist_to_all_existing_big):
# thetaVector = self.thetaFunc(self.dimension, argv=self.argv)
# l2_norm = np.linalg.norm(thetaVector, ord=2)
# new_theta = thetaVector / l2_norm
# dist_to_all_existing_big = all(
# [np.linalg.norm(new_theta - existing_theta) >= 0.7 for existing_theta in
# global_parameter_set])
# global_parameter_set.append(new_theta)
# users.append(User(key, thetaVector / l2_norm))
# else:
# for i in range(self.UserGroups):
# usersids[i] = range(self.userNum*i/self.UserGroups, (self.userNum*(i+1))/self.UserGroups)
# for key in usersids[i]:
# thetaVector = np.multiply(self.thetaFunc(self.dimension, argv = self.argv), mask[i])
# l2_norm = np.linalg.norm(thetaVector, ord =2)
# users.append(User(key, thetaVector/l2_norm))
# return users
def simulateThetaForHomoUsers(self):
users = []
thetaVector = self.thetaFunc(self.dimension, argv=self.argv)
l2_norm = np.linalg.norm(thetaVector, ord=2)
thetaVector = thetaVector/l2_norm
for key in range(self.userNum):
users.append(User(key, thetaVector))
return users
def simulateThetaForHeteroUsers(self, global_dim):
local_dim = self.dimension-global_dim
users = []
thetaVector_g = self.thetaFunc(global_dim, argv=self.argv)
l2_norm = np.linalg.norm(thetaVector_g, ord=2)
thetaVector_g = thetaVector_g/l2_norm
for key in range(self.userNum):
thetaVector_l = self.thetaFunc(local_dim, argv=self.argv)
l2_norm = np.linalg.norm(thetaVector_l, ord=2)
thetaVector_l = thetaVector_l/l2_norm
thetaVector = np.concatenate([thetaVector_g, thetaVector_l])
users.append(User(key, thetaVector))
return users
def simulateThetaForClusteredUsers(self):
users = []
# Generate a global unique parameter set
global_parameter_set = []
for i in range(self.UserGroups):
thetaVector = self.thetaFunc(self.dimension, argv=self.argv)
l2_norm = np.linalg.norm(thetaVector, ord=2)
new_theta = thetaVector / l2_norm
if global_parameter_set == []:
global_parameter_set.append(new_theta)
else:
dist_to_all_existing_big = all([np.linalg.norm(new_theta - existing_theta) >= self.gamma for existing_theta in global_parameter_set])
while (not dist_to_all_existing_big):
thetaVector = self.thetaFunc(self.dimension, argv=self.argv)
l2_norm = np.linalg.norm(thetaVector, ord=2)
new_theta = thetaVector / l2_norm
dist_to_all_existing_big = all(
[np.linalg.norm(new_theta - existing_theta) >= self.gamma for existing_theta in
global_parameter_set])
global_parameter_set.append(new_theta)
global_parameter_set = np.array(global_parameter_set)
assert global_parameter_set.shape == (self.UserGroups, self.dimension)
# Uniformly sample a parameter for each user as initial parameter
parameter_index_for_users = np.random.randint(self.UserGroups, size=self.userNum)
print(parameter_index_for_users)
for key in range(self.userNum):
parameter_index = parameter_index_for_users[key]
users.append(User(key, global_parameter_set[parameter_index]))
assert users[key].id == key
assert np.linalg.norm(global_parameter_set[parameter_index] - users[key].theta) <= 0.001
return users, global_parameter_set, parameter_index_for_users