forked from thunlp/OpenNRE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_GRU.py
234 lines (191 loc) · 7.64 KB
/
test_GRU.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
import tensorflow as tf
import numpy as np
import time
import datetime
import os
import network
from sklearn.metrics import average_precision_score
FLAGS = tf.app.flags.FLAGS
#change the name to who you want to send
#tf.app.flags.DEFINE_string('wechat_name', 'Tang-24-0325','the user you want to send info to')
tf.app.flags.DEFINE_string('wechat_name', 'filehelper','the user you want to send info to')
#if you want to try itchat, please set it to True
itchat_run = False
if itchat_run:
import itchat
def main(_):
# ATTENTION: change pathname before you load your model
pathname = "./model/ATT_GRU_model-"
wordembedding = np.load('./data/vec.npy')
test_settings = network.Settings()
test_settings.vocab_size = 114044
test_settings.num_classes = 53
test_settings.big_num = 262*9
big_num_test = test_settings.big_num
with tf.Graph().as_default():
sess = tf.Session()
with sess.as_default():
def test_step(word_batch, pos1_batch, pos2_batch, y_batch):
feed_dict = {}
total_shape = []
total_num = 0
total_word = []
total_pos1 = []
total_pos2 = []
for i in range(len(word_batch)):
total_shape.append(total_num)
total_num += len(word_batch[i])
for word in word_batch[i]:
total_word.append(word)
for pos1 in pos1_batch[i]:
total_pos1.append(pos1)
for pos2 in pos2_batch[i]:
total_pos2.append(pos2)
total_shape.append(total_num)
total_shape = np.array(total_shape)
total_word = np.array(total_word)
total_pos1 = np.array(total_pos1)
total_pos2 = np.array(total_pos2)
feed_dict[mtest.total_shape] = total_shape
feed_dict[mtest.input_word] = total_word
feed_dict[mtest.input_pos1] = total_pos1
feed_dict[mtest.input_pos2] = total_pos2
feed_dict[mtest.input_y] = y_batch
loss, accuracy ,prob= sess.run(
[mtest.loss, mtest.accuracy,mtest.prob], feed_dict)
return prob,accuracy
# evaluate p@n
def eval_pn(test_y,test_word,test_pos1,test_pos2,test_settings):
allprob = []
acc = []
for i in range(int(len(test_word)/float(test_settings.big_num))):
prob,accuracy = test_step(test_word[i*test_settings.big_num:(i+1)*test_settings.big_num],test_pos1[i*test_settings.big_num:(i+1)*test_settings.big_num],test_pos2[i*test_settings.big_num:(i+1)*test_settings.big_num],test_y[i*test_settings.big_num:(i+1)*test_settings.big_num])
acc.append(np.mean(np.reshape(np.array(accuracy),(test_settings.big_num))))
prob = np.reshape(np.array(prob),(test_settings.big_num,test_settings.num_classes))
for single_prob in prob:
allprob.append(single_prob[1:])
allprob = np.reshape(np.array(allprob),(-1))
eval_y = []
for i in test_y:
eval_y.append(i[1:])
allans = np.reshape(eval_y,(-1))
order = np.argsort(-allprob)
print 'P@100:'
top100 = order[:100]
correct_num_100 = 0.0
for i in top100:
if allans[i] == 1:
correct_num_100 += 1.0
print correct_num_100/100
print 'P@200:'
top200 = order[:200]
correct_num_200 = 0.0
for i in top200:
if allans[i] == 1:
correct_num_200 += 1.0
print correct_num_200/200
print 'P@300:'
top300 = order[:300]
correct_num_300 = 0.0
for i in top300:
if allans[i] == 1:
correct_num_300 += 1.0
print correct_num_300/300
if itchat_run:
tempstr = 'P@100\n'+str(correct_num_100/100)+'\n'+'P@200\n'+str(correct_num_200/200)+'\n'+'P@300\n'+str(correct_num_300/300)
itchat.send(tempstr,FLAGS.wechat_name)
with tf.variable_scope("model"):
mtest = network.GRU(is_training=False, word_embeddings = wordembedding, settings = test_settings)
saver = tf.train.Saver()
# ATTENTION: change the list to the iters you want to test !!
#testlist = range(9025,14000,25)
testlist = [10900]
for model_iter in testlist:
saver.restore(sess,pathname+str(model_iter))
print("Evaluating P@N for iter "+str(model_iter))
if itchat_run:
itchat.send("Evaluating P@N for iter "+str(model_iter),FLAGS.wechat_name)
print 'Evaluating P@N for one'
if itchat_run:
itchat.send('Evaluating P@N for one',FLAGS.wechat_name)
test_y = np.load('./data/pone_test_y.npy')
test_word = np.load('./data/pone_test_word.npy')
test_pos1 = np.load('./data/pone_test_pos1.npy')
test_pos2 = np.load('./data/pone_test_pos2.npy')
eval_pn(test_y,test_word,test_pos1,test_pos2,test_settings)
print 'Evaluating P@N for two'
if itchat_run:
itchat.send('Evaluating P@N for two',FLAGS.wechat_name)
test_y = np.load('./data/ptwo_test_y.npy')
test_word = np.load('./data/ptwo_test_word.npy')
test_pos1 = np.load('./data/ptwo_test_pos1.npy')
test_pos2 = np.load('./data/ptwo_test_pos2.npy')
eval_pn(test_y,test_word,test_pos1,test_pos2,test_settings)
print 'Evaluating P@N for all'
if itchat_run:
itchat.send('Evaluating P@N for all',FLAGS.wechat_name)
test_y = np.load('./data/pall_test_y.npy')
test_word = np.load('./data/pall_test_word.npy')
test_pos1 = np.load('./data/pall_test_pos1.npy')
test_pos2 = np.load('./data/pall_test_pos2.npy')
eval_pn(test_y,test_word,test_pos1,test_pos2,test_settings)
time_str = datetime.datetime.now().isoformat()
print time_str
print 'Evaluating all test data and save data for PR curve'
if itchat_run:
itchat.send('Evaluating all test data and save data for PR curve',FLAGS.wechat_name)
test_y = np.load('./data/testall_y.npy')
test_word = np.load('./data/testall_word.npy')
test_pos1 = np.load('./data/testall_pos1.npy')
test_pos2 = np.load('./data/testall_pos2.npy')
allprob = []
acc = []
for i in range(int(len(test_word)/float(test_settings.big_num))):
prob,accuracy = test_step(test_word[i*test_settings.big_num:(i+1)*test_settings.big_num],test_pos1[i*test_settings.big_num:(i+1)*test_settings.big_num],test_pos2[i*test_settings.big_num:(i+1)*test_settings.big_num],test_y[i*test_settings.big_num:(i+1)*test_settings.big_num])
acc.append(np.mean(np.reshape(np.array(accuracy),(test_settings.big_num))))
prob = np.reshape(np.array(prob),(test_settings.big_num,test_settings.num_classes))
for single_prob in prob:
allprob.append(single_prob[1:])
allprob = np.reshape(np.array(allprob),(-1))
order = np.argsort(-allprob)
print 'saving all test result...'
current_step = model_iter
# ATTENTION: change the save path before you save your result !!
np.save('./out/allprob_iter_'+str(current_step)+'.npy',allprob)
allans = np.load('./data/allans.npy')
#caculate the pr curve area
average_precision = average_precision_score(allans,allprob)
print 'PR curve area:'+str(average_precision)
if itchat_run:
itchat.send('PR curve area:'+str(average_precision),FLAGS.wechat_name)
time_str = datetime.datetime.now().isoformat()
print time_str
print 'P@N for all test data:'
print 'P@100:'
top100 = order[:100]
correct_num_100 = 0.0
for i in top100:
if allans[i] == 1:
correct_num_100 += 1.0
print correct_num_100/100
print 'P@200:'
top200 = order[:200]
correct_num_200 = 0.0
for i in top200:
if allans[i] == 1:
correct_num_200 += 1.0
print correct_num_200/200
print 'P@300:'
top300 = order[:300]
correct_num_300 = 0.0
for i in top300:
if allans[i] == 1:
correct_num_300 += 1.0
print correct_num_300/300
if itchat_run:
tempstr = 'P@100\n'+str(correct_num_100/100)+'\n'+'P@200\n'+str(correct_num_200/200)+'\n'+'P@300\n'+str(correct_num_300/300)
itchat.send(tempstr,FLAGS.wechat_name)
if __name__ == "__main__":
if itchat_run:
itchat.auto_login(hotReload=True,enableCmdQR=2)
tf.app.run()