-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
179 lines (146 loc) · 4.86 KB
/
utils.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
import torch
from torch import nn, optim
import numpy as np
from torch import log
from time import time
from sklearn.metrics import roc_auc_score
import random
import os
from dataloader import BasicDataset
import torch.autograd.profiler as profiler
import gc
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.cuda.set_device(2)
def minibatch(args, *tensors, **kwargs):
batch_size = kwargs.get('batch_size', args.bpr_batch)
if len(tensors) == 1:
tensor = tensors[0]
for i in range(0, len(tensor), batch_size):
yield tensor[i:i + batch_size]
else:
for i in range(0, len(tensors[0]), batch_size):
yield tuple(x[i:i + batch_size] for x in tensors)
def shuffle(*arrays, **kwargs):
require_indices = kwargs.get('indices', False)
if len(set(len(x) for x in arrays)) != 1:
raise ValueError('All inputs to shuffle must have '
'the same length.')
shuffle_indices = np.arange(len(arrays[0]))
np.random.shuffle(shuffle_indices)
if len(arrays) == 1:
result = arrays[0][shuffle_indices]
else:
result = tuple(x[shuffle_indices] for x in arrays)
if require_indices:
return result, shuffle_indices
else:
return result
class BPRLoss:
def __init__(self, recmodel, opt, config):
self.model = recmodel
self.opt = opt
self.weight_decay = config.decay#default=1e-4
def compute(self, users, pos, neg):
loss, reg_loss = self.model.bpr_loss(users, pos, neg)
torch.cuda.empty_cache()
gc.collect()
reg_loss = reg_loss*self.weight_decay
loss = loss + reg_loss
return loss
class timer:
from time import time
TAPE = [-1]
NAMED_TAPE = {}
@staticmethod
def get():
if len(timer.TAPE) > 1:
return timer.TAPE.pop()
else:
return -1
@staticmethod
def dict(select_keys=None):
hint = "|"
if select_keys is None:
for key, value in timer.NAMED_TAPE.items():
hint = hint + f"{key}:{value:.2f}|"
else:
for key in select_keys:
value = timer.NAMED_TAPE[key]
hint = hint + f"{key}:{value:.2f}|"
return hint
@staticmethod
def zero(select_keys=None):
if select_keys is None:
for key, value in timer.NAMED_TAPE.items():
timer.NAMED_TAPE[key] = 0
else:
for key in select_keys:
timer.NAMED_TAPE[key] = 0
def __init__(self, tape=None, **kwargs):
if kwargs.get('name'):
timer.NAMED_TAPE[kwargs['name']] = timer.NAMED_TAPE[
kwargs['name']] if timer.NAMED_TAPE.get(kwargs['name']) else 0.
self.named = kwargs['name']
if kwargs.get("group"):
#TODO: add group function
pass
else:
self.named = False
self.tape = tape or timer.TAPE
def __enter__(self):
self.start = timer.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.named:
timer.NAMED_TAPE[self.named] += timer.time() - self.start
else:
self.tape.append(timer.time() - self.start)
def RecallPrecision_ATk(test_data, r, k):
right_pred = r[:, :k].sum(1)
precis_n = k
recall_n = np.array([len(test_data[i]) for i in range(len(test_data))])
recall = np.sum(right_pred/recall_n)
precis = np.sum(right_pred)/precis_n
return {'recall': recall, 'precision': precis}
def MRRatK_r(r, k):
"""
Mean Reciprocal Rank
"""
pred_data = r[:, :k]
scores = np.log2(1./np.arange(1, k+1))
pred_data = pred_data/scores
pred_data = pred_data.sum(1)
return np.sum(pred_data)
def NDCGatK_r(test_data,r,k):
"""
Normalized Discounted Cumulative Gain
rel_i = 1 or 0, so 2^{rel_i} - 1 = 1 or 0
"""
assert len(r) == len(test_data)
pred_data = r[:, :k]
test_matrix = np.zeros((len(pred_data), k))
for i, items in enumerate(test_data):
length = k if k <= len(items) else len(items)
test_matrix[i, :length] = 1
max_r = test_matrix
idcg = np.sum(max_r * 1./np.log2(np.arange(2, k + 2)), axis=1)
dcg = pred_data*(1./np.log2(np.arange(2, k + 2)))
dcg = np.sum(dcg, axis=1)
idcg[idcg == 0.] = 1.
ndcg = dcg/idcg
ndcg[np.isnan(ndcg)] = 0.
return np.sum(ndcg)
def getLabel(test_data, pred_data):
r = []
for i in range(len(test_data)):
groundTrue = test_data[i]
predictTopK = pred_data[i]
pred = list(map(lambda x: x in groundTrue, predictTopK))
pred = np.array(pred).astype("float")
r.append(pred)
return np.array(r).astype('float')