-
Notifications
You must be signed in to change notification settings - Fork 31
/
test.py
173 lines (141 loc) · 4.51 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
__author__ = 'jingyuan'
import numpy as np
import multiprocessing as mp
from Test_Dataset import *
from New_Dataset import *
import math
import heapq
_testRatings = None
_K = None
def test(model_folder,id_epoch,topK):
test_file = "/home/jie/jingyuan/sigir/data/test_vine"
splitter = "\t"
testset = Test_Dataset(test_file, splitter)
train_file = "/home/jie/jingyuan/sigir/data/train_vine"
batch_size = 512
global trainset
trainset = New_Dataset(train_file, splitter, batch_size)
f = file(model_folder + str(id_epoch)+ '_usremblayer.save', 'rb')
global U_np
U_np = np.asarray(cPickle.load(f).eval())
f = file(model_folder + str(id_epoch) + '_videmblayer.save', 'rb')
global V_np
V_np = np.asarray(cPickle.load(f).eval())
f = file(model_folder + str(id_epoch) + '_attentionlayer_feat.save', 'rb')
global Wu_F_np
Wu_F_np = np.asarray(cPickle.load(f).eval())
global Wv_F_np
Wv_F_np = np.asarray(cPickle.load(f).eval())
global b_F_np
b_F_np = np.asarray(cPickle.load(f).eval())
global c_F_np
c_F_np = np.asarray(cPickle.load(f).eval())
f = file(model_folder + str(id_epoch) + '_attentionlayer_item.save', 'rb')
global Wu_I_np
Wu_I_np = np.asarray(cPickle.load(f).eval())
global Wv_I_np
Wv_I_np = np.asarray(cPickle.load(f).eval())
global b_I_np
b_I_np = np.asarray(cPickle.load(f).eval())
global c_I_np
c_I_np = np.asarray(cPickle.load(f).eval())
global _testRatings
_testRatings = testset.testRatings
global _K
_K = topK
num_ratings = len(_testRatings)
num_thread = mp.cpu_count()
pool = mp.Pool(processes=num_thread)
res = pool.map(eval_one_rating, range(num_ratings))
pool.close()
pool.join()
hits = [r[0] for r in res]
ndcgs = [r[1] for r in res]
return np.array(hits).mean(), np.array(ndcgs).mean()
def eval_one_rating(idx):
rating = _testRatings[idx]
hr = ndcg = 0
u = rating[0]
gtItem = rating[1]
map_item_score = {}
maxScore, usr_vec = predict(u, gtItem)
countLarger = 0
for i in xrange(49358):
early_stop = False
score = predict_user(usr_vec,i)
map_item_score[i] = score
#print score
if score > maxScore:
countLarger += 1
if countLarger > _K:
hr = ndcg = 0
early_stop = True
break
if early_stop == False:
ranklist = heapq.nlargest(_K, map_item_score, key=map_item_score.get)
hr = getHitRatio(ranklist, gtItem)
ndcg = getNDCG(ranklist, gtItem)
return (hr, ndcg)
def softmask(x):
y = np.exp(x)
sumx = np.sum(y,axis=0)
x = y/sumx
return x
def softmask_feat(atten, mask):
atten = np.where(mask,atten,np.NINF)
def predict(user_idx, item):
uu1 = U_np[user_idx]
vv = V_np[item]
index_u = trainset.u_list_map[user_idx]
frame_feat = trainset.video_features[index_u]
mask = trainset.frame_mask[index_u]
#frame_level
frame_feat_tran = np.dot(frame_feat, Wv_F_np)
attenu = np.dot(uu1, Wu_F_np)
attenu = np.reshape(attenu,(attenu[0],1,attenu[1]))
attenu = frame_feat_tran + attenu + b_F_np
atten = np.maximum(attenu, 0, attenu)
atten = np.sum(atten * c_F_np, axis=2)
atten = softmask_feat(atten, mask)
temp1 = np.dot(vv_feat_tran, Wv_I_np)
#print temp1.shape
attenu = np.dot(uu1, Wu_I_np)
#print attenu.shape
temp2 = temp1 + attenu + b_I_np
atten = temp2 * (temp2 > 0)
atten = np.sum(atten*c_I_np,axis=1)
atten = softmask(atten)
#VV = atten.reshape(atten.shape[0],atten.shape[1],1) * vv_feat_tran
#vv = self.V_np[item]
#attenu = attenu.reshape(attenu.shape[0],1,attenu.shape[1])
#temp1 = np.dot(VV, self.Wv_np)
#temp2 = temp1 + attenu + self.b_np
#atten = temp2 * (temp2 > 0)
#atten = np.sum(atten*self.c_np,axis=1)
#atten = softmask(atten)
oo = atten.reshape(-1,1) * temp1
uu2 = np.sum(oo, axis=0)
# print uu1.shape
# print uu2.shape
#print vv.shape
u_vec = uu1 + uu2
# print u_vec.shape
return np.inner(u_vec, vv), u_vec
def getHitRatio(ranklist, gtItem):
for item in ranklist:
if item == gtItem:
return 1
return 0
def getNDCG(ranklist, gtItem):
for i in xrange(len(ranklist)):
item = ranklist[i]
if item == gtItem:
return math.log(2) / math.log(i+2)
return 0
def predict_user(user, item):
u_vec = user
vv = V_np[item]
return np.inner(u_vec, vv)
hits,ndcgs = test(100)
print 'hits', hits
print 'ndcgs', ndcgs