-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.py
159 lines (130 loc) · 5.38 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
import os
import random
import torch
import numpy as np
from time import time
from tqdm import tqdm
from copy import deepcopy
import logging
from prettytable import PrettyTable
from utils.parser import parse_args
from utils.data_loader import load_data
from utils.evaluate import test
from utils.helper import early_stopping
n_users = 0
n_items = 0
def get_feed_dict(train_entity_pairs, train_pos_set, start, end, n_negs=1):
def sampling(user_item, train_set, n):
neg_items = []
for user, _ in user_item.cpu().numpy():
user = int(user)
negitems = []
for i in range(n): # sample n times
while True:
negitem = random.choice(range(n_items))
if negitem not in train_set[user]:
break
negitems.append(negitem)
neg_items.append(negitems)
return neg_items
feed_dict = {}
entity_pairs = train_entity_pairs[start:end]
feed_dict['users'] = entity_pairs[:, 0]
feed_dict['pos_items'] = entity_pairs[:, 1]
feed_dict['neg_items'] = torch.LongTensor(sampling(entity_pairs,
train_pos_set,
n_negs*K)).to(device)
return feed_dict
if __name__ == '__main__':
"""fix the random seed"""
seed = 2020
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
"""read args"""
global args, device
args = parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
device = torch.device("cuda:0") if args.cuda else torch.device("cpu")
"""build dataset"""
train_cf, user_dict, n_params, norm_mat = load_data(args)
train_cf_size = len(train_cf)
train_cf = torch.LongTensor(np.array([[cf[0], cf[1]] for cf in train_cf], np.int32))
n_users = n_params['n_users']
n_items = n_params['n_items']
n_negs = args.n_negs
K = args.K
"""define model"""
from modules.LightGCN import LightGCN
from modules.NGCF import NGCF
if args.gnn == 'lightgcn':
model = LightGCN(n_params, args, norm_mat).to(device)
else:
model = NGCF(n_params, args, norm_mat).to(device)
"""define optimizer"""
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
cur_best_pre_0 = 0
stopping_step = 0
should_stop = False
print("start training ...")
for epoch in range(args.epoch):
# shuffle training data
train_cf_ = train_cf
index = np.arange(len(train_cf_))
np.random.shuffle(index)
train_cf_ = train_cf_[index].to(device)
"""training"""
model.train()
loss, s = 0, 0
hits = 0
train_s_t = time()
while s + args.batch_size <= len(train_cf):
batch = get_feed_dict(train_cf_,
user_dict['train_user_set'],
s, s + args.batch_size,
n_negs)
batch_loss, _, _ = model(batch)
optimizer.zero_grad()
batch_loss.backward()
optimizer.step()
loss += batch_loss
s += args.batch_size
train_e_t = time()
if epoch % 5 == 0:
"""testing"""
train_res = PrettyTable()
train_res.field_names = ["Epoch", "training time(s)", "tesing time(s)", "Loss", "recall", "ndcg", "precision", "hit_ratio"]
model.eval()
test_s_t = time()
test_ret = test(model, user_dict, n_params, mode='test')
test_e_t = time()
train_res.add_row(
[epoch, train_e_t - train_s_t, test_e_t - test_s_t, loss.item(), test_ret['recall'], test_ret['ndcg'],
test_ret['precision'], test_ret['hit_ratio']])
if user_dict['valid_user_set'] is None:
valid_ret = test_ret
else:
test_s_t = time()
valid_ret = test(model, user_dict, n_params, mode='valid')
test_e_t = time()
train_res.add_row(
[epoch, train_e_t - train_s_t, test_e_t - test_s_t, loss.item(), valid_ret['recall'], valid_ret['ndcg'],
valid_ret['precision'], valid_ret['hit_ratio']])
print(train_res)
# *********************************************************
# early stopping when cur_best_pre_0 is decreasing for 10 successive steps.
cur_best_pre_0, stopping_step, should_stop = early_stopping(valid_ret['recall'][0], cur_best_pre_0,
stopping_step, expected_order='acc',
flag_step=10)
if should_stop:
break
"""save weight"""
if valid_ret['recall'][0] == cur_best_pre_0 and args.save:
torch.save(model.state_dict(), args.out_dir + 'model_' + '.ckpt')
else:
# logging.info('training loss at epoch %d: %f' % (epoch, loss.item()))
print('using time %.4fs, training loss at epoch %d: %.4f' % (train_e_t - train_s_t, epoch, loss.item()))
print('early stopping at %d, recall@20:%.4f' % (epoch, cur_best_pre_0))