-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.py
More file actions
159 lines (140 loc) · 5.95 KB
/
Copy pathserver.py
File metadata and controls
159 lines (140 loc) · 5.95 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import time
import random
import uuid
import json
import logging
import pathlib
from math import ceil
import numpy as np
import tensorflow as tf
import ray
from models.perceptron import Perceptron
from models.cnn import CNN
from models.lstm import LSTM
logging.basicConfig(level=logging.DEBUG,
format='[Server] %(asctime)s %(levelname)s %(message)s')
class Server:
def __init__(self, clients, X_test, y_test, config):
self.clients = clients
self.X_test = X_test
self.y_test = y_test
self.config = config
self.val_history = {
"duration" : [],
"config": config,
"learning_rate": []
}
self.save_path = self.config['save_dir'] + "/" + str(uuid.uuid1())
def setup_model(self, model_type):
self.model_type = model_type
if model_type == "perceptron":
self.model = Perceptron()
elif model_type == "cnn":
self.model = CNN()
elif model_type == "lstm":
self.model = LSTM()
else:
raise ValueError("Model {0} not supported.".format(model_type))
def get_initial_weights(self, model_type):
tf.reset_default_graph()
if model_type == "perceptron":
m = Perceptron()
inputs = tf.placeholder(tf.float32, shape=(None, 28*28))
_ = m.get_model(features={"x": inputs}, labels=None, mode='predict', params=None)
else:
raise ValueError("Model {model_type} not supported.".format(model_type))
with tf.Session().as_default() as sess:
sess.run(tf.global_variables_initializer())
collection = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
weights = {tensor.name:sess.run(tensor) for tensor in collection}
tf.reset_default_graph()
return weights
def federated_learning(self, fraction, max_rounds, model_type):
self.setup_model(model_type)
weights = self.get_initial_weights(model_type)
num_clients = max( ceil(fraction * len(self.clients)), 1 )
best_accuracy = 0.0
goal_accuracy = self.config["goal_accuracy"]
@ray.remote
def train_model(client, weights, config):
return client.train(weights, config)
ray.init(num_cpus=num_clients)
for t in range(1, max_rounds + 1):
if best_accuracy > goal_accuracy:
logging.info("Reached goal accuracy of {0} at round {1}."\
.format(goal_accuracy, t))
break
start_time = time.time()
logging.info('Round number {0}.'.format(t+1))
random_clients = random.sample(self.clients, num_clients)
threads = ray.get([train_model.remote(c, weights, self.config) for c in random_clients])
weights, n = threads[0]
if num_clients > 1:
for result in threads[1:]:
update, num_data = result
update = self.model.scale_weights(update, num_data)
weights = self.model.sum_weights(weights, update)
n += num_data
weights = self.model.inverse_scale_weights(weights, n)
eval_results = self.validate_model(t + 1, weights)
best_accuracy = max(best_accuracy, eval_results["accuracy"])
# Update validation history
for key, value in eval_results.items():
if key not in self.val_history:
self.val_history[key] = []
self.val_history[key].append(float(value))
elapsed_time = time.time() - start_time
self.val_history["learning_rate"].append(self.do_learning_rate_decay())
self.val_history["duration"].append(elapsed_time)
# Save validation history
with open(self.save_path, 'w') as f:
f.write(json.dumps(self.val_history))
logging.info("Final validation accuracy: {0}.".format(best_accuracy))
logging.info("Saved results at {0}.".format(self.save_path))
logging.info("----- Federated Learning Completed -----")
def validate_model(self, t, weights):
# check if this is needed
self.setup_model(self.model_type)
classifier = tf.estimator.Estimator(
model_fn=self.model.get_model,
model_dir=self.get_checkpoints_folder(),
params = {'new_weights': weights, 'learning_rate': 0.0}
)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": self.X_test},
y=self.y_test,
batch_size=1,
num_epochs=None,
shuffle=False
)
classifier.train(
input_fn=train_input_fn,
steps=1
)
metagraph_file = self.get_checkpoints_folder() + '.meta'
self.model.load_weights(weights, self.get_latest_checkpoint(),
self.get_checkpoints_folder())
logging.info('Main model updated.')
self.setup_model(self.model_type)
classifier = tf.estimator.Estimator(
model_fn=self.model.get_model,
model_dir=self.get_checkpoints_folder(),
params = {'new_weights': weights}
)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": self.X_test},
y=self.y_test,
num_epochs=1,
shuffle=False
)
eval_results = classifier.evaluate(input_fn=eval_input_fn)
logging.info("[Round {0}] Validation results: {1}".format(t, eval_results))
return eval_results
def do_learning_rate_decay(self):
self.config["learning_rate"] *= self.config["lr_decay"]
logging.info("Learning rate after decay: {0}.".format(self.config["learning_rate"]))
return self.config["learning_rate"]
def get_checkpoints_folder(self):
return "./checkpoints/" + self.model_type + '/'
def get_latest_checkpoint(self):
return tf.train.latest_checkpoint(self.get_checkpoints_folder())