-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
307 lines (253 loc) · 11 KB
/
client.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
import torch
import random
import os
import pickle
from operator import itemgetter
from torchvision import datasets, transforms
import numpy as np
import json
import math
from tqdm import tqdm
import multiprocessing as mp
from datasets import load_bank, load_kdd99, load_nslkdd
import torch.utils.data as Data
import time
def extr_noniid_dirt(dataset, n_clients, n_classes, alpha=0.5):
data_size = len(dataset)
idxs_client_dict = {i: [] for i in range(n_clients)}
idxs = np.arange(data_size)
labels = np.array(dataset.targets)
# sort labels
idxs_labels = np.vstack((idxs, labels))
idxs_labels = idxs_labels[:, idxs_labels[1, :].argsort()]
idxs = idxs_labels[0, :].astype(int)
labels = idxs_labels[1, :]
data_size_each_class = np.array([np.sum(labels == c) for c in range(n_classes)]).reshape(n_classes, 1)
# divide and assign
idxs_classes = []
for j in range(n_classes):
idxs_classj = idxs[data_size_each_class[:j].sum() : data_size_each_class[:j+1].sum()].tolist()
idxs_classes.append(idxs_classj)
max_class = np.argmax(data_size_each_class)
# for i in range(n_clients):
# rand_set = np.random.choice(idxs_classes[max_class], 10, replace=False).tolist()
# idxs_classes[max_class] = list(set(idxs_classes[max_class]) - set(rand_set))
# idxs_client_dict[i] = idxs_client_dict[i] + rand_set
# data_size_each_class[max_class] -= 10 * n_clients
distribution = np.random.dirichlet(np.repeat(alpha, n_clients), size=n_classes).astype(np.float64)
data_size_each_class[max_class] -= 10 * n_clients
data_size_each_class_client = (distribution * data_size_each_class).astype(int)
data_size_each_class_client[max_class] += 10
for i in range(n_clients):
for j in range(n_classes):
if i == n_clients - 1:
rand_set = idxs_classes[j]
else:
rand_set = np.random.choice(idxs_classes[j], data_size_each_class_client[j, i], replace=False).tolist()
idxs_classes[j] = list(set(idxs_classes[j]) - set(rand_set))
idxs_client_dict[i] = idxs_client_dict[i] + rand_set
return idxs_client_dict
class Client:
def __init__(self, idx, dataset_name="", data_indices=None, data_size=None, val_func_type=None, privacy_budget=None, factor=None):
self.idx = idx
self.dataset_name = dataset_name
self.data_indices = data_indices
self.data_size = data_size
self.val_func_type = val_func_type
self.privacy_budget = privacy_budget
self.factor = factor
def __int__(self, **attrs):
self.__dict__.update(attrs)
def data(self, dataset):
subset = torch.utils.data.Subset(dataset, self.data_indices)
data_loader = Data.DataLoader(subset, batch_size=len(subset))
for data in data_loader:
return data
def return_bid(self, n_items):
if self.val_func_type == "grad":
def v(x):
return x ** 2
type = 0
elif self.val_func_type == "sqrt":
def v(x):
return 2 * x ** 0.5
type = 1
elif self.val_func_type == "linear":
def v(x):
return 2 * x
type = 2
else:
def v(x):
return math.exp(x) - 1
type = 3
bid = []
for k in range(n_items):
item = (k + 1) * self.privacy_budget / n_items
bid.append(self.factor * v(item))
bid.append(self.privacy_budget)
bid.append(self.data_size)
bid.append(type)
bid.append(self.factor)
return bid
def return_dict(self):
client_dict = {}
client_dict.update(self.__dict__)
return client_dict
class Clients:
def __init__(self):
self.data = []
self.n_runs = 0
self.dirs = "data/"
self.filename = ""
self.min_n_samples = 10
self.min_pbudget = 1.0
self.max_pbudget = 5.0
self.min_factor = 0.5
self.max_factor = 1.5
def generate_clients(self, dataset_name, n_profiles, n_clients_per_profile, iid=True, alpha=0.5):
np.random.seed((os.getpid() * int(time.time())) % 123456789)
random.seed((os.getpid() * int(time.time())) % 123456789)
if dataset_name == "Bank":
train_data, _ = load_bank()
n_classes = 2
elif dataset_name == "KDD99":
train_data, _ = load_kdd99()
n_classes = 5
elif dataset_name == "NSL-KDD":
train_data, _ = load_nslkdd()
n_classes = 5
else:
raise ValueError(f"Dataset {dataset_name} is not defined")
n_clients = n_profiles * n_clients_per_profile
data_size = len(train_data)
if iid:
idxs = np.arange(data_size).tolist()
dist = np.random.lognormal(0, 2.0, n_clients).astype(np.float64)
n_samples_dist = ((data_size - self.min_n_samples * n_clients) * dist / dist.sum()).astype(int)
n_samples_dist += self.min_n_samples
clients_dict = {}
for i in range(n_clients):
if i == n_clients - 1:
samples = idxs
else:
samples = np.random.choice(idxs, n_samples_dist[i], replace=False).tolist()
idxs = list(set(idxs) - set(samples))
val_func_type = random.choice(["grad", "sqrt", "expo", "linear"])
privacy_budget = random.uniform(self.min_pbudget, self.max_pbudget)
factor = random.uniform(self.min_factor, self.max_factor)
client = Client(i, dataset_name, samples, len(samples), val_func_type, privacy_budget, factor)
client_dict = client.return_dict()
clients_dict[client.idx] = client_dict
else:
indices_ls = extr_noniid_dirt(train_data, n_clients, n_classes, alpha)
clients_dict = {}
for i in range(n_clients):
val_func_type = random.choice(["grad", "sqrt", "expo", "linear"])
privacy_budget = random.uniform(self.min_pbudget, self.max_pbudget)
factor = random.uniform(self.min_factor, self.max_factor)
client = Client(i, dataset_name, indices_ls[i], len(indices_ls[i]), val_func_type, privacy_budget,
factor)
client_dict = client.return_dict()
clients_dict[client.idx] = client_dict
return clients_dict
def generate_clients_mulruns(self, dataset_name, n_profiles, n_clients_per_profile, n_runs, iid=True, alpha=0.5, overlap=False):
self.data = []
n_processes = 10
run_processes = n_processes
clients_dict = {}
times = int(math.ceil(n_runs / n_processes))
for j in tqdm(range(times)):
if j == times - 1 and n_runs % n_processes != 0:
run_processes = n_runs % n_processes
print(run_processes)
pool = mp.Pool(run_processes)
workers = []
for i in range(run_processes):
worker = pool.apply_async(self.generate_clients, args=(dataset_name, n_profiles, n_clients_per_profile, iid, alpha))
workers.append(worker)
pool.close()
pool.join()
for i in range(run_processes):
sub_clients_dict = workers[i].get()
clients_dict["run %s" % (j * n_processes + i + 1)] = sub_clients_dict
self.save_json(clients_dict, overlap=overlap)
self.n_runs = n_runs
def save_json(self, clients_dict, overlap=False):
if not os.path.exists(self.dirs):
os.makedirs(self.dirs)
if not overlap and os.path.exists(self.dirs+self.filename):
print("Clients data exists")
return
with open(self.dirs+self.filename, 'w', encoding='utf-8') as f:
content = json.dumps(clients_dict, indent=2)
f.write(content)
def load_json(self):
with open(self.dirs+self.filename, 'r', encoding='utf8') as f:
clients_dict = json.load(f)
self.n_runs = len(clients_dict)
self.data = []
for i in range(self.n_runs):
sub_clients_dict = clients_dict["run %s" %(i + 1)]
sub_clients = []
for client_dict in sub_clients_dict.values():
client = Client(**client_dict)
sub_clients.append(client)
self.data.append(sub_clients)
def return_bids(self, n_items):
clients_ls = []
for i in range(self.n_runs):
sub_clients_ls = []
for client in self.data[i]:
sub_clients_ls.append(client.return_bid(n_items))
clients_ls.append(sub_clients_ls)
return np.array(clients_ls)
def return_bids_run(self, n_items, run):
clients_ls = []
for client in self.data[run]:
clients_ls.append(client.return_bid(n_items))
return np.array(clients_ls)
def return_local_sets_run(self, dataset, n_agents, run):
local_sets = []
clients = self.data[run]
rnds = len(clients) // n_agents
for rnd in range(rnds):
local_sets_rnd = []
for i in range(n_agents):
client = clients[rnd * n_agents + i]
local_sets_rnd.append(client.data(dataset))
local_sets.append(local_sets_rnd)
return local_sets
def return_clients_by_run(self, run):
clients = Clients()
clients.data = [self.data[run]]
clients.n_runs = 1
clients.dirs = self.dirs
clients.min_pbudget = self.min_pbudget
clients.max_pbudget = self.max_pbudget
clients.min_factor = self.min_factor
clients.max_factor = self.max_factor
clients.min_n_samples = self.min_n_samples
return clients
if __name__ == '__main__':
clients = Clients()
clients.dirs = "data/nslkdd/iid/"
clients.min_pbudget = 0.5
clients.max_pbudget = 2.0
# clients.filename = "train_profiles_2.json"
# clients.generate_clients_mulruns("NSL-KDD", 100, 10, 1024, iid=True, overlap=True)
clients.filename = "test_profiles_2mp.json"
clients.generate_clients_mulruns("NSL-KDD", 100, 10, 1000, iid=True, overlap=True)
# for run in range(10, 100):
# clients.filename = f"test_profiles_100r_{run}.json"
# clients.generate_clients_mulruns("NSL-KDD", 100, 10, 100, iid=False, overlap=True)
clients = Clients()
clients.dirs = "data/bank/iid/"
clients.min_pbudget = 0.5
clients.max_pbudget = 2.0
# clients.filename = "train_profiles_2.json"
# clients.generate_clients_mulruns("NSL-KDD", 100, 10, 1024, iid=True, overlap=True)
clients.filename = "test_profiles_2mp.json"
clients.generate_clients_mulruns("Bank", 100, 10, 1000, iid=True, overlap=True)
# for run in range(10, 100):
# clients.filename = f"test_profiles_100r_{run}.json"
# clients.generate_clients_mulruns("NSL-KDD", 100, 10, 100, iid=False, overlap=True)