-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
39 lines (30 loc) · 1.13 KB
/
server.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
import flwr as fl
import sys
import numpy as np
#controller code
from controller import server_rounds
from controller import server_grpc_max_message_length
from controller import server_address
#########################################################################
class SaveModelStrategy(fl.server.strategy.FedAvg):
def aggregate_fit(
self,
rnd,
results,
failures
):
aggregated_weights = super().aggregate_fit(rnd, results, failures)
if aggregated_weights is not None:
# Save aggregated_weights
print(f"Saving round {rnd} aggregated_weights...")
np.savez(f"round-{rnd}-weights.npz", *aggregated_weights)
return aggregated_weights
# Create strategy and run server
strategy = SaveModelStrategy()
# Start Flower server for three rounds of federated learning
fl.server.start_server(
server_address =server_address ,
config=fl.server.ServerConfig(num_rounds=server_rounds) ,
grpc_max_message_length = server_grpc_max_message_length *server_grpc_max_message_length*server_grpc_max_message_length ,
strategy = strategy
)