-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (65 loc) · 2.46 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
import world
import utils
from world import cprint
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import torch
import numpy as np
from tensorboardX import SummaryWriter
import time
import Procedure
from os.path import join
# ==============================
utils.set_seed(world.seed)
print(">>SEED:", world.seed)
# ==============================
import register
import tracemalloc
from register import dataset
from parse import parse_args
args = parse_args()
Recmodel = register.MODELS[world.model_name](world.config, dataset)
Recmodel = Recmodel.to(world.device)
bpr = utils.BPRLoss(Recmodel, world.config)
weight_file = utils.getFileName()
print(f"load and save to {weight_file}")
if world.LOAD:
try:
Recmodel.load_state_dict(torch.load(weight_file,map_location=torch.device('cpu')))
world.cprint(f"loaded model weights from {weight_file}")
except FileNotFoundError:
print(f"{weight_file} not exists, start from beginning")
Neg_k = 1
# init tensorboard
if world.tensorboard:
w : SummaryWriter = SummaryWriter(
join(world.BOARD_PATH, time.strftime("%m-%d-%Hh%Mm%Ss-") + "-" + world.comment)
)
else:
w = None
world.cprint("not enable tensorflowboard")
try:
best_recall = 0
tracemalloc.start(25)
for epoch in range(world.TRAIN_epochs):
start = time.time()
if epoch % 10 == 0:
cprint("[TEST]")
results = Procedure.Test(dataset, Recmodel, epoch, w, world.config['multicore'])
if args.model in {'mf', 'ours-one'} and results['recall'] > best_recall:
best_recall = results['recall']
embed_user = Recmodel.embedding_user.weight.detach().cpu().numpy()
embed_item = Recmodel.embedding_item.weight.detach().cpu().numpy()
np.save('lgn_embed_user_'+args.dataset+'.npy', embed_user)
np.save('lgn_embed_item_'+args.dataset+'.npy', embed_item)
T1 = time.time()
output_information = Procedure.BPR_train_original(dataset, Recmodel, bpr, epoch, neg_k=Neg_k,w=w)
T2 = time.time()
print(f'EPOCH[{epoch+1}/{world.TRAIN_epochs}] {output_information}')
torch.save(Recmodel.state_dict(), weight_file)
size, peak = tracemalloc.get_traced_memory()
print("Total time: ", (T2-T1)*1000, "ms")
print('memory blocks:{:>10.4f} KiB'.format(peak / 1024))
finally:
if world.tensorboard:
w.close()