-
Notifications
You must be signed in to change notification settings - Fork 4
/
lmu_extensions.py
252 lines (202 loc) · 8.84 KB
/
lmu_extensions.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
#!/usr/bin/env python
import logging
import numpy as np
import sys
import os
import importlib
import cPickle
import theano
from theano import tensor
from blocks.extensions import Printing, SimpleExtension, FinishAfter, ProgressBar
from blocks.extensions.monitoring import DataStreamMonitoring, TrainingDataMonitoring
from blocks.graph import ComputationGraph
from blocks.main_loop import MainLoop
from blocks.model import Model
from blocks.algorithms import GradientDescent
class RankerEvaluator(SimpleExtension):
def __init__(self, path, model, data_stream, vocab_size, vocab, eval_mode, quiet=False, iter_num=1, **kwargs):
super(RankerEvaluator, self).__init__(**kwargs)
self.path = path
self.model = model
self.data_stream = data_stream
self.iter_num = iter_num
self.gen_fun = self.model.get_theano_function()
self.vocab_size = vocab_size
self.eval_mode = eval_mode
self.quiet = quiet
self.vocab = vocab
self.best_macroF1 = 0.0
def generate_candidates(self, context):
pass
def compute_batch(self, data, batch_num):
#TODO
data.pop('answer',None)
data.pop('answer_mask',None)
temp = self.gen_fun(**data)[0]
print"temp: "
print temp
return temp.shape[0] - temp.sum()
def do_load(self):
try:
with open(self.path, 'r') as f:
print 'Loading parameters from ' + self.path
self.model.set_parameter_values(cPickle.load(f))
except IOError:
print 'Error in loading!'
def do(self, which_callback, *args):
if self.path != "":
self.do_load()
epoch_iter = self.data_stream.get_epoch_iterator(as_dict=True)
count = 0
macroF1 = 0.0
num_of_examples = 0.0
precision_sum, recall_sum, exact_sum,f1_sum = 0.0 , 0.0 , 0.0, 0.0
total_correct_examples = 0.0
correct_examples = 0
for data in epoch_iter:
# data = epoch_iter.next()
count += 1
correct_examples = self.compute_batch(data, count)
print "batch #"+str(count)+" : "+str(correct_examples)
total_correct_examples += correct_examples
if self.eval_mode == 'batch':
break
print "total correct examples: ", total_correct_examples
class EvaluateModel(SimpleExtension):
def __init__(self, path, model, data_stream, vocab_size, vocab, eval_mode, quiet=False, iter_num=1, **kwargs):
super(EvaluateModel, self).__init__(**kwargs)
self.path = path
self.model = model
self.data_stream = data_stream
self.iter_num = iter_num
self.gen_fun = self.model.get_theano_function()
self.vocab_size = vocab_size
self.eval_mode = eval_mode
self.quiet = quiet
self.vocab = vocab
self.best_macroF1 = 0.0
def compute_batch(self, data, batch_num):
answer = data['answer']
answers = data['answers']
data.pop('answer_mask', None)
data.pop('answer', None)
data.pop('ans_indices_mask', None)
data.pop('ans_indices', None)
data.pop('answers', None)
temp = self.gen_fun(**data)
predictions_indices = np.asarray(temp).T
context = data['context']
predictions = []
for i,ctx in enumerate(context): #loop over all contexts to get answers
predictions.append(context[i,predictions_indices[i]])
predictions = np.asarray(predictions)
if self.quiet==False:
for i in range(len(predictions)):
for j in range(len(predictions[i,:])):
if self.vocab[predictions[i,j]] == "<EOA>":
print "found <EOA>, all zeros"
predictions[i,j:] = 0
break;
for i in range(len(predictions)):
print "answer:",
for j in range(len(answer[i,:])):
if answer[i,j] > 1:
print self.vocab[answer[i,j]],
print ""
print "predictions: ",
for j in range(len(predictions[i,:])):
if predictions[i,j] > 1:
print self.vocab[predictions[i,j]],
print ""
print "context: "
for j in range(len(context[i,:])):
if context[i, j] > 1 :
print self.vocab[context[i, j]],
print
print "answers: "
for j in answers[i]:
for k in j:
if k > 1:
print self.vocab[k],
print
print ""
print ""
context_bag = (context[:,:,None] == np.arange(self.vocab_size)).sum(axis = 1).clip(0,1)
answer_bag = (answer[:,None] == np.arange(self.vocab_size)[:,None]).sum(axis=2).clip(0,1)
answer_bag[:,0:3] = 0
#predictions_bag = (predictions[:,None] == np.arange(self.vocab_size)[:,None]).sum(axis=2).clip(0,1)
predictions_bag = (predictions[:,None] == np.arange(self.vocab_size)).sum(axis=1).clip(0,1).sum(axis=1)
# print predictions_bag.sum(axis=1)
predictions_bag[:,0:4] = 0
selected_items = predictions_bag.sum(axis=1, dtype=float)
precision = np.zeros(shape=(selected_items.shape[0]),dtype=float)
recall = np.zeros(shape=(selected_items.shape[0]),dtype=float)
macroF1 = np.zeros(shape=(selected_items.shape[0]),dtype=float)
answers_bag = []
for i,document in enumerate(answers):
prediction_bag = predictions_bag[i] #pred bag for one sample
document = np.array(list(document))
document_answers_bag = (document[:,None] == np.arange(self.vocab_size)[:,None]).sum(axis=2,dtype=float).clip(0,1)
document_answers_bag[:,0:3] = 0 #multiple answers for one sample
tps = (document_answers_bag * prediction_bag).sum(axis=1,dtype=float) #tps for multiple answers e.g. [3,0,1]
relevant_items = document_answers_bag.sum(axis=1,dtype=float) #rel items for multiple answers e.g. [4,3,3]
precisions = tps / selected_items[i]
recalls = tps / relevant_items
precisions[np.isnan(precisions)] = 0;
recalls[np.isnan(recalls)] = 1;
macroF1s = (2 * ( precisions * recalls )) / (precisions + recalls)
macroF1s[np.isnan(macroF1s)] = 0;
macroF1[i] = macroF1s.max()
precision[i] = precisions[np.argmax(macroF1s)]
recall[i] = recalls[np.argmax(macroF1s)]
answers_bag.append(document_answers_bag)
num_of_examples = selected_items.shape[0]
precision_sum = precision.sum()
recall_sum = recall.sum()
f1_sum = macroF1.sum()
exact = (precision * recall == 1)
exact_sum = exact.sum()
avg_precision = precision.mean()
avg_recall = recall.mean()
macroF1_of_avg = (2 * ( avg_precision * avg_recall )) / (avg_precision + avg_recall)
return (precision_sum, recall_sum, exact_sum, f1_sum, num_of_examples)
def do_load(self):
try:
with open(self.path, 'r') as f:
print 'Loading parameters from ' + self.path
self.model.set_parameter_values(cPickle.load(f))
except IOError:
print 'Error in loading!'
def do(self, which_callback, *args):
if self.path != "":
self.do_load()
epoch_iter = self.data_stream.get_epoch_iterator(as_dict=True)
count = 0
macroF1 = 0.0
num_of_examples = 0.0
precision_sum, recall_sum, exact_sum,f1_sum = 0.0 , 0.0 , 0.0, 0.0
for data in epoch_iter:
# print('batch %d'%count)
count += 1
p,r,e,f1,n = self.compute_batch(data, count)
precision_sum += p
recall_sum += r
exact_sum += e
f1_sum += f1
num_of_examples += n
if self.eval_mode == 'batch':
break
avg_precision = precision_sum / num_of_examples
avg_recall = recall_sum / num_of_examples
avg_exact = exact_sum / num_of_examples
macroF1 = (2 * ( avg_precision * avg_recall )) / (avg_precision + avg_recall)
avg_of_f1s = f1_sum / num_of_examples
self.best_macroF1 = max(self.best_macroF1, macroF1)
print('Validation Set:')
print " avg_recall: " + str(avg_recall)
print " avg_precision: " + str(avg_precision)
print " macroF1: " + str(macroF1)
print " averageF1: " + str(avg_of_f1s)
print " exact match acc: " + str(avg_exact)
print " # of examples: " + str(num_of_examples)
print " best macroF1: " + str(self.best_macroF1)