-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.py
131 lines (114 loc) · 4.72 KB
/
main.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
import numpy as np
import argparse
import importlib
import torch
import os
from src.utils.worker_utils import read_data
from config import OPTIMIZERS, DATASETS, MODEL_PARAMS, TRAINERS
def read_options():
parser = argparse.ArgumentParser()
parser.add_argument('--algo',
help='name of trainer;',
type=str,
choices=OPTIMIZERS,
default='fedavg4')
parser.add_argument('--dataset',
help='name of dataset;',
type=str,
default='mnist_all_data_0_equal_niid')
parser.add_argument('--model',
help='name of model;',
type=str,
default='logistic')
parser.add_argument('--wd',
help='weight decay parameter;',
type=float,
default=0.001)
parser.add_argument('--gpu',
action='store_true',
default=False,
help='use gpu (default: False)')
parser.add_argument('--noprint',
action='store_true',
default=False,
help='whether to print inner result (default: False)')
parser.add_argument('--noaverage',
action='store_true',
default=False,
help='whether to only average local solutions (default: True)')
parser.add_argument('--device',
help='selected CUDA device',
default=0,
type=int)
parser.add_argument('--num_round',
help='number of rounds to simulate;',
type=int,
default=200)
parser.add_argument('--eval_every',
help='evaluate every ____ rounds;',
type=int,
default=5)
parser.add_argument('--clients_per_round',
help='number of clients trained per round;',
type=int,
default=10)
parser.add_argument('--batch_size',
help='batch size when clients train on data;',
type=int,
default=64)
parser.add_argument('--num_epoch',
help='number of epochs when clients train on data;',
type=int,
default=5)
parser.add_argument('--lr',
help='learning rate for inner solver;',
type=float,
default=0.1)
parser.add_argument('--seed',
help='seed for randomness;',
type=int,
default=0)
parser.add_argument('--dis',
help='add more information;',
type=str,
default='')
parsed = parser.parse_args()
options = parsed.__dict__
options['gpu'] = options['gpu'] and torch.cuda.is_available()
# Set seeds
np.random.seed(1 + options['seed'])
torch.manual_seed(12 + options['seed'])
if options['gpu']:
torch.cuda.manual_seed_all(123 + options['seed'])
# read data
idx = options['dataset'].find("_")
if idx != -1:
dataset_name, sub_data = options['dataset'][:idx], options['dataset'][idx+1:]
else:
dataset_name, sub_data = options['dataset'], None
assert dataset_name in DATASETS, "{} not in dataset {}!".format(dataset_name, DATASETS)
# Add model arguments
options.update(MODEL_PARAMS(dataset_name, options['model']))
# Load selected trainer
trainer_path = 'src.trainers.%s' % options['algo']
mod = importlib.import_module(trainer_path)
trainer_class = getattr(mod, TRAINERS[options['algo']])
# Print arguments and return
max_length = max([len(key) for key in options.keys()])
fmt_string = '\t%' + str(max_length) + 's : %s'
print('>>> Arguments:')
for keyPair in sorted(options.items()):
print(fmt_string % keyPair)
return options, trainer_class, dataset_name, sub_data
def main():
# Parse command line arguments
options, trainer_class, dataset_name, sub_data = read_options()
train_path = os.path.join('./data', dataset_name, 'data', 'train')
test_path = os.path.join('./data', dataset_name, 'data', 'test')
# `dataset` is a tuple like (cids, groups, train_data, test_data)
all_data_info = read_data(train_path, test_path, sub_data)
# Call appropriate trainer
trainer = trainer_class(options, all_data_info)
trainer.train()
if __name__ == '__main__':
main()