-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTransR.py
679 lines (540 loc) · 27.9 KB
/
TransR.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
import codecs
import numpy as np
import copy
import time
import random
import matplotlib.pyplot as plt
import json
import operator # operator模块输出一系列对应Python内部操作符的函数
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
entities2id = {}
relations2id = {}
relation_tph = {}
relation_hpt = {}
def dataloader(file1, file2, file3, file4):
print("load file...")
entity = []
relation = []
with open(file2, 'r') as f1, open(file3, 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
for line in lines1:
line = line.strip().split('\t')
if len(line) != 2:
continue
entities2id[line[0]] = line[1]
entity.append(int(line[1]))
for line in lines2:
line = line.strip().split('\t')
if len(line) != 2:
continue
relations2id[line[0]] = line[1]
relation.append(int(line[1]))
triple_list = []
relation_head = {}
relation_tail = {}
with codecs.open(file1, 'r') as f:
content = f.readlines()
for line in content:
triple = line.strip().split("\t")
if len(triple) != 3:
continue
h_ = int(entities2id[triple[0]])
r_ = int(relations2id[triple[1]])
t_ = int(entities2id[triple[2]])
triple_list.append([h_, r_, t_])
if r_ in relation_head:
if h_ in relation_head[r_]:
relation_head[r_][h_] += 1
else:
relation_head[r_][h_] = 1
else:
relation_head[r_] = {}
relation_head[r_][h_] = 1
if r_ in relation_tail:
if t_ in relation_tail[r_]:
relation_tail[r_][t_] += 1
else:
relation_tail[r_][t_] = 1
else:
relation_tail[r_] = {}
relation_tail[r_][t_] = 1
for r_ in relation_head:
sum1, sum2 = 0, 0
for head in relation_head[r_]:
sum1 += 1
sum2 += relation_head[r_][head]
tph = sum2 / sum1
relation_tph[r_] = tph
for r_ in relation_tail:
sum1, sum2 = 0, 0
for tail in relation_tail[r_]:
sum1 += 1
sum2 += relation_tail[r_][tail]
hpt = sum2 / sum1
relation_hpt[r_] = hpt
valid_triple_list = []
with codecs.open(file4, 'r') as f:
content = f.readlines()
for line in content:
triple = line.strip().split("\t")
if len(triple) != 3:
continue
h_ = int(entities2id[triple[0]])
r_ = int(relations2id[triple[1]])
t_ = int(entities2id[triple[2]])
valid_triple_list.append([h_, r_, t_])
print("Complete load. entity : %d , relation : %d , train triple : %d, , valid triple : %d" % (
len(entity), len(relation), len(triple_list), len(valid_triple_list)))
return entity, relation, triple_list, valid_triple_list
class TransR(nn.Module):
def __init__(self, entity_num, relation_num, ent_dim, rel_dim, margin, norm, C):
super(TransR, self).__init__()
self.entity_num = entity_num
self.relation_num = relation_num
self.ent_dim = ent_dim
self.rel_dim = rel_dim
self.margin = margin
self.norm = norm
self.C = C
self.ent_embedding = torch.nn.Embedding(num_embeddings=self.entity_num,
embedding_dim=self.ent_dim).cuda()
self.rel_embedding = torch.nn.Embedding(num_embeddings=self.relation_num,
embedding_dim=self.rel_dim).cuda()
self.rel_matrix = torch.nn.Embedding(num_embeddings= self.relation_num,
embedding_dim=self.ent_dim*self.rel_dim).cuda()
self.loss_F = nn.MarginRankingLoss(self.margin, reduction="mean").cuda()
self.__data_init()
def __data_init(self):
# embedding.weight (Tensor) -形状为(num_embeddings, embedding_dim)的嵌入中可学习的权值
nn.init.xavier_uniform_(self.ent_embedding.weight.data)
nn.init.xavier_uniform_(self.rel_embedding.weight.data)
identity = torch.zeros(self.ent_dim, self.rel_dim)
for i in range(min(self.ent_dim, self.rel_dim)):
identity[i][i] = 1
identity = identity.view(self.ent_dim * self.rel_dim)
for i in range(self.relation_num):
self.rel_matrix.weight.data[i] = identity
def input_pre_transe(self, ent_vector, rel_vector):
for i in range(self.entity_num):
self.ent_embedding.weight.data[i] = torch.from_numpy(np.array(ent_vector[i]))
for i in range(self.relation_num):
self.rel_embedding.weight.data[i] = torch.from_numpy(np.array(rel_vector[i]))
def input_pre_transr(self, ent_vector, rel_vector, rel_matrix):
for i in range(self.entity_num):
self.ent_embedding.weight.data[i] = torch.from_numpy(np.array(ent_vector[i]))
for i in range(self.relation_num):
self.rel_embedding.weight.data[i] = torch.from_numpy(np.array(rel_vector[i]))
for i in range(self.relation_num):
self.rel_matrix.weight.data[i] = torch.from_numpy(np.array(rel_matrix[i]))
def transfer(self, e, rel_mat):
# view 的作用是重新将一个tensor转化成另一个形状
# 数据按照行优先的顺序排成一个一维的数据(这里应该是因为要求地址是连续存储的),然后按照参数组合成其他维度的tensor
# -1 表示根据其他维度来自动计算这一维的数量
# view 的领一个作用是,让新数据和原数据共享内存,因此我们在修改rel_matrix 的数据后,原数据rel_mat也会改变
e = F.normalize(e, 2, -1)
rel_matrix = rel_mat.view(-1, self.ent_dim, self.rel_dim)
e = e.view(-1, 1, self.ent_dim)
e = torch.matmul(e, rel_matrix)
return e.view(-1, self.rel_dim)
def distance(self, h, r, t):
# 在 tensor 的指定维度操作就是对指定维度包含的元素进行操作,如果想要保持结果的维度不变,设置参数keepdim=True即可
# 如 下面sum中 r_norm * h 结果是一个1024 *50的矩阵(2维张量) sum在dim的结果就变成了 1024的向量(1位张量) 如果想和r_norm对应元素两两相乘
# 就需要sum的结果也是2维张量 因此需要使用keepdim= True报纸张量的维度不变
# 另外关于 dim 等于几表示张量的第几个维度,从0开始计数,可以理解为张量的最开始的第几个左括号,具体可以参考这个https://www.cnblogs.com/flix/p/11262606.html
# torch.nn.embedding类的forward只接受longTensor类型的张量
head = self.ent_embedding(h)
rel = self.rel_embedding(r)
rel_mat = self.rel_matrix(r)
tail = self.ent_embedding(t)
head = self.transfer(head, rel_mat)
tail = self.transfer(tail, rel_mat)
head = F.normalize(head, 2, -1)
rel = F.normalize(rel, 2, -1)
tail = F.normalize(tail, 2, -1)
distance = head + rel - tail
# dim = -1表示的是维度的最后一维 比如如果一个张量有3维 那么 dim = 2 = -1, dim = 0 = -3
score = torch.norm(distance, p = self.norm, dim=1)
return score
def test_distance(self, h, r, t):
head = self.ent_embedding(h.cuda())
rel = self.rel_embedding(r.cuda())
rel_mat = self.rel_matrix(r.cuda())
tail = self.ent_embedding(t.cuda())
head = self.transfer(head, rel_mat)
tail = self.transfer(tail, rel_mat)
distance = head + rel - tail
# dim = -1表示的是维度的最后一维 比如如果一个张量有3维 那么 dim = 2 = -1, dim = 0 = -3
score = torch.norm(distance, p=self.norm, dim=1)
return score.cpu().detach().numpy()
def scale_loss(self, embedding):
return torch.sum(
torch.max(
torch.sum(
embedding ** 2, dim=1, keepdim=True
)-torch.autograd.Variable(torch.FloatTensor([1.0]).cuda()),
torch.autograd.Variable(torch.FloatTensor([0.0]).cuda())
))
def forward(self, current_triples, corrupted_triples):
h, r, t = torch.chunk(current_triples, 3, dim=1)
h_c, r_c, t_c = torch.chunk(corrupted_triples, 3, dim=1)
h = torch.squeeze(h, dim=1).cuda()
r = torch.squeeze(r, dim=1).cuda()
t = torch.squeeze(t, dim=1).cuda()
h_c = torch.squeeze(h_c, dim=1).cuda()
r_c = torch.squeeze(r_c, dim=1).cuda()
t_c = torch.squeeze(t_c, dim=1).cuda()
entity_embedding = self.ent_embedding(torch.cat([h, t, h_c, t_c]).cuda())
relation_embedding = self.rel_embedding(torch.cat([r, r_c]).cuda())
pos = self.distance(h, r, t)
neg = self.distance(h_c, r_c, t_c)
# loss_F = max(0, -y*(x1-x2) + margin)
y = Variable(torch.Tensor([-1])).cuda()
loss = self.loss_F(pos, neg, y)
ent_scale_loss = self.scale_loss(entity_embedding) / len(entity_embedding)
rel_scale_loss = self.scale_loss(relation_embedding) / len(relation_embedding)
# + self.C * (ent_scale_loss + rel_scale_loss)
return loss
class TransR_Training:
def __init__(self, entity_set, relation_set, triple_list, ent_dim=50, rel_dim=50, lr=0.01, margin=1.0, norm=1, C = 1.0, valid_triples = None):
self.entities = entity_set
self.relations = relation_set
self.triples = triple_list
self.ent_dim = ent_dim
self.rel_dim = rel_dim
self.learning_rate = lr
self.margin = margin
self.norm = norm
self.loss = 0.0
self.valid_loss = 0.0
self.valid_triples = valid_triples
self.C = C
self.train_loss = []
self.validation_loss = []
self.test_triples = []
def data_initialise(self, transe_ent = None, transe_rel = None):
self.model = TransR(len(self.entities), len(self.relations), self.ent_dim, self.rel_dim, self.margin, self.norm, self.C)
# self.optim = optim.SGD(self.model.parameters(), lr=self.learning_rate)
self.optim = optim.Adam(self.model.parameters(), lr=self.learning_rate)
if transe_ent != None and transe_rel != None:
entity_dic = {}
relation_dic = {}
with codecs.open(transe_ent, 'r') as f1, codecs.open(transe_rel, 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
for line in lines1:
line = line.strip().split('\t')
if len(line) != 2:
continue
entity_dic[int(line[0])] = json.loads(line[1])
for line in lines2:
line = line.strip().split('\t')
if len(line) != 2:
continue
relation_dic[int(line[0])] = json.loads(line[1])
self.model.input_pre_transe(entity_dic, relation_dic)
def insert_pre_data(self, file1, file2, file3):
entity_dic = {}
relation = {}
rel_mat = {}
with codecs.open(file1, 'r') as f1, codecs.open(file2, 'r') as f2, codecs.open(file3, 'r') as f3:
lines1 = f1.readlines()
lines2 = f2.readlines()
lines3 = f3.readlines()
for line in lines1:
line = line.strip().split('\t')
if len(line) != 2:
continue
entity_dic[int(line[0])] = json.loads(line[1])
for line in lines2:
line = line.strip().split('\t')
if len(line) != 2:
continue
relation[int(line[0])] = json.loads(line[1])
for line in lines3:
line = line.strip().split('\t')
if len(line) != 2:
continue
rel_mat[int(line[0])] = json.loads(line[1])
self.model.input_pre_transr(entity_dic, relation, rel_mat)
def insert_test_data(self, file1, file2, file3, file4):
self.insert_pre_data(file1, file2, file3)
triple_list = []
with codecs.open(file4, 'r') as f4:
content = f4.readlines()
for line in content:
triple = line.strip().split("\t")
if len(triple) != 3:
continue
head = int(entities2id[triple[0]])
relation = int(relations2id[triple[1]])
tail = int(entities2id[triple[2]])
triple_list.append([head, relation, tail])
self.test_triples = triple_list
def insert_traning_data(self, file1, file2, file3, file4):
self.insert_pre_data(file1, file2, file3)
with codecs.open(file4, 'r') as f5:
lines = f5.readlines()
for line in lines:
line = line.strip().split('\t')
self.train_loss = json.loads(line[0])
self.validation_loss = json.loads(line[1])
print(self.train_loss, self.validation_loss)
def training_run(self, epochs=300, batch_size=100, out_file_title = ''):
n_batches = int(len(self.triples) / batch_size)
valid_batch = int(len(self.valid_triples) / batch_size) + 1
print("batch size: ", n_batches, "valid_batch: " , valid_batch)
for epoch in range(epochs):
start = time.time()
self.loss = 0.0
self.valid_loss = 0.0
for batch in range(n_batches):
batch_samples = random.sample(self.triples, batch_size)
current = []
corrupted = []
for sample in batch_samples:
corrupted_sample = copy.deepcopy(sample)
pr = np.random.random(1)[0]
p = relation_tph[int(corrupted_sample[1])] / (
relation_tph[int(corrupted_sample[1])] + relation_hpt[int(corrupted_sample[1])])
if pr > p:
# change the head entity
corrupted_sample[0] = random.sample(self.entities, 1)[0]
while corrupted_sample[0] == sample[0]:
corrupted_sample[0] = random.sample(self.entities, 1)[0]
else:
# change the tail entity
corrupted_sample[2] = random.sample(self.entities, 1)[0]
while corrupted_sample[2] == sample[2]:
corrupted_sample[2] = random.sample(self.entities, 1)[0]
current.append(sample)
corrupted.append(corrupted_sample)
current = torch.from_numpy(np.array(current)).long()
corrupted = torch.from_numpy(np.array(corrupted)).long()
self.update_triple_embedding(current, corrupted)
for batch in range(valid_batch):
batch_samples = random.sample(self.valid_triples, batch_size)
current = []
corrupted = []
for sample in batch_samples:
corrupted_sample = copy.deepcopy(sample)
pr = np.random.random(1)[0]
p = relation_tph[int(corrupted_sample[1])] / (
relation_tph[int(corrupted_sample[1])] + relation_hpt[int(corrupted_sample[1])])
'''
这里关于p的说明 tph 表示每一个头结对应的平均尾节点数 hpt 表示每一个尾节点对应的平均头结点数
当tph > hpt 时 更倾向于替换头 反之则跟倾向于替换尾实体
举例说明
在一个知识图谱中,一共有10个实体 和n个关系,如果其中一个关系使两个头实体对应五个尾实体,
那么这些头实体的平均 tph为2.5,而这些尾实体的平均 hpt只有0.4,
则此时我们更倾向于替换头实体,
因为替换头实体才会有更高概率获得正假三元组,如果替换头实体,获得正假三元组的概率为 8/9 而替换尾实体获得正假三元组的概率只有 5/9
'''
if pr < p:
# change the head entity
corrupted_sample[0] = random.sample(self.entities, 1)[0]
while corrupted_sample[0] == sample[0]:
corrupted_sample[0] = random.sample(self.entities, 1)[0]
else:
# change the tail entity
corrupted_sample[2] = random.sample(self.entities, 1)[0]
while corrupted_sample[2] == sample[2]:
corrupted_sample[2] = random.sample(self.entities, 1)[0]
current.append(sample)
corrupted.append(corrupted_sample)
current = torch.from_numpy(np.array(current)).long()
corrupted = torch.from_numpy(np.array(corrupted)).long()
self.calculate_valid_loss(current, corrupted)
end = time.time()
mean_train_loss = self.loss / n_batches
mean_valid_loss = self.valid_loss / valid_batch
print("epoch: ", epoch, "cost time: %s" % (round((end - start), 3)))
print("Train loss: ", mean_train_loss, "Valid loss: ", mean_valid_loss)
self.train_loss.append(float(mean_train_loss))
self.validation_loss.append(float(mean_valid_loss))
# visualize the loss as the network trained
fig = plt.figure(figsize=(6, 4))
plt.plot(range(1, len(self.train_loss) + 1), self.train_loss, label='Train Loss')
plt.plot(range(1, len(self.validation_loss) + 1), self.validation_loss, label='Validation Loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.xlim(0, len(self.train_loss) + 1) # consistent scale
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.title(out_file_title + "TransR Training loss")
plt.show()
fig.savefig(out_file_title + 'TransR_loss_plot.png', bbox_inches='tight')
# .detach()的作用就是返回一个新的tensor,和原来tensor共享内存,但是这个张量会从计算途中分离出来,并且requires_grad=false
# 由于 能被grad的tensor不能直接使用.numpy(), 所以要是用。detach().numpy()
with codecs.open(out_file_title+"TransR_pytorch_entity_" + str(self.rel_dim) + "dim_batch" + str(batch_size), "w") as f1:
for i, e in enumerate(self.model.ent_embedding.weight):
f1.write(str(i) + "\t")
f1.write(str(e.cpu().detach().numpy().tolist()))
f1.write("\n")
with codecs.open(out_file_title+"TransR_pytorch_reltion_" + str(self.rel_dim) + "dim_batch" + str(batch_size), "w") as f2:
for i, e in enumerate(self.model.rel_embedding.weight):
f2.write(str(i) + "\t")
f2.write(str(e.cpu().detach().numpy().tolist()))
f2.write("\n")
with codecs.open(out_file_title+"TransR_pytorch_rel_matrix_"+ str(self.ent_dim) + "_"+ str(self.rel_dim) + "dim_batch" + str(batch_size), "w") as f3:
for i, e in enumerate(self.model.rel_matrix.weight):
f3.write(str(i) + "\t")
f3.write(str(e.cpu().detach().numpy().tolist()))
f3.write("\n")
with codecs.open(out_file_title + "TransR_loss_record.txt", "w") as f1:
f1.write(str(self.train_loss)+ "\t" + str(self.validation_loss))
def update_triple_embedding(self, correct_sample, corrupted_sample):
self.optim.zero_grad()
loss = self.model(correct_sample, corrupted_sample)
self.loss += loss
loss.backward()
self.optim.step()
def calculate_valid_loss(self, correct_sample, corrupted_sample):
loss = self.model(correct_sample, corrupted_sample)
self.valid_loss += loss
def test_run(self, filter=False):
self.filter = filter
hits = 0
rank_sum = 0
num = 0
for triple in self.test_triples:
start = time.time()
num += 1
print(num, triple)
rank_head_dict = {}
rank_tail_dict = {}
#
head_embedding = []
tail_embedding = []
norm_relation = []
hyper_relation = []
tamp = []
head_filter = []
tail_filter = []
if self.filter:
for tr in self.triples:
if tr[1] == triple[1] and tr[2] == triple[2] and tr[0] != triple[0]:
head_filter.append(tr)
if tr[0] == triple[0] and tr[1] == triple[1] and tr[2] != triple[2]:
tail_filter.append(tr)
for tr in self.test_triples:
if tr[1] == triple[1] and tr[2] == triple[2] and tr[0] != triple[0]:
head_filter.append(tr)
if tr[0] == triple[0] and tr[1] == triple[1] and tr[2] != triple[2]:
tail_filter.append(tr)
for tr in self.valid_triples:
if tr[1] == triple[1] and tr[2] == triple[2] and tr[0] != triple[0]:
head_filter.append(tr)
if tr[0] == triple[0] and tr[1] == triple[1] and tr[2] != triple[2]:
tail_filter.append(tr)
for i, entity in enumerate(self.entities):
head_triple = [entity, triple[1], triple[2]]
if self.filter:
if head_triple in head_filter:
continue
head_embedding.append(head_triple[0])
norm_relation.append(head_triple[1])
tail_embedding.append(head_triple[2])
tamp.append(tuple(head_triple))
head_embedding = torch.from_numpy(np.array(head_embedding)).long()
norm_relation = torch.from_numpy(np.array(norm_relation)).long()
tail_embedding = torch.from_numpy(np.array(tail_embedding)).long()
distance = self.model.test_distance(head_embedding, norm_relation, tail_embedding)
for i in range(len(tamp)):
rank_head_dict[tamp[i]] = distance[i]
head_embedding = []
tail_embedding = []
norm_relation = []
hyper_relation = []
tamp = []
for i, tail in enumerate(self.entities):
tail_triple = [triple[0], triple[1], tail]
if self.filter:
if tail_triple in tail_filter:
continue
head_embedding.append(tail_triple[0])
norm_relation.append(tail_triple[1])
tail_embedding.append(tail_triple[2])
tamp.append(tuple(tail_triple))
head_embedding = torch.from_numpy(np.array(head_embedding)).long()
norm_relation = torch.from_numpy(np.array(norm_relation)).long()
tail_embedding = torch.from_numpy(np.array(tail_embedding)).long()
distance = self.model.test_distance(head_embedding, norm_relation, tail_embedding)
for i in range(len(tamp)):
rank_tail_dict[tamp[i]] = distance[i]
# itemgetter 返回一个可调用对象,该对象可以使用操作__getitem__()方法从自身的操作中捕获item
# 使用itemgetter()从元组记录中取回特定的字段 搭配sorted可以将dictionary根据value进行排序
# sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
'''
sorted(iterable, cmp=None, key=None, reverse=False)
参数说明:
iterable -- 可迭代对象。
cmp -- 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。
key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
'''
rank_head_sorted = sorted(rank_head_dict.items(), key=operator.itemgetter(1), reverse=False)
rank_tail_sorted = sorted(rank_tail_dict.items(), key=operator.itemgetter(1), reverse=False)
# calculate the mean_rank and hit_10
# head data
i = 0
for i in range(len(rank_head_sorted)):
if triple[0] == rank_head_sorted[i][0][0]:
if i < 10:
hits += 1
rank_sum = rank_sum + i + 1
break
# tail rank
i = 0
for i in range(len(rank_tail_sorted)):
if triple[2] == rank_tail_sorted[i][0][2]:
if i < 10:
hits += 1
rank_sum = rank_sum + i + 1
break
end = time.time()
print("epoch: ", num, "cost time: %s" % (round((end - start), 3)), str(hits / (2 * num)),
str(rank_sum / (2 * num)))
self.hit_10 = hits / (2 * len(self.test_triples))
self.mean_rank = rank_sum / (2 * len(self.test_triples))
return self.hit_10, self.mean_rank
if __name__ == '__main__':
# file1 = "WN18\\wordnet-mlj12-train.txt"
# file2 = "WN18\\entity2id.txt"
# file3 = "WN18\\relation2id.txt"
# file4 = "WN18\\wordnet-mlj12-valid.txt"
file1 = "FB15k\\freebase_mtr100_mte100-train.txt"
file2 = "FB15k\\entity2id.txt"
file3 = "FB15k\\relation2id.txt"
file4 = "FB15k\\freebase_mtr100_mte100-valid.txt"
entity_set, relation_set, triple_list, valid_triple_list = dataloader(file1, file2, file3, file4)
# file5 = "WN18_torch_TransE_entity_50dim_batch4800"
# file6 = "WN18_torch_TransE_relation_50dim_batch4800"
#
# transR = TransR_Training(entity_set, relation_set, triple_list, ent_dim=50, rel_dim = 50, lr=0.001, margin=4.0, norm=1, C=1.0, valid_triples=valid_triple_list)
# transR.data_initialise("transE_entity_vector_50dim", "transE_relation_vector_50dim")
# # transR.data_initialise()
# transR.training_run(epochs=5, batch_size=1440, out_file_title="WN18_torch_")
file5 = "KB15k_torch_TransE_entity_50dim_batch9600"
file6 = "KB15k_torch_TransE_relation_50dim_batch9600"
#
transR = TransR_Training(entity_set, relation_set, triple_list, ent_dim=50, rel_dim=50, lr=0.001, margin=6.0,
norm=1, C=0.25, valid_triples=valid_triple_list)
# transR.data_initialise()
transR.data_initialise(file5, file6)
transR.training_run(epochs=100, batch_size=4800, out_file_title="FB15k_1torch_")
# file7 = "FB15k_1torch_TransR_pytorch_entity_50dim_batch4800"
# file8 = "FB15k_1torch_TransR_pytorch_reltion_50dim_batch4800"
# file9 = "FB15k_1torch_TransR_pytorch_rel_matrix_50_50dim_batch4800"
# file10 = "FB15k\\freebase_mtr100_mte100-test.txt"
# transR = TransR_Training(entity_set, relation_set, triple_list, ent_dim=50, rel_dim=50, lr=0.00001, margin=6.0,
# norm=1, C=0.25, valid_triples=valid_triple_list)
# transR.data_initialise()
# transR.insert_test_data(file7, file8, file9, file10)
# transR.test_run(filter = True)
# 关于叶节点的说明, 整个计算图中,只有叶节点的变量才能进行自动微分得到梯度,任何变量进行运算操作后,再把值付给他自己,这个变量就不是叶节点了,就不能进行自动微分