-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.py
391 lines (324 loc) · 14.1 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import torch
import Utils.TimeLogger as logger
from Utils.TimeLogger import log
from Params import args
from Model import Model, GaussianDiffusion, Denoise
from DataHandler import DataHandler
import numpy as np
from Utils.Utils import *
import os
import scipy.sparse as sp
import random
import setproctitle
from scipy.sparse import coo_matrix
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')
recallMax = 0
ndcgMax = 0
precisionMax = 0
bestEpoch = 0
log('Model Initialized')
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']
precisionMax = reses['Precision']
bestEpoch = ep
log(self.makePrint('Test', ep, reses, tstFlag))
print()
print('Best epoch : ', bestEpoch, ' , Recall : ', recallMax, ' , NDCG : ', ndcgMax, ' , Precision', precisionMax)
def prepareModel(self):
if args.data == 'tiktok':
self.model = Model(self.handler.image_feats.detach(), self.handler.text_feats.detach(), self.handler.audio_feats.detach()).cuda()
else:
self.model = Model(self.handler.image_feats.detach(), self.handler.text_feats.detach()).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.item]
in_dims = out_dims[::-1]
self.denoise_model_image = Denoise(in_dims, out_dims, args.d_emb_size, norm=args.norm).cuda()
self.denoise_opt_image = torch.optim.Adam(self.denoise_model_image.parameters(), lr=args.lr, weight_decay=0)
out_dims = eval(args.dims) + [args.item]
in_dims = out_dims[::-1]
self.denoise_model_text = Denoise(in_dims, out_dims, args.d_emb_size, norm=args.norm).cuda()
self.denoise_opt_text = torch.optim.Adam(self.denoise_model_text.parameters(), lr=args.lr, weight_decay=0)
if args.data == 'tiktok':
out_dims = eval(args.dims) + [args.item]
in_dims = out_dims[::-1]
self.denoise_model_audio = Denoise(in_dims, out_dims, args.d_emb_size, norm=args.norm).cuda()
self.denoise_opt_audio = torch.optim.Adam(self.denoise_model_audio.parameters(), lr=args.lr, weight_decay=0)
def normalizeAdj(self, mat):
degree = np.array(mat.sum(axis=-1))
dInvSqrt = np.reshape(np.power(degree, -0.5), [-1])
dInvSqrt[np.isinf(dInvSqrt)] = 0.0
dInvSqrtMat = sp.diags(dInvSqrt)
return mat.dot(dInvSqrtMat).transpose().dot(dInvSqrtMat).tocoo()
def buildUIMatrix(self, u_list, i_list, edge_list):
mat = coo_matrix((edge_list, (u_list, i_list)), shape=(args.user, args.item), dtype=np.float32)
a = sp.csr_matrix((args.user, args.user))
b = sp.csr_matrix((args.item, args.item))
mat = sp.vstack([sp.hstack([a, mat]), sp.hstack([mat.transpose(), b])])
mat = (mat != 0) * 1.0
mat = (mat + sp.eye(mat.shape[0])) * 1.0
mat = self.normalizeAdj(mat)
idxs = torch.from_numpy(np.vstack([mat.row, mat.col]).astype(np.int64))
vals = torch.from_numpy(mat.data.astype(np.float32))
shape = torch.Size(mat.shape)
return torch.sparse.FloatTensor(idxs, vals, shape).cuda()
def trainEpoch(self):
trnLoader = self.handler.trnLoader
trnLoader.dataset.negSampling()
epLoss, epRecLoss, epClLoss = 0, 0, 0
epDiLoss = 0
epDiLoss_image, epDiLoss_text = 0, 0
if args.data == 'tiktok':
epDiLoss_audio = 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()
iEmbeds = self.model.getItemEmbeds().detach()
uEmbeds = self.model.getUserEmbeds().detach()
image_feats = self.model.getImageFeats().detach()
text_feats = self.model.getTextFeats().detach()
if args.data == 'tiktok':
audio_feats = self.model.getAudioFeats().detach()
self.denoise_opt_image.zero_grad()
self.denoise_opt_text.zero_grad()
if args.data == 'tiktok':
self.denoise_opt_audio.zero_grad()
diff_loss_image, gc_loss_image = self.diffusion_model.training_losses(self.denoise_model_image, batch_item, iEmbeds, batch_index, image_feats)
diff_loss_text, gc_loss_text = self.diffusion_model.training_losses(self.denoise_model_text, batch_item, iEmbeds, batch_index, text_feats)
if args.data == 'tiktok':
diff_loss_audio, gc_loss_audio = self.diffusion_model.training_losses(self.denoise_model_audio, batch_item, iEmbeds, batch_index, audio_feats)
loss_image = diff_loss_image.mean() + gc_loss_image.mean() * args.e_loss
loss_text = diff_loss_text.mean() + gc_loss_text.mean() * args.e_loss
if args.data == 'tiktok':
loss_audio = diff_loss_audio.mean() + gc_loss_audio.mean() * args.e_loss
epDiLoss_image += loss_image.item()
epDiLoss_text += loss_text.item()
if args.data == 'tiktok':
epDiLoss_audio += loss_audio.item()
if args.data == 'tiktok':
loss = loss_image + loss_text + loss_audio
else:
loss = loss_image + loss_text
loss.backward()
self.denoise_opt_image.step()
self.denoise_opt_text.step()
if args.data == 'tiktok':
self.denoise_opt_audio.step()
log('Diffusion Step %d/%d' % (i, diffusionLoader.dataset.__len__() // args.batch), save=False, oneline=True)
log('')
log('Start to re-build UI matrix')
with torch.no_grad():
u_list_image = []
i_list_image = []
edge_list_image = []
u_list_text = []
i_list_text = []
edge_list_text = []
if args.data == 'tiktok':
u_list_audio = []
i_list_audio = []
edge_list_audio = []
for _, batch in enumerate(diffusionLoader):
batch_item, batch_index = batch
batch_item, batch_index = batch_item.cuda(), batch_index.cuda()
# image
denoised_batch = self.diffusion_model.p_sample(self.denoise_model_image, batch_item, args.sampling_steps, args.sampling_noise)
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]):
u_list_image.append(int(batch_index[i].cpu().numpy()))
i_list_image.append(int(indices_[i][j].cpu().numpy()))
edge_list_image.append(1.0)
# text
denoised_batch = self.diffusion_model.p_sample(self.denoise_model_text, batch_item, args.sampling_steps, args.sampling_noise)
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]):
u_list_text.append(int(batch_index[i].cpu().numpy()))
i_list_text.append(int(indices_[i][j].cpu().numpy()))
edge_list_text.append(1.0)
if args.data == 'tiktok':
# audio
denoised_batch = self.diffusion_model.p_sample(self.denoise_model_audio, batch_item, args.sampling_steps, args.sampling_noise)
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]):
u_list_audio.append(int(batch_index[i].cpu().numpy()))
i_list_audio.append(int(indices_[i][j].cpu().numpy()))
edge_list_audio.append(1.0)
# image
u_list_image = np.array(u_list_image)
i_list_image = np.array(i_list_image)
edge_list_image = np.array(edge_list_image)
self.image_UI_matrix = self.buildUIMatrix(u_list_image, i_list_image, edge_list_image)
self.image_UI_matrix = self.model.edgeDropper(self.image_UI_matrix)
# text
u_list_text = np.array(u_list_text)
i_list_text = np.array(i_list_text)
edge_list_text = np.array(edge_list_text)
self.text_UI_matrix = self.buildUIMatrix(u_list_text, i_list_text, edge_list_text)
self.text_UI_matrix = self.model.edgeDropper(self.text_UI_matrix)
if args.data == 'tiktok':
# audio
u_list_audio = np.array(u_list_audio)
i_list_audio = np.array(i_list_audio)
edge_list_audio = np.array(edge_list_audio)
self.audio_UI_matrix = self.buildUIMatrix(u_list_audio, i_list_audio, edge_list_audio)
self.audio_UI_matrix = self.model.edgeDropper(self.audio_UI_matrix)
log('UI matrix built!')
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.data == 'tiktok':
usrEmbeds, itmEmbeds = self.model.forward_MM(self.handler.torchBiAdj, self.image_UI_matrix, self.text_UI_matrix, self.audio_UI_matrix)
else:
usrEmbeds, itmEmbeds = self.model.forward_MM(self.handler.torchBiAdj, self.image_UI_matrix, self.text_UI_matrix)
ancEmbeds = usrEmbeds[ancs]
posEmbeds = itmEmbeds[poss]
negEmbeds = itmEmbeds[negs]
scoreDiff = pairPredict(ancEmbeds, posEmbeds, negEmbeds)
bprLoss = - (scoreDiff).sigmoid().log().sum() / args.batch
regLoss = self.model.reg_loss() * args.reg
loss = bprLoss + regLoss
epRecLoss += bprLoss.item()
epLoss += loss.item()
if args.data == 'tiktok':
usrEmbeds1, itmEmbeds1, usrEmbeds2, itmEmbeds2, usrEmbeds3, itmEmbeds3 = self.model.forward_cl_MM(self.handler.torchBiAdj, self.image_UI_matrix, self.text_UI_matrix, self.audio_UI_matrix)
else:
usrEmbeds1, itmEmbeds1, usrEmbeds2, itmEmbeds2 = self.model.forward_cl_MM(self.handler.torchBiAdj, self.image_UI_matrix, self.text_UI_matrix)
if args.data == 'tiktok':
clLoss = (contrastLoss(usrEmbeds1, usrEmbeds2, ancs, args.temp) + contrastLoss(itmEmbeds1, itmEmbeds2, poss, args.temp)) * args.ssl_reg
clLoss += (contrastLoss(usrEmbeds1, usrEmbeds3, ancs, args.temp) + contrastLoss(itmEmbeds1, itmEmbeds3, poss, args.temp)) * args.ssl_reg
clLoss += (contrastLoss(usrEmbeds2, usrEmbeds3, ancs, args.temp) + contrastLoss(itmEmbeds2, itmEmbeds3, poss, args.temp)) * args.ssl_reg
else:
clLoss = (contrastLoss(usrEmbeds1, usrEmbeds2, ancs, args.temp) + contrastLoss(itmEmbeds1, itmEmbeds2, poss, args.temp)) * args.ssl_reg
clLoss1 = (contrastLoss(usrEmbeds, usrEmbeds1, ancs, args.temp) + contrastLoss(itmEmbeds, itmEmbeds1, poss, args.temp)) * args.ssl_reg
clLoss2 = (contrastLoss(usrEmbeds, usrEmbeds2, ancs, args.temp) + contrastLoss(itmEmbeds, itmEmbeds2, poss, args.temp)) * args.ssl_reg
if args.data == 'tiktok':
clLoss3 = (contrastLoss(usrEmbeds, usrEmbeds3, ancs, args.temp) + contrastLoss(itmEmbeds, itmEmbeds3, poss, args.temp)) * args.ssl_reg
clLoss_ = clLoss1 + clLoss2 + clLoss3
else:
clLoss_ = clLoss1 + clLoss2
if args.cl_method == 1:
clLoss = clLoss_
loss += clLoss
epClLoss += clLoss.item()
loss.backward()
self.opt.step()
log('Step %d/%d: bpr : %.3f ; reg : %.3f ; cl : %.3f ' % (
i,
steps,
bprLoss.item(),
regLoss.item(),
clLoss.item()
), save=False, oneline=True)
ret = dict()
ret['Loss'] = epLoss / steps
ret['BPR Loss'] = epRecLoss / steps
ret['CL loss'] = epClLoss / steps
ret['Di image loss'] = epDiLoss_image / (diffusionLoader.dataset.__len__() // args.batch)
ret['Di text loss'] = epDiLoss_text / (diffusionLoader.dataset.__len__() // args.batch)
if args.data == 'tiktok':
ret['Di audio loss'] = epDiLoss_audio / (diffusionLoader.dataset.__len__() // args.batch)
return ret
def testEpoch(self):
tstLoader = self.handler.tstLoader
epRecall, epNdcg, epPrecision = [0] * 3
i = 0
num = tstLoader.dataset.__len__()
steps = num // args.tstBat
if args.data == 'tiktok':
usrEmbeds, itmEmbeds = self.model.forward_MM(self.handler.torchBiAdj, self.image_UI_matrix, self.text_UI_matrix, self.audio_UI_matrix)
else:
usrEmbeds, itmEmbeds = self.model.forward_MM(self.handler.torchBiAdj, self.image_UI_matrix, self.text_UI_matrix)
for usr, trnMask in tstLoader:
i += 1
usr = usr.long().cuda()
trnMask = trnMask.cuda()
allPreds = torch.mm(usrEmbeds[usr], torch.transpose(itmEmbeds, 1, 0)) * (1 - trnMask) - trnMask * 1e8
_, topLocs = torch.topk(allPreds, args.topk)
recall, ndcg, precision = self.calcRes(topLocs.cpu().numpy(), self.handler.tstLoader.dataset.tstLocs, usr)
epRecall += recall
epNdcg += ndcg
epPrecision += precision
log('Steps %d/%d: recall = %.2f, ndcg = %.2f , precision = %.2f ' % (i, steps, recall, ndcg, precision), save=False, oneline=True)
ret = dict()
ret['Recall'] = epRecall / num
ret['NDCG'] = epNdcg / num
ret['Precision'] = epPrecision / num
return ret
def calcRes(self, topLocs, tstLocs, batIds):
assert topLocs.shape[0] == len(batIds)
allRecall = allNdcg = allPrecision = 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 = precision = 0
for val in temTstLocs:
if val in temTopLocs:
recall += 1
dcg += np.reciprocal(np.log2(temTopLocs.index(val) + 2))
precision += 1
recall = recall / tstNum
ndcg = dcg / maxDcg
precision = precision / args.topk
allRecall += recall
allNdcg += ndcg
allPrecision += precision
return allRecall, allNdcg, allPrecision
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()