forked from monikkinom/ner-lstm
/
test.py
202 lines (162 loc) · 6.31 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
import tensorflow as tf
import numpy as np
import functools
import random
import argparse
from input import get_final_data, get_test_data
from tensorflow.models.rnn import rnn_cell
from tensorflow.models.rnn import rnn
WORD_DIM = 305
MAX_SEQ_LEN = 70
NUM_CLASSES = 12
BATCH_SIZE = 128
NUM_HIDDEN = 300
NUM_LAYERS = 1
def lazy_property(function):
attribute = '_' + function.__name__
@property
@functools.wraps(function)
def wrapper(self):
if not hasattr(self, attribute):
setattr(self, attribute, function(self))
return getattr(self, attribute)
return wrapper
class Model():
def __init__(self, data, target, dropout, num_hidden, num_layers):
self.data = data
self.target = target
self.dropout = dropout
self._num_hidden = num_hidden
self._num_layers = num_layers
self.prediction
self.error
self.optimize
@lazy_property
def prediction(self):
fw_cell = rnn_cell.LSTMCell(self._num_hidden)
fw_cell = rnn_cell.DropoutWrapper(fw_cell, output_keep_prob=self.dropout)
bw_cell = rnn_cell.LSTMCell(self._num_hidden)
bw_cell = rnn_cell.DropoutWrapper(bw_cell, output_keep_prob=self.dropout)
if self._num_layers > 1:
fw_cell = rnn_cell.MultiRNNCell([fw_cell] * self._num_layers)
bw_cell = rnn_cell.MultiRNNCell([bw_cell] * self._num_layers)
output, _, _ = rnn.bidirectional_rnn(fw_cell, bw_cell, tf.unpack(tf.transpose(self.data, perm=[1, 0, 2])), dtype=tf.float32, sequence_length=self.length)
max_length = int(self.target.get_shape()[1])
num_classes = int(self.target.get_shape()[2])
weight, bias = self._weight_and_bias(2*self._num_hidden, num_classes)
output = tf.reshape(tf.transpose(tf.pack(output), perm=[1, 0, 2]), [-1, 2*self._num_hidden])
prediction = tf.nn.softmax(tf.matmul(output, weight) + bias)
prediction = tf.reshape(prediction, [-1, max_length, num_classes])
return prediction
@lazy_property
def length(self):
used = tf.sign(tf.reduce_max(tf.abs(self.data), reduction_indices=2))
length = tf.reduce_sum(used, reduction_indices=1)
length = tf.cast(length, tf.int32)
return length
@lazy_property
def cost(self):
cross_entropy = self.target * tf.log(self.prediction)
cross_entropy = -tf.reduce_sum(cross_entropy, reduction_indices=2)
mask = tf.sign(tf.reduce_max(tf.abs(self.target), reduction_indices=2))
cross_entropy *= mask
cross_entropy = tf.reduce_sum(cross_entropy, reduction_indices=1)
cross_entropy /= tf.cast(self.length, tf.float32)
return tf.reduce_mean(cross_entropy)
@lazy_property
def optimize(self):
optimizer = tf.train.AdamOptimizer(0.003)
return optimizer.minimize(self.cost)
@lazy_property
def error(self):
mistakes = tf.not_equal(
tf.argmax(self.target, 2), tf.argmax(self.prediction, 2))
mistakes = tf.cast(mistakes, tf.float32)
mask = tf.sign(tf.reduce_max(tf.abs(self.target), reduction_indices=2))
mistakes *= mask
# Average over actual sequence lengths.
mistakes = tf.reduce_sum(mistakes, reduction_indices=1)
mistakes /= tf.cast(self.length, tf.float32)
return tf.reduce_mean(mistakes)
@staticmethod
def _weight_and_bias(in_size,out_size):
weight = tf.truncated_normal([in_size,out_size], stddev=0.01)
bias = tf.constant(0.1, shape=[out_size])
return tf.Variable(weight), tf.Variable(bias)
@lazy_property
def getpredf1(self):
return self.prediction,self.length
def pfunc(num):
if num == 0:
return 'O'
elif num == 1:
return 'PER'
elif num == 2:
return 'LOC'
elif num == 3:
return 'ORG'
elif num == 4:
return 'MISC'
def fw(prediction,target,length, name):
target = np.argmax(target, 2)
prediction = np.argmax(prediction, 2)
f = open(name, 'w')
for i in range(len(target)):
for j in range(length[i]):
f.write(str(target[i][j]) + " " + str(prediction[i][j]) + "\n")
f.write('\n')
def f1(prediction,target,length):
tp=np.array([0]*(NUM_CLASSES+2))
fp=np.array([0]*(NUM_CLASSES+2))
fn=np.array([0]*(NUM_CLASSES+2))
target = np.argmax(target, 2)
prediction = np.argmax(prediction, 2)
for i in range(len(target)):
for j in range(length[i]):
if target[i][j] == prediction[i][j]:
tp[target[i][j]] += 1
else:
fp[target[i][j]] += 1
fn[prediction[i][j]] += 1
NON_NAMED_ENTITY = 0
for i in range(NUM_CLASSES):
if i != NON_NAMED_ENTITY:
tp[5] += tp[i]
fp[5] += fp[i]
fn[5] += fn[i]
else:
tp[6] += tp[i]
fp[6] += fp[i]
fn[6] += fn[i]
precision = []
recall = []
fscore = []
for i in range(NUM_CLASSES+2):
precision.append(tp[i]*1.0/(tp[i]+fp[i]))
recall.append(tp[i]*1.0/(tp[i]+ fn[i]))
fscore.append(2.0*precision[i]*recall[i]/(precision[i]+recall[i]))
print "precision = " ,precision
print "recall = " ,recall
print "f1score = " ,fscore
print "Entity fscore :", fscore[5]
def train():
test_inp, test_out = get_test_data()
print "test data loaded"
final_inp, final_out = get_final_data()
print "final data loaded"
data = tf.placeholder(tf.float32,[None, MAX_SEQ_LEN, WORD_DIM])
target = tf.placeholder(tf.float32, [None, MAX_SEQ_LEN, NUM_CLASSES])
dropout = tf.placeholder(tf.float32)
model = Model(data,target,dropout,NUM_HIDDEN,NUM_LAYERS)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
saver.restore(sess, 'model_max.ckpt')
pred = sess.run(model.prediction, {data: test_inp, target: test_out, dropout: 1})
pred,length = sess.run(model.getpredf1, {data: test_inp, target: test_out, dropout: 1})
fw(pred,test_out,length, 'testaresults')
pred = sess.run(model.prediction, {data: final_inp, target: final_out, dropout: 1})
pred,length = sess.run(model.getpredf1, {data: final_inp, target: final_out, dropout: 1})
fw(pred,final_out,length, 'testbresults')
if __name__ == '__main__' :
train()