-
Notifications
You must be signed in to change notification settings - Fork 0
/
gold.py
539 lines (483 loc) · 18.8 KB
/
gold.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import argparse
import logging
import os
import random
import numpy as np
import torch
import torch.nn as nn
from sklearn import metrics
from torch.utils.data import DataLoader
from tqdm import tqdm
from config import Config
from data_loader import Dataset
from models import Encoder
os.environ["TOKENIZERS_PARALLELISM"] = "false"
LOG_FORMAT = "[%(asctime)s][%(levelname)s] %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)
def run_train(config, pretrained_entity_embeddings, pretrained_relation_embeddings):
model = Encoder(
config,
config.rnn_input_size,
config.rnn_hidden_size,
config.rnn_num_layers,
config.dropout,
config.alpha,
config.device,
use_ptlm=True,
pretrained_entity_embeddings=pretrained_entity_embeddings,
pretrained_relation_embeddings=pretrained_relation_embeddings,
)
batch_size = config.batch_size
if torch.cuda.device_count() > 1:
logging.info("try to use {} GPUs".format(torch.cuda.device_count()))
model = nn.DataParallel(model)
model.to(config.device)
criterion = nn.MarginRankingLoss(config.gama, reduce=False)
optimizer = torch.optim.Adam(model.parameters(), lr=config.learning_rate)
data_loader = DataLoader(
range(config.nr_train_samples),
batch_size=batch_size,
collate_fn=dataset.gen_train_batch,
num_workers=0,
)
logging.info("batch size = {}".format(batch_size))
all_sum = 0
model_saved_path = config.dataset_name + "_" + config.model_name + ".ckpt"
all_acc = 0
for epoch in range(config.num_epochs):
with tqdm(total=len(data_loader)) as TD:
for idx, sample in tqdm(enumerate(data_loader)):
TD.set_description("[epoch {}/{}]".format(epoch + 1, config.num_epochs))
(
batch_h,
batch_r,
batch_t,
batch_rule_h,
batch_rule_r,
batch_rule_t,
batch_rule_c,
masks,
batch_size,
) = sample
batch_h = torch.LongTensor(batch_h).to(config.device)
batch_r = torch.LongTensor(batch_r).to(config.device)
batch_t = torch.LongTensor(batch_t).to(config.device)
batch_rule_h = torch.LongTensor(batch_rule_h).to(config.device)
batch_rule_r = torch.LongTensor(batch_rule_r).to(config.device)
batch_rule_t = torch.LongTensor(batch_rule_t).to(config.device)
batch_rule_c = torch.FloatTensor(batch_rule_c).to(config.device)
masks_sum = sum(masks)
masks = torch.FloatTensor(masks).to(config.device)
out, out_att, rules_repr = model(
batch_h,
batch_r,
batch_t,
batch_rule_h,
batch_rule_r,
batch_rule_t,
batch_rule_c,
)
out = out.reshape(batch_size, -1, 2 * 3 * config.rnn_hidden_size)
out_att = out_att.reshape(
batch_size, -1, 2 * 3 * config.rnn_hidden_size
)
rules_repr = rules_repr.reshape(
batch_size, -1, config.rule_inst_cnt, 2 * 3 * config.rnn_hidden_size
)
batch_rule_c = batch_rule_c.reshape(
batch_size, -1, config.rule_inst_cnt
)
pos_h = out[:, 0, :]
neg_hs = []
neg_zs = []
for i in range(config.neg_cnt):
neg_hs.append(out[:, i + 1, :])
neg_zs.append(out_att[:, 2 * i + 2, :])
neg_zs.append(out_att[:, 2 * i + 3, :])
pos_z0 = out_att[:, 0, :]
pos_z1 = out_att[:, 1, :]
pos_loss = config.local_lambda * torch.norm(
pos_z0 - pos_z1, p=2, dim=1
) + config.global_lambda * (
torch.norm(
pos_h.unsqueeze(dim=1).repeat(1, config.rule_inst_cnt, 1)
- rules_repr[:, 0, :, :],
p=2,
dim=2,
)
* batch_rule_c[:, 0, :]
).mean(
dim=1
)
neg_loss = torch.zeros(batch_size).to(config.device)
neg_losses = []
for i in range(config.neg_cnt):
tmp_loss = config.local_lambda * torch.norm(
neg_zs[i * 2] - neg_zs[i * 2 + 1], p=2, dim=1
) + config.global_lambda * (
torch.norm(
neg_hs[i]
.unsqueeze(dim=1)
.repeat(1, config.rule_inst_cnt, 1)
- rules_repr[:, i + 1, :, :],
p=2,
dim=2,
)
* batch_rule_c[:, i + 1, :]
).mean(
dim=1
)
neg_losses.append(tmp_loss)
y = -torch.ones(batch_size).to(config.device)
losses = []
for j in range(config.neg_cnt):
losses.append(
criterion(pos_loss, neg_losses[j], y).reshape(-1, batch_size)
)
loss = losses[0].mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
TD.set_postfix(loss="{:.5f}".format(loss.item()))
TD.update(1)
logging.info("start saving model ...")
torch.save(model.state_dict(), model_saved_path[:-5] + str(epoch + 1) + ".ckpt")
logging.info("model saved")
logging.info("start evaluating")
model.eval()
if True:
all_triple = []
with torch.no_grad():
test_data_loader = DataLoader(
range(config.nr_train_samples),
batch_size=config.batch_size,
collate_fn=dataset.gen_test_batch,
num_workers=0,
)
for sample in tqdm(test_data_loader):
(
batch_h,
batch_r,
batch_t,
batch_rule_h,
batch_rule_r,
batch_rule_t,
batch_rule_c,
labels,
org,
masks,
batch_size,
) = sample
batch_h = torch.LongTensor(batch_h).to(config.device)
batch_t = torch.LongTensor(batch_t).to(config.device)
batch_r = torch.LongTensor(batch_r).to(config.device)
masks = torch.FloatTensor(masks).to(config.device)
batch_rule_h = torch.LongTensor(batch_rule_h).to(config.device)
batch_rule_r = torch.LongTensor(batch_rule_r).to(config.device)
batch_rule_t = torch.LongTensor(batch_rule_t).to(config.device)
batch_rule_c = torch.FloatTensor(batch_rule_c).to(config.device)
out, out_att, rules_repr = model(
batch_h,
batch_r,
batch_t,
batch_rule_h,
batch_rule_r,
batch_rule_t,
batch_rule_c,
)
out_att = out_att.reshape(
batch_size, 2, 2 * 3 * config.rnn_hidden_size
)
out_att_view0 = out_att[:, 0, :]
out_att_view1 = out_att[:, 1, :]
rules_repr = rules_repr.reshape(
batch_size,
-1,
config.rule_inst_cnt,
2 * 3 * config.rnn_hidden_size,
)
batch_rule_c = batch_rule_c.reshape(
batch_size, -1, config.rule_inst_cnt
)
loss = config.local_lambda * torch.norm(
out_att_view0 - out_att_view1, p=2, dim=1
) + config.global_lambda * (
torch.norm(
out.unsqueeze(dim=1).repeat(1, config.rule_inst_cnt, 1)
- rules_repr[:, 0, :, :],
p=2,
dim=2,
)
* batch_rule_c[:, 0, :]
).mean(
dim=1
)
loss = loss * masks
loss = list(loss.to("cpu").numpy())
for j in range(len(labels)):
all_triple.append([org[j], labels[j], loss[j]])
all_triple.sort(key=lambda x: -x[2])
tloss = []
tlabel = []
for j in all_triple:
tloss.append(j[2])
tlabel.append(j[1])
auc = metrics.roc_auc_score(tlabel, tloss)
crr = 0
for j in range(config.nr_error):
crr += all_triple[j][1]
logging.info(
"[ACC REPORT] Epoch {}, Acc = {:.5f}, AUC = {:.5f}".format(
epoch + 1, crr / config.nr_error, auc
)
)
model.train()
def run_test(
config, pretrained_entity_embeddings, pretrained_relation_embeddings, train_list
):
model_test = Encoder(
config,
config.rnn_input_size,
config.rnn_hidden_size,
config.rnn_num_layers,
config.dropout,
config.alpha,
config.device,
use_ptlm=True,
pretrained_entity_embeddings=pretrained_entity_embeddings,
pretrained_relation_embeddings=pretrained_relation_embeddings,
)
model_test.to(config.device)
model_saved_path = (
config.dataset_name + "_" + config.model_name + str(config.num_epochs) + ".ckpt"
)
logging.info("start loading model[{}] ...".format(model_saved_path))
model_test.load_state_dict(torch.load(model_saved_path))
logging.info("model loaded!")
model_test.eval()
batch_size = config.batch_size
data_loader = DataLoader(
range(config.nr_train_samples),
batch_size=config.batch_size,
collate_fn=dataset.gen_test_batch,
num_workers=0,
)
all_loss = []
all_label = []
result_file_name = config.dataset_name + "_" + config.model_name
if config.output_tsv:
result_file_tsv = open(result_file_name + ".tsv", "w")
with torch.no_grad():
for sample in tqdm(data_loader):
(
batch_h,
batch_r,
batch_t,
batch_rule_h,
batch_rule_r,
batch_rule_t,
batch_rule_c,
labels,
org,
masks,
batch_size,
) = sample
batch_h = torch.LongTensor(batch_h).to(config.device)
batch_t = torch.LongTensor(batch_t).to(config.device)
batch_r = torch.LongTensor(batch_r).to(config.device)
batch_rule_h = torch.LongTensor(batch_rule_h).to(config.device)
batch_rule_r = torch.LongTensor(batch_rule_r).to(config.device)
batch_rule_t = torch.LongTensor(batch_rule_t).to(config.device)
batch_rule_c = torch.FloatTensor(batch_rule_c).to(config.device)
out, out_att, rules_repr = model_test(
batch_h,
batch_r,
batch_t,
batch_rule_h,
batch_rule_r,
batch_rule_t,
batch_rule_c,
)
rules_repr = rules_repr.reshape(
batch_size, -1, config.rule_inst_cnt, 2 * 3 * config.rnn_hidden_size
)
batch_rule_c = batch_rule_c.reshape(batch_size, -1, config.rule_inst_cnt)
out_att = out_att.reshape(batch_size, 2, 2 * 3 * config.rnn_hidden_size)
out_att_view0 = out_att[:, 0, :]
out_att_view1 = out_att[:, 1, :]
loss = config.local_lambda * torch.norm(
out_att_view0 - out_att_view1, p=2, dim=1
) + config.global_lambda * (
torch.norm(
out.unsqueeze(dim=1).repeat(1, config.rule_inst_cnt, 1)
- rules_repr[:, 0, :, :],
p=2,
dim=2,
)
* batch_rule_c[:, 0, :]
).mean(
dim=1
)
all_loss += list(loss.to("cpu").numpy())
all_label += labels
if config.output_tsv:
for j in range(len(labels)):
h, r, t = train_list[org[j]][0]
l = labels[j]
s = loss[j].item()
print(
h,
r,
t,
dataset.entity[h],
dataset.relation[r],
dataset.entity[t],
l,
s,
sep="\t",
file=result_file_tsv,
)
all_loss = np.array(all_loss)
all_loss = torch.from_numpy(all_loss)
auc = metrics.roc_auc_score(all_label, all_loss)
idx = list(range(len(all_loss)))
idx.sort(key=lambda x: -all_loss[x])
acc = 0
for i in range(config.nr_error):
acc += all_label[idx[i]]
logging.info(
"[TEST RESULT] Acc = {:.5f}, AUC = {:.5f}".format(
acc / config.nr_error, auc
)
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="C-05",
choices=["C-05", "C-10", "C-20", "A-05", "A-10", "A-20"],
help="The dataset to be used. Choose from 'C-05', 'C-10', 'C-20', 'A-05', 'A-10', 'A-20'.",
)
parser.add_argument(
"--model_name", type=str, default="noname", help="The name of the model."
)
parser.add_argument(
"--epoch", type=int, default=10, help="The number of training epochs."
)
parser.add_argument(
"--batch_size",
type=int,
default=256,
help="The batch size for training and inference.",
)
parser.add_argument(
"--topk", type=int, default=100, help="The number of top rules to use."
)
parser.add_argument(
"--ptlm_model",
type=str,
default="sentence-transformers/sentence-t5-xxl",
help="The name of the pre-trained language model to be used.",
)
parser.add_argument(
"--lr",
type=float,
default=0.001,
help="The learning rate for training the model.",
)
parser.add_argument(
"--local_lambda",
type=float,
default=0.1,
help="The value of the lambda_local parameter.",
)
parser.add_argument(
"--global_lambda",
type=float,
default=0.01,
help="The value of the lambda_global parameter.",
)
parser.add_argument(
"--neg_cnt",
type=int,
default=1,
help="The number of negative samples to use during training.",
)
parser.add_argument(
"--seed", type=int, default=5, help="The random seed for reproducibility."
)
parser.add_argument(
"--test_only",
action="store_true",
help="Flag indicating whether to run in test mode only.",
)
parser.add_argument(
"--not_use_ptlm",
action="store_true",
help="Flag indicating whether to disable the use of pre-trained language models.",
)
parser.add_argument(
"--output_tsv",
action="store_true",
help="Flag indicating whether to output the results in TSV format.",
)
args = parser.parse_args()
config = Config()
config.ptlm_model = args.ptlm_model
config.num_epochs = args.epoch
config.model_name = args.model_name
config.neg_cnt = args.neg_cnt
config.local_lambda = args.local_lambda
config.learning_rate = args.lr
config.global_lambda = args.global_lambda
config.dataset = args.dataset
config.rule_top_k = args.topk
config.seed = args.seed
config.use_ptlm = not (args.not_use_ptlm)
config.output_tsv = args.output_tsv
random.seed(config.seed)
np.random.seed(config.seed)
torch.manual_seed(config.seed)
if "C" in config.dataset:
config.dataset_name = "conceptnet"
config.dataset_path = "./dataset/conceptnet"
else:
config.dataset_name = "atomic"
config.dataset_path = "./dataset/atomic"
config.error_ratio = int(config.dataset.split("-")[-1]) / 100.0
if not os.path.exists("log"):
os.makedirs("log")
file_handler = logging.FileHandler(
os.path.join(
"log/",
config.dataset + "_" + config.model_name + "_log.txt",
)
)
logger = logging.getLogger()
logger.addHandler(file_handler)
logging.info("only for test ? {}".format(args.test_only))
dataset = Dataset(config=config)
config.nr_train_samples, config.nr_error = dataset.read()
(
pretrained_entity_embeddings,
pretrained_relation_embeddings,
) = dataset.get_pretrained_embedding()
config.total_ent = dataset.entity.cnt
config.total_rel = dataset.relation.cnt
config.total_triple = dataset.triple.cnt
config.batch_size = args.batch_size
if args.test_only == False:
original_batch_size = config.batch_size
config.batch_size *= torch.cuda.device_count()
run_train(config, pretrained_entity_embeddings, pretrained_relation_embeddings)
config.batch_size = original_batch_size
logging.info("start testing")
run_test(
config,
pretrained_entity_embeddings,
pretrained_relation_embeddings,
dataset.train_list,
)
print("DONE")