-
Notifications
You must be signed in to change notification settings - Fork 13
/
DreamRec.py
533 lines (418 loc) · 20.4 KB
/
DreamRec.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import numpy as np
import pandas as pd
import math
import random
import argparse
import torch
from torch import nn
import torch.nn.functional as F
import torch.optim.lr_scheduler as lr_scheduler
import os
import logging
import time as Time
from utility import pad_history,calculate_hit,extract_axis_1
from collections import Counter
from Modules_ori import *
logging.getLogger().setLevel(logging.INFO)
def parse_args():
parser = argparse.ArgumentParser(description="Run supervised GRU.")
parser.add_argument('--epoch', type=int, default=1000,
help='Number of max epochs.')
parser.add_argument('--data', nargs='?', default='yc',
help='yc, ks, zhihu')
parser.add_argument('--random_seed', type=int, default=100,
help='random seed')
parser.add_argument('--batch_size', type=int, default=256,
help='Batch size.')
parser.add_argument('--layers', type=int, default=1,
help='gru_layers')
parser.add_argument('--hidden_factor', type=int, default=64,
help='Number of hidden factors, i.e., embedding size.')
parser.add_argument('--timesteps', type=int, default=200,
help='timesteps for diffusion')
parser.add_argument('--beta_end', type=float, default=0.02,
help='beta end of diffusion')
parser.add_argument('--beta_start', type=float, default=0.0001,
help='beta start of diffusion')
parser.add_argument('--lr', type=float, default=0.005,
help='Learning rate.')
parser.add_argument('--l2_decay', type=float, default=0,
help='l2 loss reg coef.')
parser.add_argument('--cuda', type=int, default=0,
help='cuda device.')
parser.add_argument('--dropout_rate', type=float, default=0.1,
help='dropout ')
parser.add_argument('--w', type=float, default=2.0,
help='dropout ')
parser.add_argument('--p', type=float, default=0.1,
help='dropout ')
parser.add_argument('--report_epoch', type=bool, default=True,
help='report frequency')
parser.add_argument('--diffuser_type', type=str, default='mlp1',
help='type of diffuser.')
parser.add_argument('--optimizer', type=str, default='adam',
help='type of optimizer.')
parser.add_argument('--beta_sche', nargs='?', default='exp',
help='')
parser.add_argument('--descri', type=str, default='',
help='description of the work.')
return parser.parse_args()
args = parse_args()
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
setup_seed(args.random_seed)
def extract(a, t, x_shape):
batch_size = t.shape[0]
out = a.gather(-1, t.cpu())
return out.reshape(batch_size, *((1,) * (len(x_shape) - 1))).to(t.device)
def linear_beta_schedule(timesteps, beta_start, beta_end):
beta_start = beta_start
beta_end = beta_end
return torch.linspace(beta_start, beta_end, timesteps)
def cosine_beta_schedule(timesteps, s=0.008):
steps = timesteps + 1
x = torch.linspace(0, timesteps, steps)
alphas_cumprod = torch.cos(((x / timesteps) + s) / (1 + s) * torch.pi * 0.5) ** 2
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
return torch.clip(betas, 0.0001, 0.9999)
def exp_beta_schedule(timesteps, beta_min=0.1, beta_max=10):
x = torch.linspace(1, 2 * timesteps + 1, timesteps)
betas = 1 - torch.exp(- beta_min / timesteps - x * 0.5 * (beta_max - beta_min) / (timesteps * timesteps))
return betas
def betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999):
"""
Create a beta schedule that discretizes the given alpha_t_bar function,
which defines the cumulative product of (1-beta) over time from t = [0,1].
:param num_diffusion_timesteps: the number of betas to produce.
:param alpha_bar: a lambda that takes an argument t from 0 to 1 and
produces the cumulative product of (1-beta) up to that
part of the diffusion process.
:param max_beta: the maximum beta to use; use values lower than 1 to
prevent singularities.
"""
betas = []
for i in range(num_diffusion_timesteps):
t1 = i / num_diffusion_timesteps
t2 = (i + 1) / num_diffusion_timesteps
betas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta))
return np.array(betas)
class diffusion():
def __init__(self, timesteps, beta_start, beta_end, w):
self.timesteps = timesteps
self.beta_start = beta_start
self.beta_end = beta_end
self.w = w
if args.beta_sche == 'linear':
self.betas = linear_beta_schedule(timesteps=self.timesteps, beta_start=self.beta_start, beta_end=self.beta_end)
elif args.beta_sche == 'exp':
self.betas = exp_beta_schedule(timesteps=self.timesteps)
elif args.beta_sche =='cosine':
self.betas = cosine_beta_schedule(timesteps=self.timesteps)
elif args.beta_sche =='sqrt':
self.betas = torch.tensor(betas_for_alpha_bar(self.timesteps, lambda t: 1-np.sqrt(t + 0.0001),)).float()
# define alphas
self.alphas = 1. - self.betas
self.alphas_cumprod = torch.cumprod(self.alphas, axis=0)
self.alphas_cumprod_prev = F.pad(self.alphas_cumprod[:-1], (1, 0), value=1.0)
self.sqrt_recip_alphas = torch.sqrt(1.0 / self.alphas)
# calculations for diffusion q(x_t | x_{t-1}) and others
self.sqrt_alphas_cumprod = torch.sqrt(self.alphas_cumprod)
self.sqrt_one_minus_alphas_cumprod = torch.sqrt(1. - self.alphas_cumprod)
self.sqrt_recip_alphas_cumprod = torch.sqrt(1. / self.alphas_cumprod)
self.sqrt_recipm1_alphas_cumprod = torch.sqrt(1. / self.alphas_cumprod - 1)
self.posterior_mean_coef1 = self.betas * torch.sqrt(self.alphas_cumprod_prev) / (1. - self.alphas_cumprod)
self.posterior_mean_coef2 = (1. - self.alphas_cumprod_prev) * torch.sqrt(self.alphas) / (1. - self.alphas_cumprod)
# calculations for posterior q(x_{t-1} | x_t, x_0)
self.posterior_variance = self.betas * (1. - self.alphas_cumprod_prev) / (1. - self.alphas_cumprod)
def q_sample(self, x_start, t, noise=None):
# print(self.betas)
if noise is None:
noise = torch.randn_like(x_start)
# noise = torch.randn_like(x_start) / 100
sqrt_alphas_cumprod_t = extract(self.sqrt_alphas_cumprod, t, x_start.shape)
sqrt_one_minus_alphas_cumprod_t = extract(
self.sqrt_one_minus_alphas_cumprod, t, x_start.shape
)
return sqrt_alphas_cumprod_t * x_start + sqrt_one_minus_alphas_cumprod_t * noise
def p_losses(self, denoise_model, x_start, h, t, noise=None, loss_type="l2"):
#
if noise is None:
noise = torch.randn_like(x_start)
# noise = torch.randn_like(x_start) / 100
#
x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise)
predicted_x = denoise_model(x_noisy, h, t)
#
if loss_type == 'l1':
loss = F.l1_loss(x_start, predicted_x)
elif loss_type == 'l2':
loss = F.mse_loss(x_start, predicted_x)
elif loss_type == "huber":
loss = F.smooth_l1_loss(x_start, predicted_x)
else:
raise NotImplementedError()
return loss, predicted_x
def predict_noise_from_start(self, x_t, t, x0):
return (
(extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t - x0) / \
extract(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape)
)
@torch.no_grad()
def p_sample(self, model_forward, model_forward_uncon, x, h, t, t_index):
x_start = (1 + self.w) * model_forward(x, h, t) - self.w * model_forward_uncon(x, t)
x_t = x
model_mean = (
extract(self.posterior_mean_coef1, t, x_t.shape) * x_start +
extract(self.posterior_mean_coef2, t, x_t.shape) * x_t
)
if t_index == 0:
return model_mean
else:
posterior_variance_t = extract(self.posterior_variance, t, x.shape)
noise = torch.randn_like(x)
return model_mean + torch.sqrt(posterior_variance_t) * noise
@torch.no_grad()
def sample(self, model_forward, model_forward_uncon, h):
x = torch.randn_like(h)
# x = torch.randn_like(h) / 100
for n in reversed(range(0, self.timesteps)):
x = self.p_sample(model_forward, model_forward_uncon, x, h, torch.full((h.shape[0], ), n, device=device, dtype=torch.long), n)
return x
class SinusoidalPositionEmbeddings(nn.Module):
def __init__(self, dim):
super().__init__()
self.dim = dim
def forward(self, time):
device = time.device
half_dim = self.dim // 2
embeddings = math.log(10000) / (half_dim - 1)
embeddings = torch.exp(torch.arange(half_dim, device=device) * -embeddings)
embeddings = time[:, None] * embeddings[None, :]
embeddings = torch.cat((embeddings.sin(), embeddings.cos()), dim=-1)
return embeddings
class Tenc(nn.Module):
def __init__(self, hidden_size, item_num, state_size, dropout, diffuser_type, device, num_heads=1):
super(Tenc, self).__init__()
self.state_size = state_size
self.hidden_size = hidden_size
self.item_num = int(item_num)
self.dropout = nn.Dropout(dropout)
self.diffuser_type = diffuser_type
self.device = device
self.item_embeddings = nn.Embedding(
num_embeddings=item_num + 1,
embedding_dim=hidden_size,
)
nn.init.normal_(self.item_embeddings.weight, 0, 1)
self.none_embedding = nn.Embedding(
num_embeddings=1,
embedding_dim=self.hidden_size,
)
nn.init.normal_(self.none_embedding.weight, 0, 1)
self.positional_embeddings = nn.Embedding(
num_embeddings=state_size,
embedding_dim=hidden_size
)
# emb_dropout is added
self.emb_dropout = nn.Dropout(dropout)
self.ln_1 = nn.LayerNorm(hidden_size)
self.ln_2 = nn.LayerNorm(hidden_size)
self.ln_3 = nn.LayerNorm(hidden_size)
self.mh_attn = MultiHeadAttention(hidden_size, hidden_size, num_heads, dropout)
self.feed_forward = PositionwiseFeedForward(hidden_size, hidden_size, dropout)
self.s_fc = nn.Linear(hidden_size, item_num)
# self.ac_func = nn.ReLU()
# self.step_embeddings = nn.Embedding(
# num_embeddings=50,
# embedding_dim=hidden_size
# )
self.step_mlp = nn.Sequential(
SinusoidalPositionEmbeddings(self.hidden_size),
nn.Linear(self.hidden_size, self.hidden_size*2),
nn.GELU(),
nn.Linear(self.hidden_size*2, self.hidden_size),
)
self.emb_mlp = nn.Sequential(
nn.SiLU(),
nn.Linear(self.hidden_size, self.hidden_size*2)
)
self.diff_mlp = nn.Sequential(
nn.Linear(self.hidden_size * 3, self.hidden_size*2),
nn.GELU(),
nn.Linear(self.hidden_size*2, self.hidden_size),
)
if self.diffuser_type =='mlp1':
self.diffuser = nn.Sequential(
nn.Linear(self.hidden_size*3, self.hidden_size)
)
elif self.diffuser_type =='mlp2':
self.diffuser = nn.Sequential(
nn.Linear(self.hidden_size * 3, self.hidden_size*2),
nn.GELU(),
nn.Linear(self.hidden_size*2, self.hidden_size)
)
def forward(self, x, h, step):
t = self.step_mlp(step)
if self.diffuser_type == 'mlp1':
res = self.diffuser(torch.cat((x, h, t), dim=1))
elif self.diffuser_type == 'mlp2':
res = self.diffuser(torch.cat((x, h, t), dim=1))
return res
def forward_uncon(self, x, step):
h = self.none_embedding(torch.tensor([0]).to(self.device))
h = torch.cat([h.view(1, 64)]*x.shape[0], dim=0)
t = self.step_mlp(step)
if self.diffuser_type == 'mlp1':
res = self.diffuser(torch.cat((x, h, t), dim=1))
elif self.diffuser_type == 'mlp2':
res = self.diffuser(torch.cat((x, h, t), dim=1))
return res
# return x
def cacu_x(self, x):
x = self.item_embeddings(x)
return x
def cacu_h(self, states, len_states, p):
#hidden
inputs_emb = self.item_embeddings(states)
inputs_emb += self.positional_embeddings(torch.arange(self.state_size).to(self.device))
seq = self.emb_dropout(inputs_emb)
mask = torch.ne(states, self.item_num).float().unsqueeze(-1).to(self.device)
seq *= mask
seq_normalized = self.ln_1(seq)
mh_attn_out = self.mh_attn(seq_normalized, seq)
ff_out = self.feed_forward(self.ln_2(mh_attn_out))
ff_out *= mask
ff_out = self.ln_3(ff_out)
state_hidden = extract_axis_1(ff_out, len_states - 1)
h = state_hidden.squeeze()
B, D = h.shape[0], h.shape[1]
mask1d = (torch.sign(torch.rand(B) - p) + 1) / 2
maske1d = mask1d.view(B, 1)
mask = torch.cat([maske1d] * D, dim=1)
mask = mask.to(self.device)
# print(h.device, self.none_embedding(torch.tensor([0]).to(self.device)).device, mask.device)
h = h * mask + self.none_embedding(torch.tensor([0]).to(self.device)) * (1-mask)
return h
def predict(self, states, len_states, diff):
#hidden
inputs_emb = self.item_embeddings(states)
inputs_emb += self.positional_embeddings(torch.arange(self.state_size).to(self.device))
seq = self.emb_dropout(inputs_emb)
mask = torch.ne(states, self.item_num).float().unsqueeze(-1).to(self.device)
seq *= mask
seq_normalized = self.ln_1(seq)
mh_attn_out = self.mh_attn(seq_normalized, seq)
ff_out = self.feed_forward(self.ln_2(mh_attn_out))
ff_out *= mask
ff_out = self.ln_3(ff_out)
state_hidden = extract_axis_1(ff_out, len_states - 1)
h = state_hidden.squeeze()
x = diff.sample(self.forward, self.forward_uncon, h)
test_item_emb = self.item_embeddings.weight
scores = torch.matmul(x, test_item_emb.transpose(0, 1))
return scores
def evaluate(model, test_data, diff, device):
eval_data=pd.read_pickle(os.path.join(data_directory, test_data))
batch_size = 100
evaluated=0
total_clicks=1.0
total_purchase = 0.0
total_reward = [0, 0, 0, 0]
hit_clicks=[0,0,0,0]
ndcg_clicks=[0,0,0,0]
hit_purchase=[0,0,0,0]
ndcg_purchase=[0,0,0,0]
seq, len_seq, target = list(eval_data['seq'].values), list(eval_data['len_seq'].values), list(eval_data['next'].values)
num_total = len(seq)
for i in range(num_total // batch_size):
seq_b, len_seq_b, target_b = seq[i * batch_size: (i + 1)* batch_size], len_seq[i * batch_size: (i + 1)* batch_size], target[i * batch_size: (i + 1)* batch_size]
states = np.array(seq_b)
states = torch.LongTensor(states)
states = states.to(device)
prediction = model.predict(states, np.array(len_seq_b), diff)
_, topK = prediction.topk(100, dim=1, largest=True, sorted=True)
topK = topK.cpu().detach().numpy()
sorted_list2=np.flip(topK,axis=1)
sorted_list2 = sorted_list2
calculate_hit(sorted_list2,topk,target_b,hit_purchase,ndcg_purchase)
total_purchase+=batch_size
hr_list = []
ndcg_list = []
print('{:<10s} {:<10s} {:<10s} {:<10s} {:<10s} {:<10s}'.format('HR@'+str(topk[0]), 'NDCG@'+str(topk[0]), 'HR@'+str(topk[1]), 'NDCG@'+str(topk[1]), 'HR@'+str(topk[2]), 'NDCG@'+str(topk[2])))
for i in range(len(topk)):
hr_purchase=hit_purchase[i]/total_purchase
ng_purchase=ndcg_purchase[i]/total_purchase
hr_list.append(hr_purchase)
ndcg_list.append(ng_purchase[0,0])
if i == 1:
hr_20 = hr_purchase
print('{:<10.6f} {:<10.6f} {:<10.6f} {:<10.6f} {:<10.6f} {:<10.6f}'.format(hr_list[0], (ndcg_list[0]), hr_list[1], (ndcg_list[1]), hr_list[2], (ndcg_list[2])))
return hr_20
if __name__ == '__main__':
# args = parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.cuda)
data_directory = './data/' + args.data
data_statis = pd.read_pickle(
os.path.join(data_directory, 'data_statis.df')) # read data statistics, includeing seq_size and item_num
seq_size = data_statis['seq_size'][0] # the length of history to define the seq
item_num = data_statis['item_num'][0] # total number of items
topk=[10, 20, 50]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
timesteps = args.timesteps
model = Tenc(args.hidden_factor,item_num, seq_size, args.dropout_rate, args.diffuser_type, device)
diff = diffusion(args.timesteps, args.beta_start, args.beta_end, args.w)
if args.optimizer == 'adam':
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, eps=1e-8, weight_decay=args.l2_decay)
elif args.optimizer =='adamw':
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, eps=1e-8, weight_decay=args.l2_decay)
elif args.optimizer =='adagrad':
optimizer = torch.optim.Adagrad(model.parameters(), lr=args.lr, eps=1e-8, weight_decay=args.l2_decay)
elif args.optimizer =='rmsprop':
optimizer = torch.optim.RMSprop(model.parameters(), lr=args.lr, eps=1e-8, weight_decay=args.l2_decay)
# scheduler = lr_scheduler.LinearLR(optimizer, start_factor=0.1, end_factor=1, total_iters=20)
model.to(device)
# optimizer.to(device)
train_data = pd.read_pickle(os.path.join(data_directory, 'train_data.df'))
total_step=0
hr_max = 0
best_epoch = 0
num_rows=train_data.shape[0]
num_batches=int(num_rows/args.batch_size)
for i in range(args.epoch):
start_time = Time.time()
for j in range(num_batches):
batch = train_data.sample(n=args.batch_size).to_dict()
seq = list(batch['seq'].values())
len_seq = list(batch['len_seq'].values())
target=list(batch['next'].values())
optimizer.zero_grad()
seq = torch.LongTensor(seq)
len_seq = torch.LongTensor(len_seq)
target = torch.LongTensor(target)
seq = seq.to(device)
target = target.to(device)
len_seq = len_seq.to(device)
x_start = model.cacu_x(target)
h = model.cacu_h(seq, len_seq, args.p)
n = torch.randint(0, args.timesteps, (args.batch_size, ), device=device).long()
loss, predicted_x = diff.p_losses(model, x_start, h, n, loss_type='l2')
loss.backward()
optimizer.step()
# scheduler.step()
if args.report_epoch:
if i % 1 == 0:
print("Epoch {:03d}; ".format(i) + 'Train loss: {:.4f}; '.format(loss) + "Time cost: " + Time.strftime(
"%H: %M: %S", Time.gmtime(Time.time()-start_time)))
if (i + 1) % 10 == 0:
eval_start = Time.time()
print('-------------------------- VAL PHRASE --------------------------')
_ = evaluate(model, 'val_data.df', diff, device)
print('-------------------------- TEST PHRASE -------------------------')
_ = evaluate(model, 'test_data.df', diff, device)
print("Evalution cost: " + Time.strftime("%H: %M: %S", Time.gmtime(Time.time()-eval_start)))
print('----------------------------------------------------------------')