-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
228 lines (199 loc) · 8.79 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
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 modules.MF import Matrix_Factorization
from utils.parser import parse_args
from utils.data_loader import load_data
from utils.evaluate import test
from utils.helper import early_stopping
import optuna
import datetime
from modules.MCAP import MCAP
from modules.LightGCN import LightGCN
from modules.ApeGNN_APPNP import APPNP
from modules.ApeGNN_HT import HeatKernel
n_users = 0
n_items = 0
def get_feed_dict(train_entity_pairs, train_pos_set, start, end, n_negs=1, K=1, n_items=0):
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
def opt_objective(trial, args, train_cf, user_dict, n_params, norm_mat, deg, outdeg, u2u):
valid_res_list = []
args.dim = trial.suggest_int('dim', 64, 512)
args.l2 = trial.suggest_float('l2', 0, 1)
args.context_hops = trial.suggest_int('context_hops', 1, 6)
print(args)
valid_best_result = main(args, args.seed, train_cf, user_dict, n_params, norm_mat, deg, outdeg, u2u)
valid_res_list.append(valid_best_result)
return np.mean(valid_res_list)
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def main(args, run, train_cf, user_dict, n_params, norm_mat, deg, outdeg, u2u):
"""define model"""
if args.gnn == "MCAP":
model = MCAP(n_params, args, norm_mat, u2u).to(device)
elif args.gnn == "LightGCN":
model = LightGCN(n_params, args, norm_mat).to(device)
elif args.gnn == 'MF':
model = Matrix_Factorization(n_params, args).to(device)
elif args.gnn == 'ApeGNN_APPNP':
model = APPNP(n_params, args, norm_mat, deg).to(device)
elif args.gnn == 'ApeGNN_HT':
model = HeatKernel(n_params, args, norm_mat, deg).to(device)
"""define optimizer"""
optimizer = torch.optim.Adam([{'params': model.parameters(),
'lr': args.lr}])
n_items = n_params['n_items']
cur_best_pre_0 = 0
stopping_step = 0
best_epoch = 0
print("start training ...")
hyper = {"dim": args.dim, "l2": args.l2, "hops": args.context_hops}
print("Start hyper parameters: ", hyper)
test_results = {}
epoch2model = {}
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,
args.n_negs,
args.K,
n_items)
batch_loss, _, _ = model(batch)
optimizer.zero_grad()
batch_loss.backward()
optimizer.step()
loss += batch_loss
s += args.batch_size
train_e_t = time()
print('loss:', round(loss.item(), 2), "time: ", round(train_e_t - train_s_t, 2), 's')
if epoch % args.step == 0:
"""testing"""
train_res = PrettyTable()
train_res.field_names = ["Phase", "Epoch", "training time(s)", "tesing time(s)", "Loss", "recall", "ndcg",
"hit_ratio", "precision"]
model.eval()
test_s_t = time()
test_ret, user_result, deg_recall, deg_recall_mean = test(model, user_dict, n_params, deg, mode='test')
test_e_t = time()
train_res.add_row(
["Test", epoch, round(train_e_t - train_s_t, 2), round(test_e_t - test_s_t, 2), round(loss.item(), 2),
test_ret['recall'],
test_ret['ndcg'],
test_ret['hit_ratio'],
test_ret['precision']])
test_results[epoch] = ["Test", epoch, round(train_e_t - train_s_t, 2), round(test_e_t - test_s_t, 2), round(loss.item(), 2),
test_ret['recall'],
test_ret['ndcg'],
test_ret['hit_ratio'],
test_ret['precision']]
"""valid"""
if user_dict['valid_user_set'] is None:
valid_ret = test_ret
else:
test_s_t = time()
valid_ret, user_result, deg_recall, deg_recall_mean = test(model, user_dict, n_params, deg, mode='valid')
test_e_t = time()
train_res.add_row(
["Valid", epoch, round(train_e_t - train_s_t, 2), round(test_e_t - test_s_t, 2), round(loss.item(), 2),
valid_ret['recall'],
valid_ret['ndcg'],
valid_ret['hit_ratio'],
valid_ret['precision']])
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'][2], cur_best_pre_0,
stopping_step, expected_order='acc',
flag_step=args.early_stop)
if valid_ret['recall'][2] == cur_best_pre_0:
best_epoch = epoch
if should_stop:
break
epoch2model[epoch] = model
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, best_epoch at %d' % (epoch, cur_best_pre_0, best_epoch))
best_pretty = PrettyTable()
best_pretty.field_names = ["Phase", "Epoch", "training time(s)", "tesing time(s)", "Loss", "recall", "ndcg",
"hit_ratio", "precision"]
best_pretty.add_row(test_results[best_epoch])
print(best_pretty)
"""save weight"""
if args.save:
current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
print(current_time)
torch.save(epoch2model[best_epoch].state_dict(), args.out_dir + f'{args.dataset}_{args.dim}_{args.context_hops}_{args.l2}_{current_time}_' + args.gnn + '.ckpt')
print("Seed:", run)
print("End hyper parameters: ", hyper)
print(f"Best valid_ret['recall']: ", cur_best_pre_0)
return cur_best_pre_0
if __name__ == '__main__':
"""read args"""
s = datetime.datetime.now()
print("time of start: ", s)
global args, device
args = parse_args()
set_seed(args.seed)
if args.gpu_id != -1 and torch.cuda.is_available():
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
device = torch.device("cuda:{}".format(args.gpu_id))
else:
device = torch.device("cpu")
"""build dataset"""
train_cf, user_dict, n_params, norm_mat, deg, outdeg, u2u = 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))
trials = 1
search_space = {'dim': [args.dim], 'context_hops': [args.context_hops], 'l2': [args.l2]}
print("search_space: ", search_space)
print("trials: ", trials)
study = optuna.create_study(sampler=optuna.samplers.GridSampler(search_space))
study.optimize(lambda trial: opt_objective(trial, args, train_cf, user_dict, n_params, norm_mat, deg, outdeg, u2u), n_trials=trials)
e = datetime.datetime.now()
print(study.best_trial.params)
print("time of end: ", e)
print("finished all")