-
Notifications
You must be signed in to change notification settings - Fork 6
/
EvaluateUser.py
executable file
·408 lines (347 loc) · 15.3 KB
/
EvaluateUser.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
import math
import tensorflow as tf
from multiprocessing import Pool
import numpy as np
from time import time
import gc
import pdb
import scipy.io as sio
# Global variables that are shared across processes
_model = None
_sess = None
_dataset = None
_K = None
_DictList = None
_gtItem = None
_user_rating = None
_user_order = None
_model_name = None
_userIdList = None
_size_part = None
def init_evaluate_model(dataset):
#DictList = []
userIdList = []
for idx in xrange(len(dataset.testRatings)):
user, gtItem = dataset.testRatings[idx] # gtItem: [list of items]
userIdList.append(user)
return userIdList
def init_report_model(dataset):
#DictList = []
userIdList1 = []
userIdList2 = []
for idx in xrange(len(dataset.testRatings)):
user, gtItem = dataset.testRatings[idx] # gtItem: [list of items]
userIdList1.append(user)
for idx in xrange(len(dataset.reportRatings)):
user, gtItem = dataset.reportRatings[idx] # gtItem: [list of items]
userIdList2.append(user)
return userIdList1, userIdList2
def evalRecList(model, sess, dataset, userIdList, args, mode=True):
global _model
global _DictList
global _sess
global _dataset
global _gtItem
global _user_rating
global _user_order
global _model_name
global _userIdList
global eval_batch
global _size_part
cpu_num = 1
eval_batch = len(dataset.testRatings) #2000
if args.tensor_batch == 0:
tensor_batch = eval_batch
else:
tensor_batch = args.tensor_batch
print "start evaluate RecList"
_dataset = dataset
_model = model
_sess = sess
_model_name = args.model
_userIdList = userIdList
_gpus = len(args.gpu.split(','))
# run_options = tf.RunOptions(report_tensor_allocations_upon_oom=True)
aucs, ndcgs, _gtItem = [], [], []
test_num = len(_dataset.testRatings)
userids, itemids = [], []
index = 0
_user_rating = []
_user_order = []
eval_batch_begin = time()
while True:
UserList = [] # eval_batch*1
if index > len(dataset.testRatings):
break
for idx in range(index, min(index + eval_batch, len(dataset.testRatings))):
user, gtItem = dataset.testRatings[idx] # gtItem: [list of items]
#
if idx == min(index + eval_batch, len(dataset.testRatings)) - 1:
UserList.append([user])
if _user_rating == []:
list_rating, list_order = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.testRecList[idx + 1 - len(UserList):idx + 1, :]})
_user_rating.extend(zip(UserList, list_rating))
_user_order.extend(list_order)
else:
user_prediction_temp, user_order_temp = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.testRecList[idx+1-len(UserList):idx+1,:]})
_user_rating.extend(zip(UserList, user_prediction_temp))
_user_order.extend(user_order_temp)
elif (idx - index) > 0 and (idx - index) % tensor_batch == 0:
if _user_rating == []:
user_prediction_temp, user_order_temp = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.testRecList[idx-len(UserList):idx,:]})
_user_rating.extend(zip(UserList, user_prediction_temp))
_user_order.extend(user_order_temp)
else:
user_prediction_temp, user_order_temp = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.testRecList[idx-len(UserList):idx,:]})
_user_rating.extend(zip(UserList, user_prediction_temp))
_user_order.extend(user_order_temp)
UserList = [[user]]
else:
UserList.append([user])
_gtItem.append(gtItem) # append a [list of items]
index += eval_batch
eval_batch_time_0 = time() - eval_batch_begin
print eval_batch_time_0
_size_part = int(math.ceil(len(_user_rating) / (0. + cpu_num)))
_user_order = np.array(_user_order)
res = _eval_users_list(0)
userids.extend(res[0])
itemids.extend(res[1])
aucs.extend(res[2])
ndcgs.extend(res[3])
eval_batch_time = time() - eval_batch_begin
return (userids, itemids, aucs, ndcgs)
def evalReportRecList(model, sess, dataset, userIdList, args, mode=True):
global _model
global _DictList
global _sess
global _dataset
global _gtItem
global _user_rating
global _user_order
global _model_name
global _userIdList
global eval_batch
global _size_part
cpu_num = 1
eval_batch = len(dataset.reportRatings) #2000
if args.tensor_batch == 0:
tensor_batch = eval_batch
else:
tensor_batch = args.tensor_batch
print "start evaluate RecList"
_dataset = dataset
_model = model
# _DictList = DictList
_sess = sess
_model_name = args.model
_userIdList = userIdList
_gpus = len(args.gpu.split(','))
# run_options = tf.RunOptions(report_tensor_allocations_upon_oom=True)
aucs, ndcgs, _gtItem = [], [], []
test_num = len(_dataset.reportRatings)
# give predictions on users
# for idx in xrange(len(_DictList)):
userids, itemids = [], []
index = 0
_user_rating = []
_user_order = []
eval_batch_begin = time()
while True:
UserList = [] # eval_batch*1
if index > len(dataset.reportRatings):
break
for idx in range(index, min(index + eval_batch, len(dataset.reportRatings))):
user, gtItem = dataset.reportRatings[idx] # gtItem: [list of items]
#
if idx == min(index + eval_batch, len(dataset.reportRatings)) - 1:
UserList.append([user])
if _user_rating == []:
list_rating, list_order = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.reportRecList[idx + 1 - len(UserList):idx + 1, :]})
_user_rating.extend(zip(UserList, list_rating))
_user_order.extend(list_order)
else:
user_prediction_temp, user_order_temp = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.reportRecList[idx+1-len(UserList):idx+1,:]})
_user_rating.extend(zip(UserList, user_prediction_temp))
_user_order.extend(user_order_temp)
elif (idx - index) > 0 and (idx - index) % tensor_batch == 0:
if _user_rating == []:
user_prediction_temp, user_order_temp = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.reportRecList[idx-len(UserList):idx,:]})
_user_rating.extend(zip(UserList, user_prediction_temp))
_user_order.extend(user_order_temp)
else:
user_prediction_temp, user_order_temp = _sess.run([_model.list_rating,_model.list_order], feed_dict={model.user_input: UserList, _model.candidates_reclist: dataset.reportRecList[idx-len(UserList):idx,:]})
_user_rating.extend(zip(UserList, user_prediction_temp))
_user_order.extend(user_order_temp)
UserList = [[user]]
else:
UserList.append([user])
_gtItem.append(gtItem) # append a [list of items]
index += eval_batch
eval_batch_time_0 = time() - eval_batch_begin
print eval_batch_time_0
_size_part = int(math.ceil(len(_user_rating) / (0. + cpu_num)))
_user_order = np.array(_user_order)
# pool = Pool(cpu_num)
# res = pool.map(_eval_users_list, range(cpu_num))
#
# pool.close()
# pool.join()
# for r in res:
# userids.extend(r[0])
# itemids.extend(r[1])
# aucs.extend(r[2])
# ndcgs.extend(r[3])
res = _eval_report_users_list(0)
userids.extend(res[0])
itemids.extend(res[1])
aucs.extend(res[2])
ndcgs.extend(res[3])
# index += eval_batch
eval_batch_time = time() - eval_batch_begin
return (userids, itemids, aucs, ndcgs)
def eval(model, sess, dataset, userIdList, args, mode = True):
global _model
global _K
global _DictList
global _sess
global _dataset
global _gtItem
# global _user_prediction # for memory
global _model_name
global _userIdList
global eval_batch
cpu_num = 10
eval_batch=2000
if args.tensor_batch == 0:
tensor_batch = eval_batch
else:
tensor_batch = args.tensor_batch
print "start evaluate"
_dataset = dataset
_model = model
_sess = sess
_K = args.topK
_model_name = args.model
_userIdList = userIdList
_gpus = len(args.gpu.split(','))
# run_options = tf.RunOptions(report_tensor_allocations_upon_oom=True)
hits, ndcgs, _gtItem= [], [], []
items = range(_dataset.num_items) # rank on all items
item_input = np.array(items)[:, None]
test_num = len(_dataset.testRatings)
userids,itemids=[],[]
index=0
_user_prediction = []
eval_batch_begin = time()
while True:
UserList = [] # eval_batch*1
if index>len(dataset.testRatings):
break
for idx in range(index,min(index+eval_batch,len(dataset.testRatings))):
user, gtItem = dataset.testRatings[idx] # gtItem: [list of items]
#
if idx == min(index + eval_batch, len(dataset.testRatings)) - 1:
UserList.append([user])
if _user_prediction == []:
_user_prediction.extend(zip( UserList, _sess.run(_model.all_rating, feed_dict={model.user_input: UserList})))
else:
user_prediction_temp = _sess.run(_model.all_rating,
feed_dict={model.user_input: UserList})
_user_prediction.extend(zip( UserList, user_prediction_temp))
# _user_prediction = np.concatenate([_user_prediction, user_prediction_temp], 0)
elif (idx - index) > 0 and (idx - index) % tensor_batch == 0:
if _user_prediction == []:
user_prediction_temp = _sess.run(_model.all_rating, feed_dict={model.user_input: UserList})
_user_prediction.extend(zip(UserList, user_prediction_temp))
# _user_prediction = np.concatenate(_user_prediction_tmp, 0)
else:
user_prediction_temp = _sess.run(_model.all_rating,
feed_dict={model.user_input: UserList})
# _user_prediction = np.concatenate([_user_prediction, user_prediction_temp], 0)
_user_prediction.extend(zip(UserList, user_prediction_temp))
UserList = [[user]]
else:
UserList.append([user])
_gtItem.append(gtItem) # append a [list of items]
index += eval_batch
eval_batch_time_0 = time() - eval_batch_begin
print eval_batch_time_0
pool = Pool(cpu_num)
res = pool.map(_eval_one_user_true, _user_prediction)
pool.close()
pool.join()
userids = userids + [r[0] for r in res]
itemids = itemids + [r[1] for r in res]
hits = hits + [r[2] for r in res]
ndcgs = ndcgs + [r[3] for r in res]
eval_batch_time = time() - eval_batch_begin
del _user_prediction
gc.collect()
return (userids, itemids, hits, ndcgs)
def _eval_report_users_list(i):
L = len(_user_rating)
user_rating = _user_rating[i*_size_part: min((i+1)*_size_part, L)]
user_order = _user_order[i*_size_part: min((i+1)*_size_part, L),:] # np.narray
user_flag = _dataset.reportFlagList[i*_size_part: min((i+1)*_size_part, L),:] # np.narray
dcg = np.sum(np.log(2) * ((np.log(user_order+2))**(-1)) * user_flag,axis=1) # (n,)
M, N = np.shape(user_order)
dcg_max = np.sum(np.log(2) * ((np.log(np.tile(range(N-1+2,-1+2,-1),[M,1])))**(-1)) * user_flag,axis=1) # (n,)
ndcg = dcg / dcg_max
user_order_masked = (user_order+1) * user_flag
user_order_insideClicked = np.argsort(np.argsort(-user_order_masked, axis=1), axis=1)
anti_auc = np.sum((user_order - user_order_insideClicked) * user_flag, axis=1)
auc_max = (N-np.sum(user_flag, axis=1)) * np.sum(user_flag, axis=1)
auc = (auc_max-anti_auc) / (0.+auc_max)
gtItem = _gtItem[i*_size_part: min((i+1)*_size_part, L)] # [list of pos items]
userid = _userIdList[i*_size_part: min((i+1)*_size_part, L)]
return (userid, gtItem, auc, ndcg)
def _eval_users_list(i):
L = len(_user_rating)
user_rating = _user_rating[i*_size_part: min((i+1)*_size_part, L)]
user_order = _user_order[i*_size_part: min((i+1)*_size_part, L),:] # np.narray
user_flag = _dataset.testFlagList[i*_size_part: min((i+1)*_size_part, L),:] # np.narray
dcg = np.sum(np.log(2) * ((np.log(user_order+2))**(-1)) * user_flag,axis=1) # (n,)
M, N = np.shape(user_order)
dcg_max = np.sum(np.log(2) * ((np.log(np.tile(range(N-1+2,-1+2,-1),[M,1])))**(-1)) * user_flag,axis=1) # (n,)
ndcg = dcg / dcg_max
user_order_masked = (user_order+1) * user_flag
user_order_insideClicked = np.argsort(np.argsort(-user_order_masked, axis=1), axis=1)
anti_auc = np.sum((user_order - user_order_insideClicked) * user_flag, axis=1)
auc_max = (N-np.sum(user_flag, axis=1)) * np.sum(user_flag, axis=1)
auc = (auc_max-anti_auc) / (0.+auc_max)
gtItem = _gtItem[i*_size_part: min((i+1)*_size_part, L)] # [list of pos items]
userid = _userIdList[i*_size_part: min((i+1)*_size_part, L)]
return (userid, gtItem, auc, ndcg)
def _eval_one_user_true(_user_prediction):
# predictions = _user_prediction[idx%eval_batch]
idx = _user_prediction[0][0]
predictions = _user_prediction[1]
gtItem = _gtItem[idx] # [list of pos items]
userid = _userIdList[idx]
rank_score = predictions[gtItem]
rank = np.zeros((len(gtItem),1),dtype=np.int32)
cur = 0
for i in predictions:
early_stop = 0
for s in xrange(len(gtItem)):
if i >= rank_score[s] and gtItem[s] != cur:
rank[s] += 1
if rank[s] >= _K:
early_stop += 1
if early_stop == len(gtItem):
hr = 0
ndcg = 0
return (userid, gtItem, hr, ndcg)
cur += 1
hr = 0
dcg_max = 0
dcg = 0
for s in xrange(len(gtItem)):
if rank[s] < _K:
hr += 1
dcg += math.log(2) / math.log(rank[s] + 2)
dcg_max += math.log(2) / math.log(s + 2)
hr /= (0.+len(gtItem))
ndcg = dcg/dcg_max
return (userid, gtItem, hr, ndcg)