-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
240 lines (204 loc) · 9.06 KB
/
test.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
import utils as ut
# from params import *
import params
import load_data
import multiprocessing
import numpy as np
cores = 4
# cores = multiprocessing.cpu_count()
# print("cores:"+str(cores))
print(params.BATCH_SIZE)
# train_user_file_name = 'testTrain.dat'
data_generator_1 = load_data.Data(train_file=params.DIR + params.trainUserFileName_1, test_file=params.DIR+params.testUserFileName_1,batch_size=params.BATCH_SIZE)
data_generator_2 = load_data.Data(train_file=params.DIR + params.trainUserFileName_2, test_file=params.DIR+params.testUserFileName_2,batch_size=params.BATCH_SIZE)
data_generator_all = [data_generator_1, data_generator_2]
# print(params.trainUserFileName_1, params.testUserFileName_1)
# print(params.trainUserFileName_2, params.testUserFileName_2)
USER_NUM_1, ITEM_NUM_1 = data_generator_1.get_num_users_items()
USER_NUM_2, ITEM_NUM_2 = data_generator_2.get_num_users_items()
def test_one_user(x):
# user u's ratings for user u
rating = x[0]
#uid
u = x[1]
#user u's items in the training set
item_num = x[2]
data_generator = data_generator_all[x[3]]
training_items = data_generator.train_items[u]
#user u's items in the test set
user_pos_test = data_generator.test_set[u]
all_items = set(range(item_num))
test_items = list(all_items - set(training_items))
item_score = []
for i in test_items:
item_score.append((i, rating[i]))
item_score = sorted(item_score, key=lambda x: x[1])
item_score.reverse()
item_sort = [x[0] for x in item_score]
r = []
for i in item_sort:
if i in user_pos_test:
r.append(1)
else:
r.append(0)
recall_20 = ut.recall_at_k(r, 20, len(user_pos_test))
recall_40 = ut.recall_at_k(r, 40, len(user_pos_test))
recall_60 = ut.recall_at_k(r, 60, len(user_pos_test))
recall_80 = ut.recall_at_k(r, 80, len(user_pos_test))
recall_100 = ut.recall_at_k(r, 100, len(user_pos_test))
ap_20 = ut.average_precision(r,20)
ap_40 = ut.average_precision(r, 40)
ap_60 = ut.average_precision(r, 60)
ap_80 = ut.average_precision(r, 80)
ap_100 = ut.average_precision(r, 100)
return np.array([recall_20,recall_40,recall_60,recall_80,recall_100, ap_20,ap_40,ap_60,ap_80,ap_100])
def testAll(sess, model, users_to_test, feed_dict):
result_1 = np.array([0.] * 10)
result_2 = np.array([0.] * 10)
batch_size = params.BATCH_SIZE
#all users needed to test
test_users_1 = users_to_test[0]
test_users_2 = users_to_test[1]
test_user_num_1 = len(test_users_1)
test_user_num_2 = max(len(test_users_2),1)
item_num_list_1 = [model.n_items_1] * test_user_num_1
item_num_list_2 = [model.n_items_2] * test_user_num_2
data_generator_batch_1 = [0] * test_user_num_1
data_generator_batch_2 = [1] * test_user_num_2
user_batch_1 = test_users_1
user_batch_2 = test_users_2
# user_batch_rating_1, user_batch_rating_2 = sess.run([model.all_ratings_1, model.all_ratings_2], feed_dict=feed_dict)
user_batch_rating_1, user_batch_rating_2 = sess.run([model.all_ratings_1, model.all_ratings_2], feed_dict=feed_dict)
# user_batch_rating = sess.run(model.all_ratings, {model.users: user_batch})
# user_batch_rating = sess.run(model.all_ratings, {model.users: user_batch})
user_batch_rating_uid_1 = zip(user_batch_rating_1, user_batch_1, item_num_list_1, data_generator_batch_1)
user_batch_rating_uid_2 = zip(user_batch_rating_2, user_batch_2, item_num_list_2, data_generator_batch_2)
with multiprocessing.Pool(cores) as pool:
batch_result_1 = pool.map(test_one_user, user_batch_rating_uid_1)
batch_result_2 = pool.map(test_one_user, user_batch_rating_uid_2)
for re in batch_result_1:
result_1 += re
for re in batch_result_2:
result_2 += re
pool.close()
ret_1 = result_1 / test_user_num_1
ret_2 = result_2 / test_user_num_2
ret = [list(ret_1), list(ret_2)]
return ret
# def testAll(sess, model, users_to_test, data_generator, feed_dict):
# result_1 = np.array([0.] * 10)
# result_2 = np.array([0.] * 10)
# data_generator_1 = data_generator[0]
# data_generator_2 = data_generator[1]
# pool = multiprocessing.Pool(cores)
# batch_size = params.BATCH_SIZE
# #all users needed to test
# test_users_1 = users_to_test[0]
# test_users_2 = users_to_test[1]
# test_user_num_1 = len(test_users_1)
# test_user_num_2 = len(test_users_2)
# item_num_list_1 = [model.n_items_1] * params.BATCH_SIZE
# item_num_list_2 = [model.n_items_2] * params.BATCH_SIZE
# data_generator_batch_1 = [0] * params.BATCH_SIZE
# data_generator_batch_2 = [1] * params.BATCH_SIZE
# index = 0
# while True:
# if index >= min(test_user_num_1, test_user_num_2):
# break
# user_batch_1 = test_users_1[index:index + batch_size]
# user_batch_2 = test_users_2[index:index + batch_size]
# index += batch_size
# FLAG_1 = False
# if len(user_batch_1) < batch_size:
# user_batch_1 += [user_batch_1[-1]] * (batch_size - len(user_batch_1))
# user_batch_len_1 = len(user_batch_1)
# FLAG_1 = True
# FLAG_2 = False
# if len(user_batch_2) < batch_size:
# user_batch_2 += [user_batch_2[-1]] * (batch_size - len(user_batch_2))
# user_batch_len_2 = len(user_batch_2)
# FLAG = True
# user_batch_rating_1, user_batch_rating_2 = sess.run([model.all_ratings_1, model.all_ratings_2], feed_dict=feed_dict)
# # user_batch_rating = sess.run(model.all_ratings, {model.users: user_batch})
# user_batch_rating_uid_1 = zip(user_batch_rating_1, user_batch_1, item_num_list_1, data_generator_batch_1)
# user_batch_rating_uid_2 = zip(user_batch_rating_2, user_batch_2, item_num_list_2, data_generator_batch_2)
# batch_result_1 = pool.map(test_one_user, user_batch_rating_uid_1)
# batch_result_2 = pool.map(test_one_user, user_batch_rating_uid_2)
# if FLAG_1 == True:
# batch_result_1 = batch_result_1[:user_batch_len_1]
# if FLAG_2 == True:
# batch_result_2 = batch_result_2[:user_batch_len_2]
# for re in batch_result_1:
# result_1 += re
# for re in batch_result_2:
# result_2 += re
# pool.close()
# ret_1 = result_1 / test_user_num_1
# ret_2 = result_2 / test_user_num_2
# ret = [list(ret_1), list(ret_2)]
# return ret
# data_generator = load_data.Data(train_file=params.DIR + params.trainUserFileName, test_file=params.DIR+params.testUserFileName,batch_size=params.BATCH_SIZE)
# USER_NUM, ITEM_NUM = data_generator.get_num_users_items()
# def test_one_user(x):
# # user u's ratings for user u
# rating = x[0]
# #uid
# u = x[1]
# #user u's items in the training set
# training_items = data_generator.train_items[u]
# #user u's items in the test set
# user_pos_test = data_generator.test_set[u]
# all_items = set(range(ITEM_NUM))
# test_items = list(all_items - set(training_items))
# item_score = []
# for i in test_items:
# item_score.append((i, rating[i]))
# item_score = sorted(item_score, key=lambda x: x[1])
# item_score.reverse()
# item_sort = [x[0] for x in item_score]
# r = []
# for i in item_sort:
# if i in user_pos_test:
# r.append(1)
# else:
# r.append(0)
# recall_20 = ut.recall_at_k(r, 20, len(user_pos_test))
# recall_40 = ut.recall_at_k(r, 40, len(user_pos_test))
# recall_60 = ut.recall_at_k(r, 60, len(user_pos_test))
# recall_80 = ut.recall_at_k(r, 80, len(user_pos_test))
# recall_100 = ut.recall_at_k(r, 100, len(user_pos_test))
# ap_20 = ut.average_precision(r,20)
# ap_40 = ut.average_precision(r, 40)
# ap_60 = ut.average_precision(r, 60)
# ap_80 = ut.average_precision(r, 80)
# ap_100 = ut.average_precision(r, 100)
# return np.array([recall_20,recall_40,recall_60,recall_80,recall_100, ap_20,ap_40,ap_60,ap_80,ap_100])
# def testAll(sess, model, users_to_test):
# result = np.array([0.] * 10)
# pool = multiprocessing.Pool(cores)
# batch_size = params.BATCH_SIZE
# #all users needed to test
# test_users = users_to_test
# test_user_num = len(test_users)
# index = 0
# while True:
# if index >= test_user_num:
# break
# user_batch = test_users[index:index + batch_size]
# index += batch_size
# FLAG = False
# if len(user_batch) < batch_size:
# user_batch += [user_batch[-1]] * (batch_size - len(user_batch))
# user_batch_len = len(user_batch)
# FLAG = True
# user_batch_rating = sess.run(model.all_ratings, {model.users: user_batch,})
# user_batch_rating_uid = zip(user_batch_rating, user_batch)
# batch_result = pool.map(test_one_user, user_batch_rating_uid)
# if FLAG == True:
# batch_result = batch_result[:user_batch_len]
# for re in batch_result:
# result += re
# pool.close()
# ret = result / test_user_num
# ret = list(ret)
# return ret