-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
292 lines (266 loc) · 9.88 KB
/
train.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
281
282
283
284
285
286
287
288
289
290
291
292
import os
import sys
import timeit
import logging
import argparse
from collections import defaultdict as ddict
from tqdm import tqdm
from sklearn.metrics import roc_auc_score
import numpy as np
import torch
from torch.autograd import Variable
import torch.multiprocessing as mp
from torch.optim import Adam
from torch.utils.data import DataLoader
import models, train
def wordnet(model, data, optimizer, opt, log, cuda, test_adjacency):
loader = DataLoader(
data,
batch_size=opt.batchsize,
shuffle=True,
num_workers=opt.ndproc,
collate_fn=data.collate
)
min_rank = (np.Inf, -1)
max_AUC = (0, -1)
iter_counter = 0
former_loss = np.Inf
t_start = timeit.default_timer()
while True:
train_loss = []
loss = None
for inputs, targets in loader:
if cuda:
inputs = inputs.cuda()
targets = targets.cuda()
optimizer.zero_grad()
preds = model(inputs)
loss = model.module.loss(preds, targets, size_average=True)
loss.backward()
optimizer.step()
train_loss.append(loss.item())
iter_counter+=1
if iter_counter % opt.eval_each == 0:
model.eval()
eval_elapsed = timeit.default_timer()
MR, AUC, ela = evaluation(model, opt.distfn, test_adjacency, opt.neproc, cuda=cuda, verbose=True)
eval_elapsed = timeit.default_timer() - eval_elapsed
model.train()
if MR < min_rank[0]:
min_rank = (MR, iter_counter)
if AUC > max_AUC[0]:
max_AUC = (AUC, iter_counter)
log.info(
('[%s] Eval: {'
'"iter": %d, '
'"loss": %.6f, '
'"elapsed (for %d iter.)": %.2f, '
'"elapsed (for eval.)": %.2f, '
'"auc": %.6f, '
'"best_auc": %.6f'
'}') % (
opt.name, iter_counter, np.mean(train_loss), opt.eval_each, timeit.default_timer() - t_start, eval_elapsed, AUC, max_AUC[0])
)
former_loss = np.mean(train_loss)
train_loss = []
t_start = timeit.default_timer()
if iter_counter >= opt.iters:
log.info(
('[%s] RESULT: {'
'"auc": %.6f, '
'}') % (
opt.name, max_AUC[0])
)
print(""" save model """)
torch.save({
'model': model.state_dict(),
'auc': max_AUC[0],
'iteration': iter_counter
}, f'{opt.name}.pth')
sys.exit()
def co_author(model, data, vectors, optimizer, opt, log, cuda, test_adjacency, valid_adjacency):
loader = DataLoader(
data,
batch_size=opt.batchsize,
shuffle=True,
num_workers=opt.ndproc,
collate_fn=data.collate
)
max_AUC = (0, -1)
iter_counter = 0
final_AUC = 0
former_loss = np.Inf
t_start = timeit.default_timer()
best_model = {
'model': None,
'iteration': 0
}
while True:
train_loss = []
loss = None
for inputs, targets in loader:
if cuda:
inputs = inputs.cuda()
targets = targets.cuda()
optimizer.zero_grad()
preds = model(inputs)
loss = model.module.loss(preds, targets, size_average=True)
loss.backward()
optimizer.step()
train_loss.append(loss.item())
iter_counter+=1
if iter_counter % opt.eval_each == 0:
model.eval()
_, AUC, ela = evaluation(model, opt.distfn, valid_adjacency, opt.neproc, vectors, cuda=cuda, verbose=True)
model.train()
log.info(
('[%s] Validation: {'
'"iter": %d, '
'"loss": %.6f, '
'"elapsed (for %d iter.)": %.2f, '
'"val_auc": %.6f, '
'"best_val_auc": %.6f}'
'"test_auc@best_val_auc": %.6f}') % (
opt.name, iter_counter, np.mean(train_loss), opt.eval_each, timeit.default_timer() - t_start, AUC, max_AUC[0], final_AUC)
)
if AUC > max_AUC[0]:
max_AUC = (AUC, iter_counter)
model.eval()
_, final_AUC, ela = evaluation(model, opt.distfn, test_adjacency, opt.neproc, vectors, cuda=cuda, verbose=True)
model.train()
log.info(
('[%s] Test: {'
'"iter": %d, '
'"auc": %.6f, '
'}') % (
opt.name, iter_counter, final_AUC)
)
best_model['model'] = model.state_dict()
best_model['iter'] = iter_counter
former_loss = np.mean(train_loss)
train_loss = []
t_start = timeit.default_timer()
if iter_counter >= opt.iters:
model.eval()
_, AUC, ela = evaluation(model, opt.distfn, test_adjacency, opt.neproc, vectors, cuda=cuda, verbose=True)
model.train()
log.info(
('[%s] Test@LastIteration: {'
'"iter": %d, '
'"test_auc": %.6f, '
'"test_auc@best_val_auc": %.6f}'
'}') % (
opt.name, iter_counter, AUC, final_AUC)
)
print(""" save model """)
torch.save(best_model, f'{opt.name}.pth')
sys.exit()
def evaluation(model, name, adjacency, neproc, vectors=None, cuda=False, verbose=False):
t_start = timeit.default_timer()
adjacency = list(adjacency.items())
chunk = int(len(adjacency)/neproc + 1)
if vectors is not None:
with torch.no_grad():
vectors = Variable(torch.from_numpy(vectors).float())
if cuda:
vectors = vectors.cuda()
embeds = model.module.embed(vectors)
else:
embeds = model.module.embed()
queue = mp.Manager().Queue()
processes = []
for rank in range(neproc):
if "sips" in name:
p = mp.Process(
target=eval_sips_thread,
args=(adjacency[rank*chunk:(rank+1)*chunk], model, embeds, queue, rank==0 and verbose)
)
else:
p = mp.Process(
target=eval_thread,
args=(adjacency[rank*chunk:(rank+1)*chunk], model, embeds, queue, rank==0 and verbose)
)
p.start()
processes.append(p)
ranks = list()
ap_scores = list()
for i in range(neproc):
msg = queue.get()
_ranks, _ap_scores = msg
ranks += _ranks
ap_scores += _ap_scores
return np.mean(ranks), np.mean(ap_scores), timeit.default_timer()-t_start
def eval_thread(adjacency_thread, model, embeds, queue, verbose):
lt = torch.from_numpy(embeds[0])
with torch.no_grad():
embedding = Variable(lt)
ranks = []
ap_scores = []
if verbose : bar = tqdm(desc='Eval', total=len(adjacency_thread), mininterval=1, bar_format='{desc}: {percentage:3.0f}% ({remaining} left)')
for s, s_adjacency in adjacency_thread:
if verbose : bar.update()
s = torch.tensor(s)
with torch.no_grad():
s_e = Variable(lt[s].expand_as(embedding))
_dists = model.module.distfn(s_e, embedding).data.cpu().numpy().flatten()
_dists[s] = 1e+12
_labels = np.zeros(embedding.size(0))
_dists_masked = _dists.copy()
_ranks = []
for o in s_adjacency:
o = torch.tensor(o)
_dists_masked[o] = np.Inf
_labels[o] = 1
""" MAP """
_ap_scores = roc_auc_score(_labels, -_dists)
ap_scores.append(_ap_scores)
for o in s_adjacency:
o = torch.tensor(o)
d = _dists_masked.copy()
d[o] = _dists[o]
""" Mean rank """
r = np.argsort(d)
_ranks.append(np.where(r == o)[0][0] + 1)
ranks += _ranks
if verbose : bar.close()
queue.put(
(ranks, ap_scores)
)
def eval_sips_thread(adjacency_thread, model, embeds, queue, verbose):
assert(len(embeds) == 2)
lt = torch.from_numpy(embeds[0])
ltb = torch.from_numpy(embeds[1])
with torch.no_grad():
embedding = Variable(lt)
embeddingb = Variable(ltb)
ranks = []
ap_scores = []
if verbose : bar = tqdm(desc='Eval', total=len(adjacency_thread), mininterval=1, bar_format='{desc}: {percentage:3.0f}% ({remaining} left)')
for s, s_adjacency in adjacency_thread:
s = torch.tensor(s)
if verbose : bar.update()
with torch.no_grad():
s_e = Variable(lt[s].expand_as(embedding))
s_eb = Variable(ltb[s].expand_as(embeddingb))
_dists = model.module.distfn(s_e, s_eb, embedding, embeddingb).data.cpu().numpy().flatten()
_dists[s] = 1e+12
_labels = np.zeros(embedding.size(0))
_dists_masked = _dists.copy()
_ranks = []
for o in s_adjacency:
o = torch.tensor(o)
_dists_masked[o] = np.Inf
_labels[o] = 1
_ap_scores = roc_auc_score(_labels, -_dists)
ap_scores.append(_ap_scores)
for o in s_adjacency:
o = torch.tensor(o)
d = _dists_masked.copy()
d[o] = _dists[o]
r = np.argsort(d)
_ranks.append(np.where(r == o)[0][0] + 1)
ranks += _ranks
if verbose : bar.close()
queue.put(
(ranks, ap_scores)
)