-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_GRU.py
342 lines (269 loc) · 12.7 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
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
from pprint import pprint
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
# embedding the position
def pos_embed(x):
if x < -60:
return 0
if -60 <= x <= 60:
return x + 61
if x > 60:
return 122
def main_for_evaluation():
pathname = "./model/ATT_GRU_model-"
wordembedding = np.load('./data/vec.npy')
test_settings = network.Settings()
test_settings.vocab_size = 16693
test_settings.num_classes = 12
test_settings.big_num = 5561
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
with tf.variable_scope("model"):
mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings)
names_to_vars = {v.op.name: v for v in tf.global_variables()}
saver = tf.train.Saver(names_to_vars)
#testlist = range(1000, 1800, 100)
testlist = [9000]
for model_iter in testlist:
# for compatibility purposes only, name key changes from tf 0.x to 1.x, compat_layer
saver.restore(sess, pathname + str(model_iter))
time_str = datetime.datetime.now().isoformat()
print(time_str)
print('Evaluating all test data and save data for PR curve')
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
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))
def main(_):
#If you retrain the model, please remember to change the path to your own model below:
pathname = "./model/ATT_GRU_model-9000"
wordembedding = np.load('./data/vec.npy')
test_settings = network.Settings()
test_settings.vocab_size = 16693
test_settings.num_classes = 12
test_settings.big_num = 1
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
with tf.variable_scope("model"):
mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings)
names_to_vars = {v.op.name: v for v in tf.global_variables()}
saver = tf.train.Saver(names_to_vars)
saver.restore(sess, pathname)
print('reading word embedding data...')
vec = []
word2id = {}
f = open('./origin_data/vec.txt', encoding='utf-8')
content = f.readline()
content = content.strip().split()
dim = int(content[1])
while True:
content = f.readline()
if content == '':
break
content = content.strip().split()
word2id[content[0]] = len(word2id)
content = content[1:]
content = [(float)(i) for i in content]
vec.append(content)
f.close()
word2id['UNK'] = len(word2id)
word2id['BLANK'] = len(word2id)
print('reading relation to id')
relation2id = {}
id2relation = {}
f = open('./origin_data/relation2id.txt', 'r', encoding='utf-8')
while True:
content = f.readline()
if content == '':
break
content = content.strip().split()
relation2id[content[0]] = int(content[1])
id2relation[int(content[1])] = content[0]
f.close()
while True:
#try:
#BUG: Encoding error if user input directly from command line.
line = input('请输入中文句子,格式为 "name1 name2 sentence":')
#Read file from test file
'''
infile = open('test.txt', encoding='utf-8')
line = ''
for orgline in infile:
line = orgline.strip()
break
infile.close()
'''
en1, en2, sentence = line.strip().split()
print("实体1: " + en1)
print("实体2: " + en2)
print(sentence)
relation = 0
en1pos = sentence.find(en1)
if en1pos == -1:
en1pos = 0
en2pos = sentence.find(en2)
if en2pos == -1:
en2post = 0
output = []
# length of sentence is 70
fixlen = 70
# max length of position embedding is 60 (-60~+60)
maxlen = 60
#Encoding test x
for i in range(fixlen):
word = word2id['BLANK']
rel_e1 = pos_embed(i - en1pos)
rel_e2 = pos_embed(i - en2pos)
output.append([word, rel_e1, rel_e2])
for i in range(min(fixlen, len(sentence))):
word = 0
if sentence[i] not in word2id:
#print(sentence[i])
#print('==')
word = word2id['UNK']
#print(word)
else:
#print(sentence[i])
#print('||')
word = word2id[sentence[i]]
#print(word)
output[i][0] = word
test_x = []
test_x.append([output])
#Encoding test y
label = [0 for i in range(len(relation2id))]
label[0] = 1
test_y = []
test_y.append(label)
test_x = np.array(test_x)
test_y = np.array(test_y)
test_word = []
test_pos1 = []
test_pos2 = []
for i in range(len(test_x)):
word = []
pos1 = []
pos2 = []
for j in test_x[i]:
temp_word = []
temp_pos1 = []
temp_pos2 = []
for k in j:
temp_word.append(k[0])
temp_pos1.append(k[1])
temp_pos2.append(k[2])
word.append(temp_word)
pos1.append(temp_pos1)
pos2.append(temp_pos2)
test_word.append(word)
test_pos1.append(pos1)
test_pos2.append(pos2)
test_word = np.array(test_word)
test_pos1 = np.array(test_pos1)
test_pos2 = np.array(test_pos2)
#print("test_word Matrix:")
#print(test_word)
#print("test_pos1 Matrix:")
#print(test_pos1)
#print("test_pos2 Matrix:")
#print(test_pos2)
prob, accuracy = test_step(test_word, test_pos1, test_pos2, test_y)
prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0]
print("关系是:")
#print(prob)
top3_id = prob.argsort()[-3:][::-1]
for n, rel_id in enumerate(top3_id):
print("No." + str(n+1) + ": " + id2relation[rel_id] + ", Probability is " + str(prob[rel_id]))
#except Exception as e:
# print(e)
#result = model.evaluate_line(sess, input_from_line(line, char_to_id), id_to_tag)
#print(result)
if __name__ == "__main__":
tf.app.run()