forked from roxanneluo/Federated-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
22 lines (19 loc) · 649 Bytes
/
Copy pathserver.py
File metadata and controls
22 lines (19 loc) · 649 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import threading
class Server:
def __init__(self):
self.weights = None # what to fill in here
self.num_samples = 0
self.lock = threading.Lock()
def update_weights(self, num_samples, weights):
with self.lock:
self.num_samples += num_samples
for sw, w in zip(self.weights, weights):
sw += num_samples * w
def get_weights(self):
with self.lock:
if self.num_samples == 0:
return self.weights
for w in self.weights:
w /= self.num_samples
self.num_samples = 0
return self.weights