-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
80 lines (66 loc) · 2.28 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
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
import argparse
from typing import List, Tuple
import flwr as fl
from flwr.common import Metrics
parser = argparse.ArgumentParser(description="Flower Embedded devices")
parser.add_argument(
"--server_address",
type=str,
default="0.0.0.0:8080",
help=f"gRPC server address (deafault '0.0.0.0:8080')",
)
parser.add_argument(
"--rounds",
type=int,
default=5,
help="Number of rounds of federated learning (default: 5)",
)
parser.add_argument(
"--sample_fraction",
type=float,
default=1.0,
help="Fraction of available clients used for fit/evaluate (default: 1.0)",
)
parser.add_argument(
"--min_num_clients",
type=int,
default=2,
help="Minimum number of available clients required for sampling (default: 2)",
)
# Define metric aggregation function
def weighted_average(metrics: List[Tuple[int, Metrics]]) -> Metrics:
"""This function averages teh `accuracy` metric sent by the clients in a `evaluate`
stage (i.e. clients received the global model and evaluate it on their local
validation sets)."""
# Multiply accuracy of each client by number of examples used
accuracies = [num_examples * m["accuracy"] for num_examples, m in metrics]
examples = [num_examples for num_examples, _ in metrics]
# Aggregate and return custom metric (weighted average)
return {"accuracy": sum(accuracies) / sum(examples)}
def fit_config(server_round: int):
"""Return a configuration with static batch size and (local) epochs."""
config = {
"epochs": 8, # Number of local epochs done by clients
"batch_size": 32, # Batch size to use by clients during fit()
}
return config
def main():
args = parser.parse_args()
print(args)
# Define strategy
strategy = fl.server.strategy.FedAvg(
fraction_fit=args.sample_fraction,
fraction_evaluate=args.sample_fraction,
min_fit_clients=args.min_num_clients,
min_available_clients=args.min_num_clients,
on_fit_config_fn=fit_config,
evaluate_metrics_aggregation_fn=weighted_average,
)
# Start Flower server
fl.server.start_server(
server_address=args.server_address,
config=fl.server.ServerConfig(num_rounds=args.rounds),
strategy=strategy,
)
if __name__ == "__main__":
main()