-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn.py
379 lines (315 loc) · 11.1 KB
/
learn.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import argparse
import json
import logging
import pickle
from bisect import insort
from typing import Dict
from tqdm import tqdm
import random
import torch
from torch import optim
from datasets import Dataset
from models import CP, ComplEx, Distmult
from regularizers import F2, N3, DURA, DURA_W, DURA_RESCAL
from optimizers import KBCOptimizer, KBCOptimizer_composite
from regularizer_composite import regularizer_composite
import os
import numpy as np
import sys
print(sys.path)
big_datasets = ['WN18RR', 'FB237', 'umls', 'kinship']
datasets = big_datasets
parser = argparse.ArgumentParser(
description="Relational learning contraption"
)
parser.add_argument(
'--dataset', choices=datasets, default = 'umls',
help="Dataset in {}".format(datasets)
)
models = ['CP', 'ComplEx', 'Distmult']
parser.add_argument(
'--model', choices=models, default = 'ComplEx',
help="Model in {}".format(models)
)
regularizers = ['N3', 'F2', 'composite', 'DURA', 'DURA_W', 'DURA_RESCAL']
parser.add_argument(
'--regularizer', choices=regularizers, default='composite',
help="Regularizer in {}".format(regularizers)
)
optimizers = ['Adagrad', 'Adam', 'SGD']
parser.add_argument(
'--optimizer', choices=optimizers, default='Adagrad',
help="Optimizer in {}".format(optimizers)
)
parser.add_argument(
'--max_epochs', default=100, type=int,
help="Number of epochs."
)
parser.add_argument(
'--valid', default=2, type=float,
help="Number of epochs before valid."
)
parser.add_argument(
'--rank', default=2000, type=int,
help="Factorization rank."
)
parser.add_argument(
'--batch_size', default=100, type=int,
help="Factorization rank."
)
parser.add_argument(
'--reg', default=0, type=float,
help="Regularization weight"
)
parser.add_argument(
'--init', default=1e-3, type=float,
help="Initial scale"
)
parser.add_argument(
'--learning_rate', default=1e-1, type=float,
help="Learning rate"
)
parser.add_argument(
'--decay1', default=0.9, type=float,
help="decay rate for the first moment estimate in Adam"
)
parser.add_argument(
'--decay2', default=0.999, type=float,
help="decay rate for second moment estimate in Adam"
)
parser.add_argument(
'--save_epochs', default=10, type=int
)
parser.add_argument(
'--save_path', default='models/', type=str
)
parser.add_argument(
'--init_checkpoint', default=None, type=str
)
parser.add_argument(
'--data_path', default=None, type=str
)
parser.add_argument(
'--n_pos', default=10, type=int
)
parser.add_argument(
'--use_N3', action='store_true'
)
parser.add_argument(
'--use_N3_weight', default=0, type=float
)
parser.add_argument(
'--no_reg', action='store_true'
)
parser.add_argument(
'--mode_list', type=str
)
# weights
parser.add_argument(
'--w1', type=float, default= 0, help='weight for emb_len=1'
)
parser.add_argument(
'--w2', type=float, default= 0, help='weight for emb_len=2'
)
parser.add_argument(
'--w3', type=float, default= 0, help='weight for emb_len=3'
)
# whether fully_train
parser.add_argument(
'--fully_train', action='store_true'
)
parser.add_argument(
'--train_num', default=1000, type = int, help = 'num of examples to train'
)
parser.add_argument(
'--fact_dist', type=str, default= 'rand', choices=['rand','jaccard']
)
# DURA related
parser.add_argument(
'--use_DURA', action='store_true'
)
parser.add_argument(
'--use_DURA_W', action='store_true'
)
parser.add_argument(
'--use_DURA_RESCAL', action='store_true'
)
parser.add_argument(
'--use_DURA_weight', default=5e-2, type=float
)
parser.add_argument(
'--use_DURA_W_weight', default=1e-1, type=float
)
parser.add_argument(
'--use_DURA_RESCAL_weight', default=5e-2, type=float
)
parser.add_argument('-weight', '--do_ce_weight', action='store_true')
parser.add_argument('--seed', default=0, type=int)
args = parser.parse_args()
# args.gpu_id = os.environ['CUDA_VISIBLE_DEVICES']
args.gpu_id = 0
print('parse finished.')
def build_base(args): # build base path for checkpoints and logs
if args.regularizer == 'composite':
if args.use_N3:
model_name = f'{args.model}_lN3_{args.use_N3_weight}_w1_{args.w1}_w2_{args.w2}_w3_{args.w3}_lr_{args.learning_rate}_reg_{args.regularizer}'
else:
model_name = f'{args.model}_lN3_0_w1_{args.w1}_w2_{args.w2}_w3_{args.w3}_lr_{args.learning_rate}_reg_{args.regularizer}'
elif args.regularizer != '':
model_name = f'{args.model}_rank_{args.rank}_decay1_{args.decay1}_decay2_{args.decay2}_reg_{args.regularizer}'
elif args.no_reg:
model_name = f'{args.model}_rank_{args.rank}_decay1_{args.decay1}_decay2_{args.decay2}_noreg'
gpu_str = f'gpu_{args.gpu_id}'
base_path = os.path.join(args.save_path, gpu_str, model_name)
return base_path
def set_logger(args): # set log
base_path = build_base(args)
if not os.path.exists(base_path):
os.makedirs(base_path)
log_file = os.path.join(base_path, 'run.log')
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
filename=log_file,
filemode='w'
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def avg_both(mrs: Dict[str, float], mrrs: Dict[str, float], hits: Dict[str, torch.FloatTensor]): # average metrics for rhs and lhs
"""
aggregate metrics for missing lhs and rhs
:param mrrs: d
:param hits:
:return:
"""
m = (mrrs['lhs'] + mrrs['rhs']) / 2.
h = (hits['lhs'] + hits['rhs']) / 2.
mr = (mrs['lhs'] + mrs['rhs']) / 2.
return {'MR':mr, 'MRR': m, 'hits@[1,3,10]': h}
def save_model(model, optimizer, save_variable_list, args): # save checkpoint
'''
Save the parameters of the model and the optimizer,
as well as some other variables such as step and learning_rate
'''
base_path = build_base(args)
if not os.path.exists(base_path):
os.makedirs(base_path)
argparse_dict = vars(args)
with open(os.path.join(base_path, 'config.json'), 'w') as fjson:
json.dump(argparse_dict, fjson)
torch.save({
**save_variable_list,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()},
os.path.join(base_path, 'checkpoint')
)
def load_model(args): # load checkpoint
base_path = build_base(args)
if not os.path.exists(base_path):
os.makedirs(base_path)
checkpoint = torch.load(os.path.join(base_path, 'checkpoint'))
return checkpoint
def main(args): # model training
set_logger(args)
logging.info('Logger setting done.')
random.seed(args.seed)
dataset = Dataset(args.dataset, args.data_path)
# print('load data finished.')
logging.info('load data finished.')
examples = torch.from_numpy(dataset.get_train().astype('int64'))
if args.do_ce_weight:
ce_weight = torch.Tensor(dataset.get_weight()).cuda()
else:
ce_weight = None
device = 'cuda' # set seed
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.deterministic = True
print(dataset.get_shape())
# build regularizer
regularizer = None
if args.regularizer == 'F2':
regularizer = F2(args.reg)
elif args.regularizer == 'N3':
regularizer = N3(args.reg)
elif args.regularizer == 'DURA':
regularizer = DURA(args.reg)
elif args.regularizer == 'DURA_W':
regularizer = DURA_W(args.reg)
elif args.regularizer == 'DURA_RESCAL':
regularizer = DURA_RESCAL(args.reg)
elif args.regularizer == 'composite':
regularizer = regularizer_composite(args, args.dataset, examples, dataset.n_entities, dataset.n_predicates)
assert regularizer is not None, "Invalid regularizer: {}".format(args.regularizer)
model = {
'CP': lambda: CP(dataset.get_shape(), args.rank, args.init),
'ComplEx': lambda: ComplEx(dataset.get_shape(), args.rank, args.init),
'Distmult': lambda: Distmult(dataset.get_shape(), args.rank, args.init)
}[args.model]()
logging.info(device)
model = model.to(device)
optim_method = {
'Adagrad': lambda: optim.Adagrad(model.parameters(), lr=args.learning_rate),
'Adam': lambda: optim.Adam(model.parameters(), lr=args.learning_rate),
'SGD': lambda: optim.SGD(model.parameters(), lr=args.learning_rate)
}[args.optimizer]()
# build optimizer
if args.regularizer in ['lowrank', 'composite']:
optimizer = KBCOptimizer_composite(args, model, regularizer, optim_method, args.batch_size, \
regularizer_N3 = N3(args.use_N3_weight), regularizer_DURA = DURA(args.use_DURA_weight), \
regularizer_DURA_W = DURA_W(args.use_DURA_W_weight), regularizer_DURA_RESCAL = DURA_RESCAL(args.use_DURA_RESCAL_weight))
else:
optimizer = KBCOptimizer(model, regularizer, optim_method, args.batch_size)
# load checkpoint if needed
if args.init_checkpoint:
# Restore model from checkpoint directory
logging.info('Loading checkpoint %s...' % args.init_checkpoint)
checkpoint = torch.load(os.path.join(args.init_checkpoint, 'checkpoint'))
init_step = checkpoint['step']
model.load_state_dict(checkpoint['model_state_dict'])
if not args.view_predict:
optimizer.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
else:
logging.info('Ramdomly Initializing %s Model...' % args.model)
init_step = 0
now = []
cur_loss = 0
best_val_acc = 0
curve = {'train': [], 'valid': [], 'test': []}
for e in range(args.max_epochs): # train model
cur_loss = optimizer.epoch(examples, weight=ce_weight) # use this to train the model
if (e + 1) % args.valid == 0:
valid, test = [
avg_both(*dataset.eval(model, split, -1 if split != 'train' else 50000))
for split in ['valid', 'test']
]
curve['valid'].append(valid)
curve['test'].append(test)
val_acc = valid['MRR']
if val_acc > best_val_acc:
best_val_acc = val_acc
to_save = True
else:
to_save = False
logging.info(f"\t EPOCH: {e} BEST_VALID: {best_val_acc}")
logging.info(f"\t EPOCH: {e} VALID: {valid}")
logging.info(f"\t EPOCH: {e} TEST: {test}")
if to_save:
save_variable_list = {
'step': e+1
}
save_model(optimizer.model, optimizer.optimizer, save_variable_list, args)
checkpoint = load_model(args)
model.load_state_dict(checkpoint['model_state_dict'])
avg_valid_results = avg_both(*dataset.eval(model, 'valid', -1))
avg_results = avg_both(*dataset.eval(model, 'test', -1))
logging.info(f"\n\nVALID : {avg_valid_results}")
logging.info(f"\n\nTEST : {avg_results}")
print(avg_results)
if __name__ == '__main__':
main(args)