-
Notifications
You must be signed in to change notification settings - Fork 3
/
fedr.py
282 lines (217 loc) · 11 KB
/
fedr.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from dataloader import *
import pickle
import os
import copy
import logging
from kge_model import KGEModel
from torch import optim
import torch.nn.functional as F
class Server(object):
def __init__(self, args, nrelation):
self.args = args
embedding_range = torch.Tensor([(args.gamma + args.epsilon) / args.hidden_dim])
if args.model in ['ComplEx']:
self.rel_embed = torch.zeros(nrelation, args.hidden_dim*2).to(args.gpu).requires_grad_()
else:
self.rel_embed = torch.zeros(nrelation, args.hidden_dim).to(args.gpu).requires_grad_()
nn.init.uniform_(
tensor=self.rel_embed,
a=-embedding_range.item(),
b=embedding_range.item()
)
self.nrelation = nrelation
def send_emb(self):
return copy.deepcopy(self.rel_embed)
def aggregation(self, clients, rel_update_weights):
agg_rel_mask = rel_update_weights
agg_rel_mask[rel_update_weights != 0] = 1
rel_w_sum = torch.sum(agg_rel_mask, dim=0)
rel_w = agg_rel_mask / rel_w_sum
rel_w[torch.isnan(rel_w)] = 0
if self.args.model in ['ComplEx']:
update_rel_embed = torch.zeros(self.nrelation, self.args.hidden_dim * 2).to(self.args.gpu)
else:
update_rel_embed = torch.zeros(self.nrelation, self.args.hidden_dim).to(self.args.gpu)
for i, client in enumerate(clients):
local_rel_embed = client.rel_embed.clone().detach()
update_rel_embed += local_rel_embed * rel_w[i].reshape(-1, 1)
self.rel_embed = update_rel_embed.requires_grad_()
class Client(object):
def __init__(self, args, client_id, data, train_dataloader,
valid_dataloader, test_dataloader, ent_embed):
self.args = args
self.data = data
self.train_dataloader = train_dataloader
self.valid_dataloader = valid_dataloader
self.test_dataloader = test_dataloader
self.ent_embed = ent_embed
self.client_id = client_id
self.score_local = []
self.score_global = []
self.kge_model = KGEModel(args, args.model)
self.rel_embed = None
def __len__(self):
return len(self.train_dataloader.dataset)
def client_update(self):
optimizer = optim.Adam([{'params': self.rel_embed},
{'params': self.ent_embed}], lr=self.args.lr)
losses = []
for i in range(self.args.local_epoch):
for batch in self.train_dataloader:
positive_sample, negative_sample, sample_idx = batch
positive_sample = positive_sample.to(self.args.gpu)
negative_sample = negative_sample.to(self.args.gpu)
negative_score = self.kge_model((positive_sample, negative_sample),
self.rel_embed, self.ent_embed)
negative_score = (F.softmax(negative_score * self.args.adversarial_temperature, dim=1).detach()
* F.logsigmoid(-negative_score)).sum(dim=1)
positive_score = self.kge_model(positive_sample,
self.rel_embed, self.ent_embed, neg=False)
positive_score = F.logsigmoid(positive_score).squeeze(dim=1)
positive_sample_loss = - positive_score.mean()
negative_sample_loss = - negative_score.mean()
loss = (positive_sample_loss + negative_sample_loss) / 2
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append(loss.item())
return np.mean(losses)
def client_eval(self, istest=False):
if istest:
dataloader = self.test_dataloader
else:
dataloader = self.valid_dataloader
results = ddict(float)
for batch in dataloader:
triplets, labels = batch
triplets, labels = triplets.to(self.args.gpu), labels.to(self.args.gpu)
head_idx, rel_idx, tail_idx = triplets[:, 0], triplets[:, 1], triplets[:, 2]
pred = self.kge_model((triplets, None),
self.rel_embed, self.ent_embed)
b_range = torch.arange(pred.size()[0], device=self.args.gpu)
target_pred = pred[b_range, tail_idx]
pred = torch.where(labels.byte(), -torch.ones_like(pred) * 10000000, pred)
pred[b_range, tail_idx] = target_pred
ranks = 1 + torch.argsort(torch.argsort(pred, dim=1, descending=True),
dim=1, descending=False)[b_range, tail_idx]
ranks = ranks.float()
count = torch.numel(ranks)
results['count'] += count
results['mr'] += torch.sum(ranks).item()
results['mrr'] += torch.sum(1.0 / ranks).item()
for k in [1, 3, 10]:
results['hits@{}'.format(k)] += torch.numel(ranks[ranks <= k])
for k, v in results.items():
if k != 'count':
results[k] /= results['count']
return results
class FedR(object):
def __init__(self, args, all_data):
self.args = args
train_dataloader_list, valid_dataloader_list, test_dataloader_list, \
self.rel_freq_mat, ent_embed_list, nrelation = get_all_clients(all_data, args)
self.args.nrelation = nrelation # question
# client
self.num_clients = len(train_dataloader_list)
self.clients = [
Client(args, i, all_data[i], train_dataloader_list[i], valid_dataloader_list[i],
test_dataloader_list[i], ent_embed_list[i]) for i in range(self.num_clients)
]
self.server = Server(args, nrelation)
self.total_test_data_size = sum([len(client.test_dataloader.dataset) for client in self.clients])
self.test_eval_weights = [len(client.test_dataloader.dataset) / self.total_test_data_size for client in self.clients]
self.total_valid_data_size = sum([len(client.valid_dataloader.dataset) for client in self.clients])
self.valid_eval_weights = [len(client.valid_dataloader.dataset) / self.total_valid_data_size for client in self.clients]
def write_training_loss(self, loss, e):
self.args.writer.add_scalar("training/loss", loss, e)
def write_evaluation_result(self, results, e):
self.args.writer.add_scalar("evaluation/mrr", results['mrr'], e)
self.args.writer.add_scalar("evaluation/hits10", results['hits@10'], e)
self.args.writer.add_scalar("evaluation/hits3", results['hits@3'], e)
self.args.writer.add_scalar("evaluation/hits1", results['hits@1'], e)
def save_checkpoint(self, e):
state = {'rel_embed': self.server.rel_embed,
'ent_embed': [client.ent_embed for client in self.clients]}
# delete previous checkpoint
for filename in os.listdir(self.args.state_dir):
if self.args.name in filename.split('.') and os.path.isfile(os.path.join(self.args.state_dir, filename)):
os.remove(os.path.join(self.args.state_dir, filename))
# save current checkpoint
torch.save(state, os.path.join(self.args.state_dir,
self.args.name + '.' + str(e) + '.ckpt'))
def save_model(self, best_epoch):
os.rename(os.path.join(self.args.state_dir, self.args.name + '.' + str(best_epoch) + '.ckpt'),
os.path.join(self.args.state_dir, self.args.name + '.best'))
def send_emb(self):
for k, client in enumerate(self.clients):
client.rel_embed = self.server.send_emb()
def train(self):
best_epoch = 0
best_mrr = 0
bad_count = 0
mrr_plot_result = []
loss_plot_result = []
for num_round in range(self.args.max_round):
n_sample = max(round(self.args.fraction * self.num_clients), 1)
sample_set = np.random.choice(self.num_clients, n_sample, replace=False)
self.send_emb()
round_loss = 0
for k in iter(sample_set):
client_loss = self.clients[k].client_update()
round_loss += client_loss
round_loss /= n_sample
self.server.aggregation(self.clients, self.rel_freq_mat)
logging.info('round: {} | loss: {:.4f}'.format(num_round, np.mean(round_loss)))
self.write_training_loss(np.mean(round_loss), num_round)
loss_plot_result.append(np.mean(round_loss))
if num_round % self.args.check_per_round == 0 and num_round != 0:
eval_res = self.evaluate()
self.write_evaluation_result(eval_res, num_round)
if eval_res['mrr'] > best_mrr:
best_mrr = eval_res['mrr']
best_epoch = num_round
logging.info('best model | mrr {:.4f}'.format(best_mrr))
self.save_checkpoint(num_round)
bad_count = 0
else:
bad_count += 1
logging.info('best model is at round {0}, mrr {1:.4f}, bad count {2}'.format(
best_epoch, best_mrr, bad_count))
mrr_plot_result.append(eval_res['mrr'])
if bad_count >= self.args.early_stop_patience:
logging.info('early stop at round {}'.format(num_round))
loss_file_name = 'loss/' + self.args.name + '_loss.pkl'
with open(loss_file_name, 'wb') as fp:
pickle.dump(loss_plot_result, fp)
mrr_file_name = 'loss/' + self.args.name + '_mrr.pkl'
with open(mrr_file_name, 'wb') as fp:
pickle.dump(mrr_plot_result, fp)
break
logging.info('finish training')
logging.info('save best model')
self.save_model(best_epoch)
self.before_test_load()
self.evaluate(istest=True)
def before_test_load(self):
state = torch.load(os.path.join(self.args.state_dir, self.args.name + '.best'), map_location=self.args.gpu)
self.server.rel_embed = state['rel_embed']
for idx, client in enumerate(self.clients):
client.ent_embed = state['ent_embed'][idx]
def evaluate(self, istest=False):
self.send_emb()
result = ddict(int)
if istest:
weights = self.test_eval_weights
else:
weights = self.valid_eval_weights
for idx, client in enumerate(self.clients):
client_res = client.client_eval(istest)
logging.info('mrr: {:.4f}, hits@1: {:.4f}, hits@3: {:.4f}, hits@10: {:.4f}'.format(
client_res['mrr'], client_res['hits@1'],
client_res['hits@3'], client_res['hits@10']))
for k, v in client_res.items():
result[k] += v * weights[idx]
logging.info('mrr: {:.4f}, hits@1: {:.4f}, hits@3: {:.4f}, hits@10: {:.4f}'.format(
result['mrr'], result['hits@1'],
result['hits@3'], result['hits@10']))
return result