-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_rotate3D_WN18RR.py
147 lines (130 loc) · 5.88 KB
/
train_rotate3D_WN18RR.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
import openke
from openke.config import Trainer, Tester
from openke.module.model import Rotate3D
from openke.module.loss import SigmoidLoss
from openke.module.strategy import NegativeSampling
from openke.data import TrainDataLoader, TestDataLoader, FixedDataLoader, FixedTestDataLoader
import openke
from openke.config import Trainer, Tester, TesterForRetrievalTest
import pdb
import torch
import os.path as osp
import argparse
def main(args):
# dataloader for training
train_dataloader = TrainDataLoader(
in_path = "./benchmarks/WN18RR/",
batch_size = 500, #原来是2000
threads = 8,
sampling_mode = "normal",
bern_flag = 0,
filter_flag = 1,
neg_ent = 64,
neg_rel = 0
)
# define the model
rotate = Rotate3D(
ent_tot = train_dataloader.get_ent_tot(),
rel_tot = train_dataloader.get_rel_tot(),
dim = 1024,
margin = 6.0,
epsilon = 2.0,
)
# define the loss function
model = NegativeSampling(
model = rotate,
loss = SigmoidLoss(adv_temperature = 2),
batch_size = train_dataloader.get_batch_size(),
regul_rate = 0.0
)
if not args.test:
# train the model
trainer = Trainer(model = model, data_loader = train_dataloader, train_times = 300, alpha = 2e-5, use_gpu = True, opt_method = "adam")
trainer.run()
rotate.save_checkpoint('./checkpoint/Rotate3D_WN18RR.ckpt')
dataset_name = 'WN18RR'
# test the model
rotate.load_checkpoint('./checkpoint/Rotate3D_WN18RR_epoch300.ckpt')
# get number of entities
entity2id_file = open("./benchmarks/" + dataset_name + "/entity2id.txt", 'r', encoding='utf-8')
nentities = (int)(entity2id_file.readline())
entity2id_file.close()
specific_test_dataloader_all = FixedTestDataLoader("./benchmarks/" + dataset_name + "/test2id.txt", "link", nentities)
specific_test_dataloader_11 = FixedTestDataLoader("./benchmarks/" + dataset_name + "/1-1.txt", "link", nentities)
specific_test_dataloader_1n = FixedTestDataLoader("./benchmarks/" + dataset_name + "/1-n.txt", "link", nentities)
specific_test_dataloader_n1 = FixedTestDataLoader("./benchmarks/" + dataset_name + "/n-1.txt", "link", nentities)
specific_test_dataloader_nn = FixedTestDataLoader("./benchmarks/" + dataset_name + "/n-n.txt", "link", nentities)
# load the dataset
actual_head = {} # actual_head[(r, t)] is the actual head of ?->r->t
actual_tail = {} # actual_tail[(h, r)] is the actual tail of h->r->?
relation2id = open(osp.join("benchmarks", dataset_name, "relation2id.txt"), "r",encoding='utf-8')
num_relations = int(relation2id.readline().strip())
entity2id = open(osp.join("benchmarks", dataset_name, "entity2id.txt"), "r",encoding='utf-8')
num_entities = int(entity2id.readline().strip())
relation2id.close()
entity2id.close()
triple = open(osp.join("benchmarks", dataset_name, "train2id.txt"), "r",encoding='utf-8')
tot = (int)(triple.readline())
for i in range(tot):
content = triple.readline()
h, t, r = content.strip().split()
h = int(h)
t = int(t)
r = int(r)
if not (h, r) in actual_tail:
actual_tail[(h, r)] = []
if not (r, t) in actual_head:
actual_head[(r, t)] = []
actual_tail[(h, r)].append(t)
actual_head[(r, t)].append(h)
triple.close()
valid = open(osp.join("benchmarks", dataset_name, "valid2id.txt"), "r",encoding='utf-8')
tot = (int)(valid.readline())
for i in range(tot):
content = valid.readline()
h, t, r = content.strip().split()
h = int(h)
t = int(t)
r = int(r)
if not (h, r) in actual_tail:
actual_tail[(h, r)] = []
if not (r, t) in actual_head:
actual_head[(r, t)] = []
actual_tail[(h, r)].append(t)
actual_head[(r, t)].append(h)
valid.close()
test = open(osp.join("benchmarks", dataset_name, "test2id.txt"), "r",encoding='utf-8')
tot = (int)(test.readline())
for i in range(tot):
content = test.readline()
h, t, r = content.strip().split()
h = int(h)
t = int(t)
r = int(r)
if not (h, r) in actual_tail:
actual_tail[(h, r)] = []
if not (r, t) in actual_head:
actual_head[(r, t)] = []
actual_tail[(h, r)].append(t)
actual_head[(r, t)].append(h)
test.close()
tester_all = TesterForRetrievalTest(model = rotate, data_loader = specific_test_dataloader_all, use_gpu = True, actual_head = actual_head, actual_tail = actual_tail)
tester_11 = TesterForRetrievalTest(model = rotate, data_loader = specific_test_dataloader_11, use_gpu = True, actual_head = actual_head, actual_tail = actual_tail)
tester_1n = TesterForRetrievalTest(model = rotate, data_loader = specific_test_dataloader_1n, use_gpu = True, actual_head = actual_head, actual_tail = actual_tail)
tester_n1 = TesterForRetrievalTest(model = rotate, data_loader = specific_test_dataloader_n1, use_gpu = True, actual_head = actual_head, actual_tail = actual_tail)
tester_nn = TesterForRetrievalTest(model = rotate, data_loader = specific_test_dataloader_nn, use_gpu = True, actual_head = actual_head, actual_tail = actual_tail)
print("Test on all relationship")
tester_all.run_link_prediction(type_constrain = False)
print("Test on 1-1 relationship")
tester_11.run_link_prediction(type_constrain = False)
print("Test on 1-n relationship")
tester_1n.run_link_prediction(type_constrain = False)
print("Test on n-1 relationship")
tester_n1.run_link_prediction(type_constrain = False)
print("Test on n-n relationship")
tester_nn.run_link_prediction(type_constrain = False)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--test', action='store_true')
args = parser.parse_args()
main(args)