-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmall_experiment.py
613 lines (422 loc) · 24.1 KB
/
small_experiment.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import atire
import numpy as np
import tensorflow as tf
import pickle as pkl
import tensorflow.contrib.slim as slim
import re
from itertools import islice
import os
from random import sample, randint, random
import random
import gensim
"""
"First system"
A prototype of the model described in the paper "Task-Oriented Query Reformulation with Reinforcement Learning",
Nogueira and Cho 2017
Author: Johnny Flame Lee
"""
DUMMYMODE = False
# How frequenty to test the network
TEST_FREQUENCY = 200
load_model = False
script_dir = os.path.dirname(__file__) # <-- absolute dir the script is in
record_dir = os.path.join(script_dir,"record")
if not os.path.exists(record_dir):
os.makedirs(record_dir)
record_file_name = "tmp"
weights_dir = os.path.join(script_dir,"weights")
if not os.path.exists(weights_dir):
os.mkdir(weights_dir)
weight_saving_path = os.path.join(script_dir,record_file_name,weights_dir,"model.ckpt")
fitness_record = os.path.join(script_dir, record_dir,record_file_name)
reformulated_query_filepath = os.path.join(script_dir, record_dir,"test time reformulated query")
test_time_MAP = os.path.join(script_dir, record_dir,"test time MAP")
CREATE_DICTIONARY = True
TOPIC_FILE = os.path.join(script_dir,"../evaluation/topics.51")
ASSESSMENT_FILE = os.path.join(script_dir,"../evaluation/WSJ.qrels")
CONTEXT_WINDOW = 4
# Total number of terms to go into the second network
CANDIDATE_AND_CONTEXT_LENGTH = CONTEXT_WINDOW * 2 + 1
WORD_VECTOR_DIMENSIONS = 300
# maximum number of terms in q0
MAX_SEQUENCE_LENGTH = 15
WORD_EMBEDDING_PATH = "wsj-collection-vectors"
METRIC = " -mMAP@40"
annealing_steps = 10000.
start_eps = 1.0
end_eps = 0.1
eps = start_eps
stepDrop = (start_eps - end_eps) / annealing_steps
PADDING = np.zeros(WORD_VECTOR_DIMENSIONS)
random.seed(500)
# HYPERPARAMETERS:
atire.init("atire -a " + ASSESSMENT_FILE + METRIC)
def write_to_file(filename, information):
"""
:param filename:
:param v: a 2d list of training information
:return:
"""
with open(filename, "a") as f:
message = ""
for row in information:
message += str(row[0]) + str(row[1]).strip('[]') + " "
message += "\n"
f.write(message)
print("Wrote record to file")
def load_lookup_table(file):
"""Return a dictionary of term-vector pairs"""
return pkl.load(open(file, "rb"))
def read_topic_file(topic_file_path,topic_list):
"""Read a TREC topic file and parse it into a dictionary"""
f = open(topic_file_path,'r')
for line in f:
topic_id = line.split()[0]
original_query = " ".join(line.split()[1::])
topic_list[topic_id] = original_query
def retrieve_document_terms(query):
"""
:param query: A query to pass to the search engine
:return: a dictionary of term-Word2Vec embedding pairs, in the order the terms appear in the collection.
"""
tokens = []
results = atire.lookup(-1, query)
for result in results:
tokens.append(atire.get_ordered_tokens(result))
return tokens
gamma = 0.99
def discount_rewards(r):
""" take 1D float array of rewards and compute discounted reward """
discounted_r = np.zeros_like(r)
running_add = 0
for t in reversed(range(0, r.size)):
running_add = running_add * gamma + r[t]
discounted_r[t] = running_add
return discounted_r
class GenerateNetwork:
def __init__(self,number_of_terms):
self.query_input = tf.placeholder(tf.float32, [None, WORD_VECTOR_DIMENSIONS], name="query_input")
self.candidate_and_context_input = tf.placeholder(tf.float32, [None, WORD_VECTOR_DIMENSIONS]
, name="candidate_vectors")
self.action_choice = tf.placeholder(tf.int32,[None,1],name="actions")
# Reshaping the query so it becomes Rank 4, the order is [batch_size, width,height, channel]
self.reshaped_query_input = tf.reshape(self.query_input, [-1, number_of_terms, WORD_VECTOR_DIMENSIONS, 1])
self.reshaped_candidate_and_context = tf.reshape(self.candidate_and_context_input,
[-1, CANDIDATE_AND_CONTEXT_LENGTH, WORD_VECTOR_DIMENSIONS, 1])
# Add 2 convolutional layers with ReLu activation
with tf.variable_scope("policy", reuse=tf.AUTO_REUSE):
self.query_conv1 = slim.conv2d(
self.reshaped_query_input, num_outputs=256,
kernel_size=[3,WORD_VECTOR_DIMENSIONS], stride=[1,1], padding='VALID', biases_initializer=tf.constant_initializer(0.1)
)
# Second convolution layer
self.query_conv2 = slim.conv2d(
self.query_conv1, num_outputs=256,
kernel_size=[3,1], stride=[1,1], padding='VALID', biases_initializer=tf.constant_initializer(0.1)
)
# Not super confident about these parameters, may need revisit
self.query_pooled = tf.nn.max_pool(
self.query_conv2,
ksize=[1,11,1,1],
strides=[1, 1, 1,1],
padding='VALID',
name="pool")
self.candidates_conv1 = slim.conv2d(
self.reshaped_candidate_and_context, num_outputs=256,
kernel_size=[5,WORD_VECTOR_DIMENSIONS], stride=[1,1], padding='VALID', biases_initializer=tf.constant_initializer(0.1)
)
self.candidates_conv2 = slim.conv2d(
self.candidates_conv1, num_outputs=256,
kernel_size=[3,1], stride=[1,1], padding='VALID', biases_initializer=tf.constant_initializer(0.1)
)
self.candidates_pooled = tf.nn.max_pool(
self.candidates_conv2,
ksize=[1, 3, 1, 1],
strides=[1, 1, 1, 1],
padding='VALID',
name="pool")
self.pooled_vectors_concatenated = tf.concat([self.query_pooled, self.candidates_pooled], 3)
self.policy_fc1 = tf.contrib.layers.fully_connected(tf.reshape(self.pooled_vectors_concatenated,[-1,512]),
num_outputs=256,activation_fn=tf.nn.tanh,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=tf.constant_initializer(0.001))
self.aprob = slim.fully_connected(self.policy_fc1,1,biases_initializer=tf.constant_initializer(0.001),activation_fn=tf.nn.sigmoid)
self.mean_context_vector = tf.reduce_mean(self.candidates_pooled,axis=0)
self.mean_context_and_query_concatenated = tf.concat((tf.reduce_mean(self.query_pooled,axis=0),self.mean_context_vector),axis=2)
with tf.variable_scope("value",reuse=tf.AUTO_REUSE):
self.value_fc1 = tf.contrib.layers.fully_connected(tf.reshape(self.mean_context_and_query_concatenated,[1,512]),
num_outputs=256,activation_fn=tf.nn.tanh,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=tf.constant_initializer(0.01))
self.value_prediction = tf.contrib.layers.fully_connected(self.value_fc1,num_outputs=1,activation_fn=tf.nn.sigmoid,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=tf.constant_initializer(0.01))
self.value = tf.squeeze(self.value_prediction)
# Using the same training parameters from the paper
self.optimizer = tf.train.AdamOptimizer(learning_rate=1e-6,beta1=0.9,beta2=0.999,epsilon=1e-8)
# Update the parameters according to the computed gradient.
# train_step = optimizer.minimize(loss)
# self.action_holder = tf.placeholder(shape=[None],dtype=tf.int32)
self.reward = tf.placeholder(shape=[],dtype=tf.float32)
self.predicted_reward = tf.placeholder(shape=[],dtype=tf.float32)
self.policy_loss = (self.reward - self.predicted_reward) * -tf.reduce_sum(tf.to_float(self.action_choice) * tf.log(self.aprob) +
(1.0-tf.to_float(self.action_choice))*tf.log(1.0-self.aprob))
# self.policy_loss = self.loss((self.reward-self.predicted_reward))
vars = tf.trainable_variables()
self.value_fc_variables = [v for v in vars if v.name.startswith("value")]
self.policy_weights = [v for v in vars if v.name.startswith("policy")]
self.value_loss = 0.01 * tf.square((self.reward - self.value_prediction))
self.train_policy_network = self.optimizer.minimize(loss=self.policy_loss,var_list=[self.policy_weights])
# self.value_loss = 0.1 * tf.nn.l2_loss(self.reward - self.value_prediction)
self.train_value_network = self.optimizer.minimize(self.value_loss,
var_list=[self.value_fc_variables])
def lookup_term_vectors(terms):
"""
Looks up the wordembedding for a set of terms,
and returns a numpy array version of the vectors to be used in the model.
:param query: the query to search for in the lookup table
:return: a numpy ndarray, each entry corresponding to a term in the set.
"""
query = []
word_vector = []
# query_terms.append([x for x in term.split(" ")])
query.append(re.sub(r'\W+', " ", terms).lower())
for terms in query:
for word in terms.split(" "):
# TODO: Retrain Word2Vec and remove this line
if word not in word_embedding.wv.vocab:
print (word)
word_vector.append(np.zeros(shape=WORD_VECTOR_DIMENSIONS))
else:
word_vector.append(word_embedding.wv[word])
if len(word_vector) < MAX_SEQUENCE_LENGTH:
diff = MAX_SEQUENCE_LENGTH - len(word_vector)
for i in range(0,diff):
word_vector.append(PADDING)
return query,np.array(word_vector)
def window(seq, n=3):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
if __name__ == "__main__":
tf.reset_default_graph() # Clear the Tensorflow graph.
word_embedding = gensim.models.Word2Vec.load(WORD_EMBEDDING_PATH)
querys_table = {}
read_topic_file(TOPIC_FILE,querys_table)
network = GenerateNetwork(number_of_terms=MAX_SEQUENCE_LENGTH)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
if load_model:
saver.restore(sess, weight_saving_path)
print("Model loaded successfully")
else:
sess.run(init)
token_cache = {}
result_cache = {}
for episode in range(0, 500000):
for topicID in querys_table:
current_query_term_list = querys_table[topicID]
# This is the input to the left half of the neural network
current_query, query_vectors = lookup_term_vectors(current_query_term_list)
# Get a list of all the words in the top 10 documents
if "".join(current_query) not in token_cache.keys():
terms_in_results = retrieve_document_terms(" ".join(current_query))
terms_in_query = current_query[0].split(" ")
terms_in_results.insert(0, terms_in_query)
terms_in_results = terms_in_results[:6]
token_cache["".join(current_query)] = tuple(terms_in_results)
else:
terms_in_results = token_cache["".join(current_query)]
# current_query = "South African"
reformulated_query = []
ep_history = []
candidate_terms = []
actions = []
# terms_in_results = [['60','50','sanction','bus','europe','50','bus','china','bus']]
# Each document in the top 10 results list
for doc in terms_in_results:
# For each term in one of the documents
for i in range(0,len(doc)):
candidate_and_context = []
candidate_term = (doc[i])
candidate_terms.append(candidate_term)
# This represents the state
candidate_and_context_vectors = []
# pad on the left
if i < CONTEXT_WINDOW:
diff = CONTEXT_WINDOW - i
candidate_and_context = doc[0:i + CONTEXT_WINDOW + 1]
for term in candidate_and_context:
candidate_and_context_vectors.append(word_embedding.wv[term])
for j in range(0,diff):
candidate_and_context.insert(0,"$PADDING$")
candidate_and_context_vectors.insert(0,PADDING)
if (len(candidate_and_context)) < CANDIDATE_AND_CONTEXT_LENGTH:
for j in range(len(candidate_and_context),CANDIDATE_AND_CONTEXT_LENGTH):
candidate_and_context.append("$PADDING$")
candidate_and_context_vectors.append(PADDING)
# pad on the right---
elif (len(doc) - (i+1)) < CONTEXT_WINDOW:
# TODO: A known issue here: '' seperation at the end of each document is counted as a valid term, this may require fixing if it causes a problem in query reformulation.
diff = CONTEXT_WINDOW - (len(doc) - (i+1))
candidate_and_context = doc[i-CONTEXT_WINDOW:len(doc)]
for term in candidate_and_context:
candidate_and_context_vectors.append(word_embedding.wv[term])
for j in range(0, diff):
candidate_and_context.append("$PADDING$")
candidate_and_context_vectors.append(PADDING)
if (len(candidate_and_context)) < CANDIDATE_AND_CONTEXT_LENGTH:
for j in range(len(candidate_and_context), CANDIDATE_AND_CONTEXT_LENGTH):
candidate_and_context.insert(0, "$PADDING$")
candidate_and_context_vectors.insert(0, PADDING)
# No padding, sliding window in normal range
else:
candidate_and_context = doc[i-CONTEXT_WINDOW:i + CONTEXT_WINDOW + 1]
for term in candidate_and_context:
candidate_and_context_vectors.append(word_embedding.wv[term])
if eps > end_eps:
eps -= stepDrop
r = random.random()
a_prob = None
if r < eps:
a_prob = randint(0,1)
else:
a_prob = sess.run(network.aprob, feed_dict={network.query_input: query_vectors,
network.candidate_and_context_input: candidate_and_context_vectors})
a = 0
if a_prob > 0.5:
a = 1
else:
a = 0
actions.append(a)
ep_history.append([query_vectors, candidate_and_context_vectors, a])
ep_history = np.array(ep_history)
for i in range(0, len(actions)):
if actions[i] == 1:
reformulated_query.append(candidate_terms[i])
reformulated_query = " ".join(reformulated_query)
print("reformulated: ", reformulated_query)
if topicID not in result_cache.keys():
result_cache[topicID] = {}
if reformulated_query not in result_cache[topicID].keys():
reward = atire.lookup(int(topicID), reformulated_query)
result_cache[topicID][reformulated_query] = reward
else:
reward = result_cache[topicID][reformulated_query]
predicted_reward = sess.run(network.value_prediction,feed_dict={network.query_input:np.vstack(ep_history[:,0]),
network.candidate_and_context_input: np.vstack(
ep_history[:, 1])})
value_loss, _ = sess.run([network.value_loss,network.train_value_network],feed_dict={network.query_input:np.vstack(ep_history[:,0]),
network.candidate_and_context_input:np.vstack(ep_history[:,1]),
network.reward:reward[0], network.predicted_reward : np.squeeze(predicted_reward)
})
print("reward: " + str(reward))
print("predicted reward: " + str(predicted_reward))
policy_loss, _ = sess.run([network.policy_loss,network.train_policy_network],feed_dict={network.query_input:np.vstack(ep_history[:,0]),
network.candidate_and_context_input:np.vstack(ep_history[:,1]),
network.reward:reward[0], network.predicted_reward : np.squeeze(predicted_reward),
network.action_choice:np.vstack(ep_history[:,2])
})
print("policy loss:{}, value loss {} ".format(policy_loss,value_loss))
if episode % 50 == 0:
# info = [
# ["episode: ", episode],
# ["average precision@40: ",reward],
# ["predicted reward: ", predicted_reward]
# ]
# write_to_file(fitness_record, info )
save_path = saver.save(sess, weight_saving_path)
print("Model saved in path: %s" % save_path)
if episode % TEST_FREQUENCY == 0:
# test begins here
print("testing....")
average_precisions = []
for topicID in querys_table:
current_query_term_list = querys_table[topicID]
# This is the input to the left half of the neural network
current_query, query_vectors = lookup_term_vectors(current_query_term_list)
terms_in_results = token_cache["".join(current_query)]
actions = []
reformulated_query = []
# terms_in_results = [['60','50','sanction','bus','europe','50','bus','china','bus']]
# Each document in the top 10 results list
for doc in terms_in_results:
# For each term in one of the documents
for i in range(0, len(doc)):
candidate_and_context = []
candidate_term = (doc[i])
candidate_terms.append(candidate_term)
# This represents the state
candidate_and_context_vectors = []
# pad on the left
if i < CONTEXT_WINDOW:
diff = CONTEXT_WINDOW - i
candidate_and_context = doc[0:i + CONTEXT_WINDOW + 1]
for term in candidate_and_context:
candidate_and_context_vectors.append(word_embedding.wv[term])
for j in range(0, diff):
candidate_and_context.insert(0, "$PADDING$")
candidate_and_context_vectors.insert(0, PADDING)
if (len(candidate_and_context)) < CANDIDATE_AND_CONTEXT_LENGTH:
for j in range(len(candidate_and_context), CANDIDATE_AND_CONTEXT_LENGTH):
candidate_and_context.append("$PADDING$")
candidate_and_context_vectors.append(PADDING)
# pad on the right---
elif (len(doc) - (i + 1)) < CONTEXT_WINDOW:
# TODO: A known issue here: '' seperation at the end of each document is counted as a valid term, this may require fixing if it causes a problem in query reformulation.
diff = CONTEXT_WINDOW - (len(doc) - (i + 1))
candidate_and_context = doc[i - CONTEXT_WINDOW:len(doc)]
for term in candidate_and_context:
candidate_and_context_vectors.append(word_embedding.wv[term])
for j in range(0, diff):
candidate_and_context.append("$PADDING$")
candidate_and_context_vectors.append(PADDING)
if (len(candidate_and_context)) < CANDIDATE_AND_CONTEXT_LENGTH:
for j in range(len(candidate_and_context), CANDIDATE_AND_CONTEXT_LENGTH):
candidate_and_context.insert(0, "$PADDING$")
candidate_and_context_vectors.insert(0, PADDING)
# No padding, sliding window in normal range
else:
candidate_and_context = doc[i - CONTEXT_WINDOW:i + CONTEXT_WINDOW + 1]
for term in candidate_and_context:
candidate_and_context_vectors.append(word_embedding.wv[term])
a_prob = sess.run(network.aprob, feed_dict={network.query_input: query_vectors,
network.candidate_and_context_input: candidate_and_context_vectors})
if a_prob > 0.5:
a = 1
else:
a = 0
actions.append(a)
for i in range(0, len(actions)):
if actions[i] == 1:
reformulated_query.append(candidate_terms[i])
reformulated_query = " ".join(reformulated_query)
print("reformulated query at test time: ", reformulated_query)
if reformulated_query not in result_cache[topicID].keys():
reward = atire.lookup(int(topicID), reformulated_query)
result_cache[topicID][reformulated_query] = reward
else:
reward = result_cache[topicID][reformulated_query]
print("average precision for test time reformulated query: ", reward)
info = [
["episode: ", episode],
["reward : ", reward],
["reformulated query ", reformulated_query],
]
write_to_file(reformulated_query_filepath,info)
average_precisions.append(reward)
average_precisions = np.array(average_precisions)
mean_average_precision = np.mean(average_precisions)
print("Mean average precision: ", mean_average_precision)
info = [
["episode: ", episode],
["MAP@40: ",mean_average_precision],
]
write_to_file(test_time_MAP, info )