-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
278 lines (208 loc) · 8.21 KB
/
utils.py
File metadata and controls
278 lines (208 loc) · 8.21 KB
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
import collections
import os
import torch
import numpy as np
import re
def merge_user_list(user_lists):
out = collections.defaultdict(list)
for user_list in user_lists:
for key, item in user_list.items():
out[key] = out[key] + item
return out
def merge_user_list_no_dup(user_lists):
out = collections.defaultdict(list)
for user_list in user_lists:
for key, item in user_list.items():
out[key] = out[key] + item
for key in out.keys():
out[key]=list(set(out[key]))
return out
def save_checkpoint(model, epoch, checkpoint_dir, buffer, max_to_keep=10):
state = {
'epoch': epoch,
'state_dict': model.state_dict(),
}
filename = os.path.join(checkpoint_dir, 'epoch={}.checkpoint.pth.tar'.format(epoch))
torch.save(state, filename)
buffer.append(filename)
if len(buffer)>max_to_keep:
os.remove(buffer[0])
del(buffer[0])
return buffer
def save_checkpoint_adv(model, epoch,checkpoint_dir):
state = {
'epoch': epoch,
'state_dict': model.state_dict(),
}
filename = os.path.join(checkpoint_dir, 'best_adv.checkpoint.pth.tar')
torch.save(state, filename)
return
def restore_checkpoint_adv(model, checkpoint_dir, device):
filename = os.path.join(checkpoint_dir, 'best_adv.checkpoint.pth.tar')
checkpoint=torch.load(filename, map_location = str(device))
model.load_state_dict(checkpoint['state_dict'])
return model
def restore_checkpoint(model, checkpoint_dir, device, force=False, pretrain=False):
"""
If a checkpoint exists, restores the PyTorch model from the checkpoint.
Returns the model and the current epoch.
"""
cp_files = [file_ for file_ in os.listdir(checkpoint_dir)
if file_.startswith('epoch=') and file_.endswith('.checkpoint.pth.tar')]
if not cp_files:
print('No saved model parameters found')
if force:
raise Exception("Checkpoint not found")
else:
return model, 0,
epoch_list = []
regex = re.compile(r'\d+')
for cp in cp_files:
epoch_list.append([int(x) for x in regex.findall(cp)][0])
epoch = max(epoch_list)
if not force:
print("Which epoch to load from? Choose in range [0, {})."
.format(epoch), "Enter 0 to train from scratch.")
print(">> ", end = '')
inp_epoch = int(input())
if inp_epoch not in range(epoch + 1):
raise Exception("Invalid epoch number")
if inp_epoch == 0:
print("Checkpoint not loaded")
clear_checkpoint(checkpoint_dir)
return model, 0,
else:
print("Which epoch to load from? Choose in range [0, {}).".format(epoch))
inp_epoch = int(input())
if inp_epoch not in range(0, epoch):
raise Exception("Invalid epoch number")
filename = os.path.join(checkpoint_dir,
'epoch={}.checkpoint.pth.tar'.format(inp_epoch))
print("Loading from checkpoint {}?".format(filename))
checkpoint = torch.load(filename, map_location = str(device))
try:
if pretrain:
model.load_state_dict(checkpoint['state_dict'], strict=False)
else:
model.load_state_dict(checkpoint['state_dict'])
print("=> Successfully restored checkpoint (trained for {} epochs)"
.format(checkpoint['epoch']))
except:
print("=> Checkpoint not successfully restored")
raise
return model, inp_epoch+1
def restore_best_checkpoint(epoch, model, checkpoint_dir, device):
"""
Restore the best performance checkpoint
"""
cp_files = [file_ for file_ in os.listdir(checkpoint_dir)
if file_.startswith('epoch=') and file_.endswith('.checkpoint.pth.tar')]
filename = os.path.join(checkpoint_dir,
'epoch={}.checkpoint.pth.tar'.format(epoch))
print("Loading from checkpoint {}?".format(filename))
checkpoint = torch.load(filename, map_location = str(device))
model.load_state_dict(checkpoint['state_dict'])
print("=> Successfully restored checkpoint (trained for {} epochs)"
.format(checkpoint['epoch']))
return model
def clear_checkpoint(checkpoint_dir):
filelist = [f for f in os.listdir(checkpoint_dir) if f.endswith(".pth.tar")]
for f in filelist:
os.remove(os.path.join(checkpoint_dir, f))
print("Checkpoint successfully removed")
def evaluation(args, data, model, epoch, base_path, evaluator, name="valid"):
# Evaluate with given evaluator
ret, _ = evaluator.evaluate(model)
n_ret = {"recall": ret[1], "hit_ratio": ret[5], "precision": ret[0], "ndcg": ret[3], "mrr":ret[4], "map":ret[2]}
perf_str = name+':{}'.format(n_ret)
print(perf_str)
with open(base_path + 'stats_{}.txt'.format(args.saveID), 'a') as f:
f.write(perf_str + "\n")
# Check if need to early stop (on validation)
is_best=False
early_stop=False
if name=="valid":
if ret[1] > data.best_valid_recall:
data.best_valid_epoch = epoch
data.best_valid_recall = ret[1]
data.patience = 0
is_best=True
else:
data.patience += 1
if data.patience >= args.patience:
print_str = "The best performance epoch is % d " % data.best_valid_epoch
print(print_str)
early_stop=True
return is_best, early_stop
def Item_pop(args, data, model):
for K in range(5):
eval_pop = ProxyEvaluator(data, data.train_user_list, data.pop_dict_list[K], top_k=[(K+1)*10],
dump_dict=merge_user_list([data.train_user_list, data.valid_user_list]))
ret, _ = eval_pop.evaluate(model)
print_str = "Overlap for K = % d is % f" % ( (K+1)*10, ret[1] )
print(print_str)
with open('stats_{}.txt'.format(args.saveID), 'a') as f:
f.write(print_str + "\n")
def ensureDir(dir_path):
if not os.path.exists(dir_path):
os.makedirs(dir_path)
def split_grp_view(grp_view,data,grp_idx):
n=len(grp_view)
split_data=[{} for _ in range(n)]
for key,item in data.items():
for it in item:
if key not in split_data[grp_idx[it]].keys():
split_data[grp_idx[it]][key]=[]
split_data[grp_idx[it]][key].append(it)
return split_data
def checktensor(tensor):
t=tensor.detach().cpu().numpy()
if np.max(np.isnan(t)):
idx=np.argmax(np.isnan(t))
return idx
else:
return -1
def sparse_dense_mul(s, d):
i = s._indices()
v = s._values()
dv = d[i[0,:], i[1,:]] # get values from relevant entries of dense matrix
return torch.sparse.FloatTensor(i, v * dv, s.size())
def binarize(y, thres=4):
"""Given threshold, binarize the ratings.
"""
y[y< thres] = 0
y[y>=thres] = 1
return y
def generate_total_sample(num_user, num_item):
sample = []
for i in range(num_user):
sample.extend([[i,j] for j in range(num_item)])
return np.array(sample)
def rating_mat_to_sample(mat):
row, col = np.nonzero(mat)
y = mat[row,col]
x = np.concatenate([row.reshape(-1,1), col.reshape(-1,1)], axis=1)
return x, y
def ndcg_func(model, x_te, y_te, device, top_k_list = [5, 10]):
"""Evaluate nDCG@K of the trained model on test dataset.
"""
all_user_idx = np.unique(x_te[:,0])
all_tr_idx = np.arange(len(x_te))
result_map = collections.defaultdict(list)
for uid in all_user_idx:
u_idx = all_tr_idx[x_te[:,0] == uid]
x_u = x_te[u_idx]
y_u = y_te[u_idx]
pred_u = model.new_predict(torch.LongTensor(x_u[:,0]).cuda(device), torch.LongTensor(x_u[:,1]).cuda(device))
for top_k in top_k_list:
pred_top_k = np.argsort(-pred_u)[:top_k]
count = y_u[pred_top_k].sum()
log2_iplus1 = np.log2(1+np.arange(1,top_k+1))
dcg_k = y_u[pred_top_k] / log2_iplus1
best_dcg_k = y_u[np.argsort(-y_u)][:top_k] / log2_iplus1
if np.sum(best_dcg_k) == 0:
ndcg_k = 1
else:
ndcg_k = np.sum(dcg_k) / np.sum(best_dcg_k)
result_map["ndcg_{}".format(top_k)].append(ndcg_k)
return result_map