-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
305 lines (268 loc) · 11.7 KB
/
Copy pathmain.py
File metadata and controls
305 lines (268 loc) · 11.7 KB
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
import shutil
from datetime import datetime
import logging
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
from src.utils import *
from src.parse_args import args
from src.data_load.KnowledgeGraph import KnowledgeGraph
from src.model.LoraKGE_Layers import TransE as LoraKGE_Layers
from src.train import *
from src.test import *
class Instructor():
""" The instructor of the model """
def __init__(self, args) -> None:
self.args = args
""" 1. Prepare for path, logger and device """
self.prepare()
""" 2. Load data """
self.kg = KnowledgeGraph(args)
""" 3. Create models and optimizer """
self.model, self.optimizer = self.create_model()
self.args.logger.info(self.args)
def create_model(self):
""" Create KGE model and optimizer """
if self.args.model_name == "LoraKGE_Layers":
model = LoraKGE_Layers(self.args, self.kg)
else:
model = LoraKGE_Layers(self.args, self.kg)
model.to(self.args.device)
optimizer = torch.optim.Adam(model.parameters(), lr=float(self.args.learning_rate), weight_decay=self.args.l2)
return model, optimizer
def reset_model(self, model=False, optimizer=False):
"""
Reset model or optimizer
:param model: If True: reset the model and optimizer
:param optimizer: If True: reset the optimizer
"""
if model:
self.model, self.optimizer = self.create_model()
if optimizer:
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=float(self.args.learning_rate), weight_decay=self.args.l2)
def prepare(self):
""" Set data path """
if not os.path.exists(args.data_path):
os.mkdir(args.data_path)
self.args.data_path = args.data_path + args.dataset + "/"
""" Set save path """
self.args.save_path = args.save_path + args.dataset
if os.path.exists(args.save_path):
shutil.rmtree(args.save_path, True)
if not os.path.exists(args.save_path):
os.mkdir(args.save_path)
if self.args.note != '':
self.args.save_path += self.args.note
if os.path.exists(args.save_path):
shutil.rmtree(args.save_path, True)
if not os.path.exists(args.save_path):
os.mkdir(args.save_path)
""" Set log path """
if not os.path.exists(args.log_path):
os.mkdir(args.log_path)
self.args.log_path = args.log_path + datetime.now().strftime("%Y%m%d%H%M%S/")
if not os.path.exists(args.log_path):
os.mkdir(args.log_path)
self.args.log_path = args.log_path + args.dataset
if self.args.note != "":
self.args.log_path += self.args.note
""" Set logger """
logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
console_formatter = logging.Formatter('%(asctime)-8s: %(message)s')
logging_file_name = f'{args.log_path}.log'
file_handler = logging.FileHandler(logging_file_name)
file_handler.setFormatter(formatter)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.formatter = console_formatter
logger.addHandler(file_handler)
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
self.args.logger = logger
""" Set device """
torch.cuda.set_device(int(args.gpu))
_ = torch.tensor([1]).cuda()
self.args.device = _.device
def next_snapshot_setting(self):
""" Prepare for next snapshot """
self.model.switch_snapshot()
def run(self):
""" Run the instructor of the model. The training process on all snapshots """
report_results = PrettyTable()
report_results.field_names = ['Snapshot', 'Time', 'Whole_MRR', 'Whole_Hits@1', 'Whole_Hits@3', 'Whole_Hits@10']
test_results = []
training_times = []
BWT = [] # h(n, i) - h(i, i)
FWT = [] # h(i- 1, i)
first_learning_res = []
""" training process """
for ss_id in range(int(self.args.snapshot_num)):
best_checkpoint = os.path.join(
self.args.save_path, f'{str(ss_id - 1)}model_best.tar'
)
self.args.snapshot = ss_id
self.args.snapshot_test = ss_id
self.args.snapshot_valid = ss_id
""" preprocess before training on a snapshot """
self.model.pre_snapshot()
if ss_id > 0:
self.args.test_FWT = True
res_before = self.test()
FWT.append(res_before['mrr'])
self.args.test_FWT = False
training_time = self.train()
""" prepare result table """
test_res = PrettyTable()
test_res.field_names = [
f'Snapshot:{str(ss_id)}',
'MRR',
'Hits@1',
'Hits@3',
'Hits@5',
'Hits@10',
]
best_checkpoint = os.path.join(
self.args.save_path, f'{str(ss_id)}model_best.tar'
)
self.load_checkpoint(best_checkpoint)
self.model.snapshot_post_processing()
reses = []
for test_ss_id in range(ss_id + 1):
self.args.snapshot_test = test_ss_id
res = self.test()
if test_ss_id == ss_id:
first_learning_res.append(res['mrr'])
test_res.add_row([
test_ss_id, res['mrr'], res['hits1'], res['hits3'], res['hits5'], res['hits10']
])
reses.append(res)
if ss_id == self.args.snapshot_num - 1:
BWT.extend(
reses[iid]['mrr'] - first_learning_res[iid]
for iid in range(self.args.snapshot_num - 1)
)
self.args.logger.info(f"\n{test_res}")
test_results.append(test_res)
""" record report results """
whole_mrr, whole_hits1, whole_hits3, whole_hits10 = self.get_report_results(reses)
report_results.add_row([ss_id, training_time, whole_mrr, whole_hits1, whole_hits3, whole_hits10])
training_times.append(training_time)
if self.args.snapshot < int(self.args.snapshot_num) - 1:
self.next_snapshot_setting()
self.reset_model(optimizer=True)
self.args.logger.info(f'Final Result:\n{test_results}')
self.args.logger.info(f'Report Result:\n{report_results}')
self.args.logger.info(f'Sum_Training_Time:{sum(training_times)}')
self.args.logger.info(f'Every_Training_Time:{training_times}')
self.args.logger.info(
f'Forward transfer: {sum(FWT) / len(FWT)} Backward transfer: {sum(BWT) / len(BWT)}'
)
def get_report_results(self, results):
mrrs, hits1s, hits3s, hits10s, num_test = [], [], [], [], []
for idx, result in enumerate(results):
mrrs.append(result['mrr'])
hits1s.append(result['hits1'])
hits3s.append(result['hits3'])
hits10s.append(result['hits10'])
num_test.append(len(self.kg.snapshots[idx].test))
whole_mrr = sum(
mrr * num_test[i] for i, mrr in enumerate(mrrs)
) / sum(num_test)
whole_hits1 = sum(
hits1 * num_test[i] for i, hits1 in enumerate(hits1s)
) / sum(num_test)
whole_hits3 = sum(
hits3 * num_test[i] for i, hits3 in enumerate(hits3s)
) / sum(num_test)
whole_hits10 = sum(
hits10 * num_test[i] for i, hits10 in enumerate(hits10s)
) / sum(num_test)
return round(whole_mrr, 3), round(whole_hits1, 3), round(whole_hits3, 3), round(whole_hits10, 3)
def train(self):
""" Training process, return training time """
start_time = time.time()
print("Start training =============================")
self.best_valid = 0.0
self.stop_epoch = 0
trainer = Trainer(self.args, self.kg, self.model, self.optimizer)
""" Trainign iteration """
for epoch in range(int(self.args.epoch_num)):
self.args.epoch = epoch
""" training """
loss, valid_res = trainer.run_epoch()
""" early stop """
if self.args.debug:
if epoch > 0:
break
if valid_res[self.args.valid_metrics] > self.best_valid:
self.best_valid = valid_res[self.args.valid_metrics]
self.stop_epoch = 0
if self.args.snapshot == 0:
self.save_model(is_best=True, lora=False)
else:
self.save_model(is_best=True, lora=True)
else:
self.stop_epoch += 1
if self.args.snapshot == 0:
self.save_model(lora=False)
else:
self.save_model(lora=True)
if self.stop_epoch >= self.args.patience:
self.args.logger.info(
f'Early Stopping! Snapshot:{self.args.snapshot} Epoch: {epoch} Best Results: {round(self.best_valid * 100, 3)}'
)
break
""" logging """
if epoch % 1 == 0:
self.args.logger.info(
f"Snapshot:{self.args.snapshot}\tEpoch:{epoch}\tLoss:{round(loss, 3)}\tMRR:{round(valid_res['mrr'] * 100, 3)}\tHits@10:{round(valid_res['hits10'] * 100, 3)}\tBest:{round(self.best_valid * 100, 3)}"
)
end_time = time.time()
return end_time - start_time
def test(self):
tester = Tester(self.args, self.kg, self.model)
return tester.test()
def save_model(self, is_best=False, lora=False):
if lora == False:
checkpoint_dict = {'state_dict': self.model.state_dict()}
checkpoint_dict['epoch_id'] = self.args.epoch
out_tar = os.path.join(
self.args.save_path,
f'{str(self.args.snapshot)}checkpoint-{self.args.epoch}.tar',
)
torch.save(checkpoint_dict, out_tar)
if is_best:
best_path = os.path.join(
self.args.save_path, f'{str(self.args.snapshot)}model_best.tar'
)
shutil.copyfile(out_tar, best_path)
else:
out_tar = os.path.join(
self.args.save_path,
f'{str(self.args.snapshot)}checkpoint-{self.args.epoch}.tar',
)
torch.save(loralib.lora_state_dict(self.model), out_tar)
if is_best:
best_path = os.path.join(
self.args.save_path, f'{str(self.args.snapshot)}model_best.tar'
)
shutil.copyfile(out_tar, best_path)
def load_checkpoint(self, input_file):
if self.args.snapshot == 0:
if os.path.isfile(input_file):
logging.info(f"=> loading checkpoint \'{input_file}\'")
checkpoint = torch.load(input_file, map_location=f"cuda:{self.args.gpu}")
self.model.load_state_dict(checkpoint['state_dict'])
else:
logging.info(f'=> no checking found at \'{input_file}\'')
else:
if os.path.isfile(input_file):
logging.info(f"=> loading checkpoint \'{input_file}\'")
checkpoint = torch.load(input_file, map_location=f"cuda:{self.args.gpu}")
self.model.load_state_dict(checkpoint, strict=False)
else:
logging.info(f'=> no checking found at \'{input_file}\'')
""" Main function """
if __name__ == "__main__":
set_seeds(args.random_seed)
ins = Instructor(args)
ins.run()