forked from HestiaSky/IMF-Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
233 lines (208 loc) · 8.6 KB
/
main.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
from models.model import *
from utils.data_util import load_data
from utils.data_loader import *
import numpy as np
import argparse
import torch
import time
def parse_args():
config_args = {
'lr': 0.0005,
'dropout_gat': 0.3,
'dropout': 0.3,
'cuda': 0,
'epochs_gat': 3000,
'epochs': 1000,
'weight_decay_gat': 1e-5,
'weight_decay': 0,
'seed': 10010,
'model': 'IMF',
'num-layers': 3,
'dim': 256,
'r_dim': 256,
'k_w': 10,
'k_h': 20,
'n_heads': 2,
'dataset': 'DB15K',
'pre_trained': 0,
'encoder': 0,
'image_features': 1,
'text_features': 1,
'patience': 5,
'eval_freq': 10,
'lr_reduce_freq': 500,
'gamma': 1.0,
'bias': 1,
'neg_num': 2,
'neg_num_gat': 2,
'alpha': 0.2,
'alpha_gat': 0.2,
'out_channels': 32,
'kernel_size': 3,
'batch_size': 256,
'save': 1
}
parser = argparse.ArgumentParser()
for param, val in config_args.items():
parser.add_argument(f"--{param}", action="append", default=val)
args = parser.parse_args()
return args
args = parse_args()
np.random.seed(args.seed)
torch.manual_seed(args.seed)
args.device = 'cuda:' + str(args.cuda) if int(args.cuda) >= 0 else 'cpu'
print(f'Using: {args.device}')
torch.cuda.set_device(args.cuda)
for k, v in list(vars(args).items()):
print(str(k) + ':' + str(v))
entity2id, relation2id, img_features, text_features, train_data, val_data, test_data = load_data(args.dataset)
print("Training data {:04d}".format(len(train_data[0])))
if args.model in ['ConvE', 'TuckER', 'Mutan', 'IMF']:
corpus = ConvECorpus(args, train_data, val_data, test_data, entity2id, relation2id)
else:
corpus = ConvKBCorpus(args, train_data, val_data, test_data, entity2id, relation2id)
if args.image_features:
args.img = F.normalize(torch.Tensor(img_features), p=2, dim=1)
if args.text_features:
args.desp = F.normalize(torch.Tensor(text_features), p=2, dim=1)
args.entity2id = entity2id
args.relation2id = relation2id
model_name = {
'OnlyConvKB': OnlyConvKB,
'IKRLConvKB': IKRLConvKB,
'ConvE': ConvE,
'TuckER': TuckER,
'Mutan': Mutan,
'IMF': IMF,
'IKRL': IKRL,
'MKGC': MKGC,
}
def train_encoder(args):
model = GAT(args)
print(str(model))
optimizer = torch.optim.Adam(
params=model.parameters(), lr=args.lr, weight_decay=args.weight_decay_gat)
lr_scheduler = torch.optim.lr_scheduler.StepLR(
optimizer, step_size=500, gamma=float(args.gamma))
tot_params = sum([np.prod(p.size()) for p in model.parameters()])
print(f'Total number of parameters: {tot_params}')
if args.cuda is not None and int(args.cuda) >= 0:
model = model.to(args.device)
# Train Model
t_total = time.time()
corpus.batch_size = len(corpus.train_triples)
corpus.neg_num = 2
for epoch in range(args.epochs_gat):
model.train()
t = time.time()
np.random.shuffle(corpus.train_triples)
train_indices, train_values = corpus.get_batch(0)
train_indices = torch.LongTensor(train_indices)
if args.cuda is not None and int(args.cuda) >= 0:
train_indices = train_indices.to(args.device)
optimizer.zero_grad()
entity_embed, relation_embed = model.forward(corpus.train_adj_matrix, train_indices)
loss = model.loss_func(train_indices, entity_embed, relation_embed)
loss.backward()
optimizer.step()
lr_scheduler.step()
print("Epoch {} , epoch_time {}".format(epoch, time.time() - t))
if args.save:
torch.save(model.state_dict(), f'./checkpoint/{args.dataset}/GAT_{epoch}.pth')
print('Saved model!')
print("GAT training finished! Total time is {}".format(time.time()-t_total))
def train_decoder(args):
if args.encoder:
model_gat = GAT(args)
model = model_name[args.model](args)
print(str(model))
optimizer = torch.optim.Adam(
params=model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, args.gamma)
tot_params = sum([np.prod(p.size()) for p in model.parameters()])
print(f'Total number of parameters: {tot_params}')
if args.cuda is not None and int(args.cuda) >= 0:
if args.encoder:
model_gat = model_gat.to(args.device)
model_gat.load_state_dict(
torch.load('./checkpoint/{}/GAT_{}.pth'.format(args.dataset, args.epochs_gat - 1)), strict=False)
pickle.dump(model_gat.final_entity_embeddings.detach().cpu().numpy(),
open('datasets/' + args.dataset + '/gat_entity_vec.pkl', 'wb'))
pickle.dump(model_gat.final_relation_embeddings.detach().cpu().numpy(),
open('datasets/' + args.dataset + '/gat_relation_vec.pkl', 'wb'))
model = model.to(args.device)
# Train Model
t_total = time.time()
counter = 0
best_val_metrics = model.init_metric_dict()
best_test_metrics = model.init_metric_dict()
corpus.batch_size = args.batch_size
corpus.neg_num = args.neg_num
for epoch in range(args.epochs):
model.train()
epoch_loss = []
t = time.time()
corpus.shuffle()
for batch_num in range(corpus.max_batch_num):
optimizer.zero_grad()
#train_indices, train_values, lookup = corpus.get_batch(batch_num)
train_indices, train_values = corpus.get_batch(batch_num)
train_indices = torch.LongTensor(train_indices)
if args.cuda is not None and int(args.cuda) >= 0:
train_indices = train_indices.to(args.device)
train_values = train_values.to(args.device)
#lookup = lookup.to(args.device)
#output = model.forward(train_indices, lookup)
#output = model.forward(train_indices)
#loss = model.loss_func(output, train_values)
loss = model.forward(train_indices)
loss.backward()
optimizer.step()
epoch_loss.append(loss.data.item())
lr_scheduler.step()
if (epoch + 1) % args.eval_freq == 0:
print("Epoch {:04d} , average loss {:.4f} , epoch_time {:.4f}\n".format(
epoch + 1, sum(epoch_loss) / len(epoch_loss), time.time() - t))
model.eval()
with torch.no_grad():
val_metrics = corpus.get_validation_pred(model, 'test')
if val_metrics['Mean Reciprocal Rank'] > best_test_metrics['Mean Reciprocal Rank']:
best_test_metrics['Mean Reciprocal Rank'] = val_metrics['Mean Reciprocal Rank']
if val_metrics['Mean Rank'] < best_test_metrics['Mean Rank']:
best_test_metrics['Mean Rank'] = val_metrics['Mean Rank']
if val_metrics['Hits@1'] > best_test_metrics['Hits@1']:
best_test_metrics['Hits@1'] = val_metrics['Hits@1']
if val_metrics['Hits@3'] > best_test_metrics['Hits@3']:
best_test_metrics['Hits@3'] = val_metrics['Hits@3']
if val_metrics['Hits@10'] > best_test_metrics['Hits@10']:
best_test_metrics['Hits@10'] = val_metrics['Hits@10']
if val_metrics['Hits@100'] > best_test_metrics['Hits@100']:
best_test_metrics['Hits@100'] = val_metrics['Hits@100']
print(' '.join(['Epoch: {:04d}'.format(epoch + 1),
model.format_metrics(val_metrics, 'test')]))
'''if model.has_improved(best_val_metrics, val_metrics):
best_val_metrics = val_metrics
with torch.no_grad():
best_test_metrics = corpus.get_validation_pred(model, 'test')
counter = 0
else:
counter += 1
if counter >= args.patience:
print("Early stopping")
break'''
print('Optimization Finished!')
print('Total time elapsed: {:.4f}s'.format(time.time() - t_total))
if not best_test_metrics:
model.eval()
with torch.no_grad():
best_test_metrics = corpus.get_validation_pred(model, 'test')
print(' '.join(['Val set results:',
model.format_metrics(best_val_metrics, 'val')]))
print(' '.join(['Test set results:',
model.format_metrics(best_test_metrics, 'test')]))
if args.save:
torch.save(model.state_dict(), f'./checkpoint/{args.dataset}/{args.model}.pth')
print('Saved model!')
if __name__ == '__main__':
#train_encoder(args)
train_decoder(args)