-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_federated_averaging.py
56 lines (40 loc) · 1.68 KB
/
train_federated_averaging.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
from argparse import ArgumentParser
import tensorflow as tf
import tensorflow_federated as tff
from ocddetection.learning.federated.simulation import training
from ocddetection.learning.federated.impl import averaging
def __arg_parser() -> ArgumentParser:
parser = ArgumentParser()
# Data
parser.add_argument('path', type=str)
parser.add_argument('output', type=str)
# Hyperparameter
parser.add_argument('--rounds', type=int, default=50)
parser.add_argument('--clients-per-round', type=int, default=4)
parser.add_argument('--checkpoint-rate', type=int, default=5)
parser.add_argument('--learning-rate', type=float, default=.001)
parser.add_argument('--epochs', type=int, default=3)
parser.add_argument('--batch-size', type=int, default=128)
parser.add_argument('--window-size', type=int, default=150)
parser.add_argument('--pos-weights', type=float, nargs='+', default=[7, 5.25, 3, 0])
# Model
parser.add_argument('--hidden-size', type=int, default=64)
parser.add_argument('--dropout', type=float, default=.2)
return parser
def main() -> None:
args = __arg_parser().parse_args()
if len(args.pos_weights) == 1:
args.pos_weights = [args.pos_weights[0]] * 4
assert len(args.pos_weights) == 4, 'pos_weights contain a single value or a value for every client'
tff.backends.native.set_local_execution_context(
server_tf_device=tf.config.list_logical_devices('CPU')[0],
client_tf_devices=tf.config.list_logical_devices('GPU')
)
training.run(
'OCD Detection',
'FedAvg',
averaging.setup,
training.Config(**vars(args))
)
if __name__ == "__main__":
main()