-
Notifications
You must be signed in to change notification settings - Fork 9
/
Main.py
266 lines (219 loc) · 8.39 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
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
import torch
import Utils.TimeLogger as logger
from Utils.TimeLogger import log
from Params import args
from Model import Model, Denoise, GaussianDiffusion
from DataHandler import DataHandler
import numpy as np
import pickle
from Utils.Utils import *
import os
import random
class Coach:
def __init__(self, handler):
self.handler = handler
print('USER', args.user, 'ITEM', args.item)
print('NUM OF INTERACTIONS', self.handler.trnLoader.dataset.__len__())
self.metrics = dict()
mets = ['Loss', 'preLoss', 'Recall', 'NDCG']
for met in mets:
self.metrics['Train' + met] = list()
self.metrics['Test' + met] = list()
def makePrint(self, name, ep, reses, save):
ret = 'Epoch %d/%d, %s: ' % (ep, args.epoch, name)
for metric in reses:
val = reses[metric]
ret += '%s = %.4f, ' % (metric, val)
tem = name + metric
if save and tem in self.metrics:
self.metrics[tem].append(val)
ret = ret[:-2] + ' '
return ret
def run(self):
self.prepareModel()
log('Model Prepared')
log('Model Initialized')
recallMax = 0
ndcgMax = 0
bestEpoch = 0
for ep in range(0, args.epoch):
tstFlag = (ep % args.tstEpoch == 0)
reses = self.trainEpoch()
log(self.makePrint('Train', ep, reses, tstFlag))
if tstFlag:
reses = self.testEpoch()
if (reses['Recall'] > recallMax):
recallMax = reses['Recall']
ndcgMax = reses['NDCG']
bestEpoch = ep
log(self.makePrint('Test', ep, reses, tstFlag))
print()
print('Best epoch : ', bestEpoch, ' , Recall : ', recallMax, ' , NDCG : ', ndcgMax)
def prepareModel(self):
self.model = Model(self.handler).cuda()
self.opt = torch.optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=0)
self.diffusion_model = GaussianDiffusion(args.noise_scale, args.noise_min, args.noise_max, args.steps).cuda()
out_dims = eval(args.dims) + [args.entity_n]
in_dims = out_dims[::-1]
self.denoise_model = Denoise(in_dims, out_dims, args.d_emb_size, norm=args.norm).cuda()
self.denoise_opt = torch.optim.Adam(self.denoise_model.parameters(), lr=args.lr, weight_decay=0)
def trainEpoch(self):
trnLoader = self.handler.trnLoader
trnLoader.dataset.negSampling()
epLoss, epRecLoss, epClLoss = 0, 0, 0
epDiLoss, epUKLoss = 0, 0
steps = trnLoader.dataset.__len__() // args.batch
diffusionLoader = self.handler.diffusionLoader
for i, batch in enumerate(diffusionLoader):
batch_item, batch_index = batch
batch_item, batch_index = batch_item.cuda(), batch_index.cuda()
ui_matrix = self.handler.ui_matrix
iEmbeds = self.model.getEntityEmbeds().detach()
uEmbeds = self.model.getUserEmbeds().detach()
self.denoise_opt.zero_grad()
diff_loss, ukgc_loss = self.diffusion_model.training_losses(self.denoise_model, batch_item, ui_matrix, uEmbeds, iEmbeds, batch_index)
loss = diff_loss.mean() * (1-args.e_loss) + ukgc_loss.mean() * args.e_loss
epDiLoss += diff_loss.mean().item()
epUKLoss += ukgc_loss.mean().item()
loss.backward()
self.denoise_opt.step()
log('Diffusion Step %d/%d' % (i, diffusionLoader.dataset.__len__() // args.batch), save=False, oneline=True)
log('')
log('Start to re-build kg')
with torch.no_grad():
denoised_edges = []
h_list = []
t_list = []
for _, batch in enumerate(diffusionLoader):
batch_item, batch_index = batch
batch_item, batch_index = batch_item.cuda(), batch_index.cuda()
denoised_batch = self.diffusion_model.p_sample(self.denoise_model, batch_item, args.sampling_steps)
top_item, indices_ = torch.topk(denoised_batch, k=args.rebuild_k)
for i in range(batch_index.shape[0]):
for j in range(indices_[i].shape[0]):
h_list.append(batch_index[i])
t_list.append(indices_[i][j])
edge_set = set()
for index in range(len(h_list)):
edge_set.add((int(h_list[index].cpu().numpy()), int(t_list[index].cpu().numpy())))
for index in range(len(h_list)):
if (int(t_list[index].cpu().numpy()), int(h_list[index].cpu().numpy())) not in edge_set:
h_list.append(t_list[index])
t_list.append(h_list[index])
relation_dict = self.handler.relation_dict
for index in range(len(h_list)):
try:
denoised_edges.append([h_list[index], t_list[index], relation_dict[int(h_list[index].cpu().numpy())][int(t_list[index].cpu().numpy())]])
except Exception:
continue
graph_tensor = torch.tensor(denoised_edges)
index_ = graph_tensor[:, :-1]
type_ = graph_tensor[:, -1]
denoisedKG = (index_.t().long().cuda(), type_.long().cuda())
log('KG built!')
with torch.no_grad():
index_, type_ = denoisedKG
mask = ((torch.rand(type_.shape[0]) + args.keepRate).floor()).type(torch.bool)
denoisedKG = (index_[:, mask], type_[mask])
self.generatedKG = denoisedKG
for i, tem in enumerate(trnLoader):
ancs, poss, negs = tem
ancs = ancs.long().cuda()
poss = poss.long().cuda()
negs = negs.long().cuda()
self.opt.zero_grad()
if args.cl_pattern == 0:
usrEmbeds, itmEmbeds = self.model(self.handler.torchBiAdj, denoisedKG)
else:
usrEmbeds, itmEmbeds = self.model(self.handler.torchBiAdj)
ancEmbeds = usrEmbeds[ancs]
posEmbeds = itmEmbeds[poss]
negEmbeds = itmEmbeds[negs]
scoreDiff = pairPredict(ancEmbeds, posEmbeds, negEmbeds)
bprLoss = - (scoreDiff).sigmoid().log().sum() / args.batch
regLoss = calcRegLoss(self.model) * args.reg
if args.cl_pattern == 0:
usrEmbeds_kg, itmEmbeds_kg = self.model(self.handler.torchBiAdj)
else:
usrEmbeds_kg, itmEmbeds_kg = self.model(self.handler.torchBiAdj, denoisedKG)
denoisedKGEmbeds = torch.concat([usrEmbeds, itmEmbeds], axis=0)
kgEmbeds = torch.concat([usrEmbeds_kg, itmEmbeds_kg], axis=0)
clLoss = (contrastLoss(kgEmbeds[args.user:], denoisedKGEmbeds[args.user:], poss, args.temp) + contrastLoss(kgEmbeds[:args.user], denoisedKGEmbeds[:args.user], ancs, args.temp)) * args.ssl_reg
loss = bprLoss + regLoss + clLoss
epLoss += loss.item()
epRecLoss += bprLoss.item()
epClLoss += clLoss.item()
loss.backward()
self.opt.step()
log('Step %d/%d: loss = %.3f, regLoss = %.3f' % (i, steps, loss, regLoss), save=False, oneline=True)
ret = dict()
ret['Loss'] = epLoss / steps
ret['recLoss'] = epRecLoss / steps
ret['clLoss'] = epClLoss / steps
ret['diLoss'] = epDiLoss / diffusionLoader.dataset.__len__()
ret['UKGCLoss'] = epUKLoss / diffusionLoader.dataset.__len__()
return ret
def testEpoch(self):
tstLoader = self.handler.tstLoader
epRecall, epNdcg = [0] * 2
i = 0
num = tstLoader.dataset.__len__()
steps = num // args.tstBat
with torch.no_grad():
if args.cl_pattern == 0:
denoisedKG = self.generatedKG
usrEmbeds, itmEmbeds = self.model(self.handler.torchBiAdj, mess_dropout=False, kg=denoisedKG)
else:
usrEmbeds, itmEmbeds = self.model(self.handler.torchBiAdj, mess_dropout=False)
for usr, trnMask in tstLoader:
i += 1
usr = usr.long().cuda()
trnMask = trnMask.cuda()
allPreds = t.mm(usrEmbeds[usr], t.transpose(itmEmbeds, 1, 0)) * (1 - trnMask) - trnMask * 1e8
_, topLocs = t.topk(allPreds, args.topk)
recall, ndcg = self.calcRes(topLocs.cpu().numpy(), self.handler.tstLoader.dataset.tstLocs, usr)
epRecall += recall
epNdcg += ndcg
log('Steps %d/%d: recall = %.2f, ndcg = %.2f ' % (i, steps, recall, ndcg), save=False, oneline=True)
ret = dict()
ret['Recall'] = epRecall / num
ret['NDCG'] = epNdcg / num
return ret
def calcRes(self, topLocs, tstLocs, batIds):
assert topLocs.shape[0] == len(batIds)
allRecall = allNdcg = 0
for i in range(len(batIds)):
temTopLocs = list(topLocs[i])
temTstLocs = tstLocs[batIds[i]]
tstNum = len(temTstLocs)
maxDcg = np.sum([np.reciprocal(np.log2(loc + 2)) for loc in range(min(tstNum, args.topk))])
recall = dcg = 0
for val in temTstLocs:
if val in temTopLocs:
recall += 1
dcg += np.reciprocal(np.log2(temTopLocs.index(val) + 2))
recall = recall / tstNum
ndcg = dcg / maxDcg
allRecall += recall
allNdcg += ndcg
return allRecall, allNdcg
def seed_it(seed):
random.seed(seed)
os.environ["PYTHONSEED"] = str(seed)
np.random.seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
torch.manual_seed(seed)
if __name__ == '__main__':
seed_it(args.seed)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
logger.saveDefault = True
log('Start')
handler = DataHandler()
handler.LoadData()
log('Load Data')
coach = Coach(handler)
coach.run()