-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn2.py
executable file
·874 lines (701 loc) · 30.9 KB
/
dqn2.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
#!/usr/bin/env python3
import os
import sys
import numpy as np
import argparse
import hashlib
import tensorflow as tf
from tldextract import extract
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
import pylab as plt
from common import MySQL, Languages, Timer
from helpers import GetEnvs, Env, Link
######################################################################################
class LearningParams:
def __init__(self, languages, saveDir, saveDirPlots, deleteDuplicateTransitions, langPair, maxLangId, defaultLang):
self.gamma = 0.99
self.lrn_rate = 0.1
self.alpha = 1.0 # 0.7
self.max_epochs = 100001
self.eps = 0.1
self.maxBatchSize = 1
self.minCorpusSize = 200
self.overSampling = 1
self.debug = False
self.walk = 2
self.NUM_ACTIONS = 30
self.FEATURES_PER_ACTION = 1
self.saveDir = saveDir
self.saveDirPlots = saveDirPlots
self.deleteDuplicateTransitions = deleteDuplicateTransitions
self.reward = 100.0 #17.0
self.cost = -1.0
self.unusedActionCost = 0.0 #-555.0
self.maxDocs = 9999999999
self.maxLangId = maxLangId
self.defaultLang = defaultLang
self.MAX_NODES = 50
langPairList = langPair.split(",")
assert(len(langPairList) == 2)
self.langIds = np.empty([1,2], dtype=np.int32)
self.langIds[0,0] = languages.GetLang(langPairList[0])
self.langIds[0,1] = languages.GetLang(langPairList[1])
#print("self.langs", self.langs)
######################################################################################
def NumParallelDocs(env, visited):
ret = 0
for urlId in visited:
node = env.nodes[urlId]
#print("node", node.Debug())
if node.alignedNode is not None and node.alignedNode.urlId in visited:
ret += 1
return ret
######################################################################################
def dumb(env, maxDocs, params):
ret = []
todo = []
todo.append(env.rootNode)
visited = set()
langsVisited = {}
while len(todo) > 0 and len(visited) < maxDocs:
node = todo.pop(0)
#print("node", node.Debug())
if node.urlId not in visited:
visited.add(node.urlId)
if node.lang not in langsVisited:
langsVisited[node.lang] = 0
langsVisited[node.lang] += 1
if params.debug and len(visited) % 40 == 0:
print(" langsVisited", langsVisited)
for link in node.links:
childNode = link.childNode
#print(" ", childNode.Debug())
todo.append(childNode)
numParallelDocs = NumParallelDocs(env, visited)
ret.append(numParallelDocs)
return ret
######################################################################################
def randomCrawl(env, maxDocs, params):
ret = []
todo = []
todo.append(env.rootNode)
visited = set()
langsVisited = {}
while len(todo) > 0 and len(visited) < maxDocs:
idx = np.random.randint(0, len(todo))
node = todo.pop(idx)
#print("node", node.Debug())
if node.urlId not in visited:
visited.add(node.urlId)
if node.lang not in langsVisited:
langsVisited[node.lang] = 0
langsVisited[node.lang] += 1
if params.debug and len(visited) % 40 == 0:
print(" langsVisited", langsVisited)
for link in node.links:
childNode = link.childNode
#print(" ", childNode.Debug())
todo.append(childNode)
numParallelDocs = NumParallelDocs(env, visited)
ret.append(numParallelDocs)
return ret
######################################################################################
def balanced(env, maxDocs, params):
ret = []
visited = set()
langsVisited = {}
langsTodo = {}
startNode = env.nodes[sys.maxsize]
#print("startNode", startNode.Debug())
assert(len(startNode.links) == 1)
link = next(iter(startNode.links))
while link is not None and len(visited) < maxDocs:
node = link.childNode
if node.urlId not in visited:
#print("node", node.Debug())
visited.add(node.urlId)
if node.lang not in langsVisited:
langsVisited[node.lang] = 0
langsVisited[node.lang] += 1
if params.debug and len(visited) % 40 == 0:
print(" langsVisited", langsVisited)
for link in node.links:
#print(" ", childNode.Debug())
AddTodo(langsTodo, visited, link)
numParallelDocs = NumParallelDocs(env, visited)
ret.append(numParallelDocs)
link = PopLink(langsTodo, langsVisited, params)
return ret
def PopLink(langsTodo, langsVisited, params):
sum = 0
# any nodes left to do
for links in langsTodo.values():
sum += len(links)
if sum == 0:
return None
del sum
# sum of all nodes visited
sumAll = 0
sumRequired = 0
for lang, count in langsVisited.items():
sumAll += count
if lang in params.langIds:
sumRequired += count
sumRequired += 0.001 #1
#print("langsVisited", sumAll, sumRequired, langsVisited)
probs = {}
for i in range(params.langIds.shape[1]):
lang = params.langIds[0, i]
#print("lang", lang)
if lang in langsVisited:
count = langsVisited[lang]
else:
count = 0
#print("langsTodo", lang, nodes)
prob = 1.0 - float(count) / float(sumRequired)
probs[lang] = prob
#print(" probs", probs)
links = None
rnd = np.random.rand(1)
#print("rnd", rnd, len(probs))
cumm = 0.0
for lang, prob in probs.items():
cumm += prob
#print("prob", prob, cumm)
if cumm > rnd[0]:
if lang in langsTodo:
links = langsTodo[lang]
break
if links is not None and len(links) > 0:
link = links.pop(0)
else:
link = RandomLink(langsTodo)
#print(" node", node.Debug())
return link
def RandomLink(langsTodo):
while True:
idx = np.random.randint(0, len(langsTodo))
langs = list(langsTodo.keys())
lang = langs[idx]
links = langsTodo[lang]
#print("idx", idx, len(nodes))
if len(links) > 0:
return links.pop(0)
raise Exception("shouldn't be here")
def AddTodo(langsTodo, visited, link):
childNode = link.childNode
if childNode.urlId in visited:
return
parentNode = link.parentNode
parentLang = parentNode.lang
if parentLang not in langsTodo:
langsTodo[parentLang] = []
langsTodo[parentLang].append(link)
######################################################################################
######################################################################################
def SavePlots(sess, qns, params, envs, saveDirPlots, epoch, sset):
for env in envs:
SavePlot(sess, qns, params, env, saveDirPlots, epoch, sset)
######################################################################################
def SavePlot(sess, qns, params, env, saveDirPlots, epoch, sset):
print("Walking", env.rootURL)
arrDumb = dumb(env, len(env.nodes), params)
arrRandom = randomCrawl(env, len(env.nodes), params)
arrBalanced = balanced(env, len(env.nodes), params)
arrRL = Walk(env, params, sess, qns)
url = env.rootURL
domain = extract(url).domain
avgRandom = avgBalanced = avgRL = 0.0
for i in range(len(arrDumb)):
if arrDumb[i] > 0:
avgRandom += arrRandom[i] / arrDumb[i]
avgBalanced += arrBalanced[i] / arrDumb[i]
#print("arrRL", arrRL[i], arrDumb[i])
avgRL += arrRL[i] / arrDumb[i]
avgRandom = avgRandom / len(arrDumb)
avgBalanced = avgBalanced / len(arrDumb)
avgRL = avgRL / len(arrDumb)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(arrDumb, label="dumb ", color='maroon')
ax.plot(arrRandom, label="random {0:.1f}".format(avgRandom), color='firebrick')
ax.plot(arrBalanced, label="balanced {0:.1f}".format(avgBalanced), color='red')
ax.plot(arrRL, label="RL {0:.1f}".format(avgRL), color='salmon')
ax.legend(loc='upper left')
plt.xlabel('#crawled')
plt.ylabel('#found')
plt.title("{sset} {domain}".format(sset=sset, domain=domain))
fig.savefig("{dir}/{sset}-{domain}-{epoch}.png".format(dir=saveDirPlots, sset=sset, domain=domain, epoch=epoch))
fig.show()
######################################################################################
class Qnets():
def __init__(self, params):
self.q = []
self.q.append(Qnetwork(params))
self.q.append(Qnetwork(params))
######################################################################################
class Corpus:
def __init__(self, params, qn):
self.params = params
self.qn = qn
self.transitions = []
self.losses = []
def AddTransition(self, transition):
if self.params.deleteDuplicateTransitions:
for currTrans in self.transitions:
if currTrans.currURLId == transition.currURLId and currTrans.nextURLId == transition.nextURLId:
return
# completely new trans
self.transitions.append(transition)
def GetBatchWithoutDelete(self, maxBatchSize):
batch = []
size = len(self.transitions)
for i in range(maxBatchSize):
idx = np.random.randint(0, size)
transition = self.transitions[idx]
batch.append(transition)
return batch
def Train(self, sess, params):
if len(self.transitions) >= params.minCorpusSize:
#for transition in self.transitions:
# print(DebugTransition(transition))
numIter = len(self.transitions) * params.overSampling / params.maxBatchSize
numIter = int(numIter) + 1
#print("numIter", numIter, len(self.transitions), params.overSampling, params.maxBatchSize)
for i in range(numIter):
batch = self.GetBatchWithoutDelete(params.maxBatchSize)
loss = self.UpdateQN(params, sess, batch)
self.losses.append(loss)
self.transitions.clear()
def UpdateQN(self, params, sess, batch):
batchSize = len(batch)
#print("batchSize", batchSize)
numLangs = np.empty([batchSize, 1], dtype=np.int)
langRequested = np.empty([batchSize, self.params.MAX_NODES], dtype=np.int)
mask = np.empty([batchSize, self.params.MAX_NODES], dtype=np.bool)
langIds = np.empty([batchSize, 2], dtype=np.int)
langsVisited = np.empty([batchSize, params.maxLangId + 1])
targetQ = np.empty([batchSize, self.params.MAX_NODES])
i = 0
for transition in batch:
#curr = transition.curr
#next = transition.next
assert(transition.numLangs == transition.targetQ.shape[1])
numLangs[i, 0] = transition.numLangs
langRequested[i, :] = transition.langRequested
mask[i, :] = transition.mask
langIds[i, :] = transition.langIds
langsVisited[i, :] = transition.langsVisited
targetQ[i, 0:transition.numLangs] = transition.targetQ
i += 1
#_, loss, sumWeight = sess.run([qn.updateModel, qn.loss, qn.sumWeight], feed_dict={qn.input: childIds, qn.nextQ: targetQ})
TIMER.Start("UpdateQN.1")
loss = self.qn.Update(sess, numLangs, langRequested, mask, langIds, langsVisited, targetQ)
TIMER.Pause("UpdateQN.1")
#print("loss", loss)
return loss
######################################################################################
class Transition:
def __init__(self, currURLId, nextURLId, numLangs, langRequested, mask, langIds, langsVisited, targetQ):
self.currURLId = currURLId
self.nextURLId = nextURLId
self.numLangs = numLangs
self.langRequested = np.array(langRequested, copy=True)
self.mask = np.array(mask, copy=True)
self.langIds = langIds
self.langsVisited = np.array(langsVisited, copy=True)
self.targetQ = targetQ
def DebugTransition(self):
ret = str(self.currURLId) + "->" + str(self.nextURLId)
return ret
######################################################################################
class Candidates:
def __init__(self, params, env):
self.params = params
self.env = env
self.dict = {} # parent lang -> links[]
#for langId in params.langIds:
# self.dict[langId] = []
def copy(self):
ret = Candidates(self.params, self.env)
for key, value in self.dict.items():
#print("key", key, value)
ret.dict[key] = value.copy()
return ret
def AddLink(self, link):
langId = link.parentNode.lang
if langId not in self.dict:
self.dict[langId] = []
self.dict[langId].append(link)
def AddLinks(self, node, visited, params):
#print(" currNode", curr, currNode.Debug())
newLinks = node.GetLinks(visited, params)
for link in newLinks:
self.AddLink(link)
def Pop(self, lang):
links = self.dict[lang]
assert(len(links) > 0)
idx = np.random.randint(0, len(links))
link = links.pop(idx)
# remove all links going to same node
for otherLinks in self.dict.values():
otherLinksCopy = otherLinks.copy()
for otherLink in otherLinksCopy:
if otherLink.childNode == link.childNode:
otherLinks.remove(otherLink)
return link
def HasLinks(self, lang):
if lang in self.dict and len(self.dict[lang]) > 0:
return True
else:
return False
def Count(self):
ret = 0
for _, dict in self.dict.items():
ret += len(dict)
return ret
def GetFeatures(self):
numLangs = 0
langRequested = np.zeros([1, self.params.MAX_NODES], dtype=np.int32)
mask = np.full([1, self.params.MAX_NODES], False, dtype=np.bool)
for langId, nodes in self.dict.items():
if len(nodes) > 0:
assert(numLangs < langRequested.shape[1])
langRequested[0, numLangs] = langId
mask[0, numLangs] = True
numLangs += 1
return numLangs, langRequested, mask
def Debug(self):
ret = ""
for lang in self.dict:
ret += "lang=" + str(lang) + ":" + str(len(self.dict[lang])) + " "
#links = self.dict[lang]
#for link in links:
# ret += " " + link.parentNode.url + "->" + link.childNode.url
return ret
######################################################################################
class Qnetwork():
def __init__(self, params):
self.params = params
self.corpus = Corpus(params, self)
HIDDEN_DIM = 512
NUM_FEATURES = params.maxLangId + 1
# EMBEDDINGS
self.embeddings = tf.Variable(tf.random_uniform([params.maxLangId + 1, HIDDEN_DIM], 0, 0.01))
# mask
self.mask = tf.placeholder(shape=[None, self.params.MAX_NODES], dtype=tf.bool)
# graph represention
self.langIds = tf.placeholder(shape=[None, 2], dtype=tf.float32)
self.langsVisited = tf.placeholder(shape=[None, NUM_FEATURES], dtype=tf.float32)
self.numLangs = tf.placeholder(shape=[None, 1], dtype=tf.int32)
# link representation
self.langRequested = tf.placeholder(shape=[None, self.params.MAX_NODES], dtype=tf.int32)
# batch size
self.batchSize = tf.shape(self.langRequested)[0]
# network
self.numLangsFloat32 = tf.cast(self.numLangs, dtype=tf.float32)
self.input = tf.concat([self.langIds, self.langsVisited, self.numLangsFloat32], 1)
#print("self.input", self.input.shape)
self.W1 = tf.Variable(tf.random_uniform([NUM_FEATURES + 3, HIDDEN_DIM], 0, 0.01))
self.b1 = tf.Variable(tf.random_uniform([1, HIDDEN_DIM], 0, 0.01))
self.hidden1 = tf.matmul(self.input, self.W1)
self.hidden1 = tf.add(self.hidden1, self.b1)
self.hidden1 = tf.nn.relu(self.hidden1)
#print("self.hidden1", self.hidden1.shape)
self.W2 = tf.Variable(tf.random_uniform([HIDDEN_DIM, HIDDEN_DIM], 0, 0.01))
self.b2 = tf.Variable(tf.random_uniform([1, HIDDEN_DIM], 0, 0.01))
self.hidden2 = tf.matmul(self.hidden1, self.W2)
self.hidden2 = tf.add(self.hidden2, self.b2)
self.hidden2 = tf.nn.relu(self.hidden2)
#print("self.hidden2", self.hidden2.shape)
self.W3 = tf.Variable(tf.random_uniform([HIDDEN_DIM, HIDDEN_DIM], 0, 0.01))
self.b3 = tf.Variable(tf.random_uniform([1, HIDDEN_DIM], 0, 0.01))
self.hidden3 = tf.matmul(self.hidden2, self.W3)
self.hidden3 = tf.add(self.hidden3, self.b3)
#self.hidden3 = tf.nn.relu(self.hidden3)
#print("self.hidden3", self.hidden3.shape)
# link-specific
self.hidden3 = tf.transpose(self.hidden3)
self.langRequestedEmbedding = tf.nn.embedding_lookup(self.embeddings, self.langRequested)
self.langRequestedEmbedding = tf.reshape(self.langRequestedEmbedding, [self.batchSize * self.params.MAX_NODES, HIDDEN_DIM])
#print("self.langRequested", self.langRequested.shape, self.langRequestedEmbedding)
self.hidden3 = tf.matmul(self.langRequestedEmbedding, self.hidden3)
self.hidden3 = tf.transpose(self.hidden3)
self.qValues = tf.boolean_mask(self.hidden3, self.mask, axis=0)
#self.hidden3 = tf.math.reduce_sum(self.hidden3, axis=1)
#self.qValues = self.hidden3
#print("self.qValues", self.qValue.shapes)
# Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
self.nextQ = tf.placeholder(shape=[None, self.params.MAX_NODES], dtype=tf.float32)
self.nextQMasked = tf.boolean_mask(self.nextQ, self.mask, axis=0)
self.loss = self.nextQMasked - self.qValues
self.loss = tf.reduce_sum(tf.square(self.loss))
#self.trainer = tf.train.GradientDescentOptimizer(learning_rate=lrn_rate)
self.trainer = tf.train.AdamOptimizer() #learning_rate=lrn_rate)
self.updateModel = self.trainer.minimize(self.loss)
self.sumWeight = tf.reduce_sum(self.W1) \
+ tf.reduce_sum(self.b1) \
+ tf.reduce_sum(self.W2) \
+ tf.reduce_sum(self.b2) \
+ tf.reduce_sum(self.W3) \
+ tf.reduce_sum(self.b3)
def PredictAll(self, env, sess, langIds, langsVisited, candidates):
numLangs, langRequested, mask = candidates.GetFeatures()
numLangsNP = np.empty([1,1], dtype=np.int32)
numLangsNP[0,0] = numLangs
if numLangs > 0:
#print("langRequested", langRequested.shape, langRequested)
#print("mask", mask.shape, mask)
qValues, hidden3 = sess.run([self.qValues, self.hidden3],
feed_dict={self.langRequested: langRequested,
self.numLangs: numLangsNP,
self.mask: mask,
self.langIds: langIds,
self.langsVisited: langsVisited})
#qValues = qValues[0]
#print("hidden3", hidden3.shape, hidden3)
#print("qValues", qValues.shape, qValues)
qValues = np.reshape(qValues, [1, qValues.shape[0] ])
#print(" qValues", qValues)
action = np.argmax(qValues[0, :numLangs])
maxQ = qValues[0, action]
#print("newAction", action, maxQ)
else:
maxQ = 0.0 #-99999.0
action = -1
qValues = np.zeros([1, self.params.MAX_NODES], dtype=np.float32)
#print("qValues", qValues.shape, qValues, action, maxQ)
return numLangs, langRequested, mask, qValues, maxQ, action
def Update(self, sess, numLangs, langRequested, mask, langIds, langsVisited, targetQ):
#print("input", langRequested.shape, langIds.shape, langFeatures.shape, targetQ.shape)
#print("targetQ", targetQ)
_, loss = sess.run([self.updateModel, self.loss],
feed_dict={self.langRequested: langRequested,
self.numLangs: numLangs,
self.mask: mask,
self.langIds: langIds,
self.langsVisited: langsVisited,
self.nextQ: targetQ})
#print("loss", loss)
return loss
######################################################################################
def GetNextState(env, params, action, visited, candidates, langRequested):
#print("candidates", action, candidates.Debug())
if action == -1:
# no explicit stop state but no candidates
stopNode = env.nodes[0]
link = Link("", 0, stopNode, stopNode)
else:
langId = langRequested[0, action]
assert(candidates.HasLinks(langId))
link = candidates.Pop(langId)
assert(link is not None)
nextNode = link.childNode
#print(" nextNode", nextNode.Debug())
if nextNode.urlId == 0:
#print(" stop")
reward = 0.0
elif nextNode.alignedNode is not None and nextNode.alignedNode.urlId in visited:
reward = params.reward
#print(" visited", visited)
#print(" reward", reward)
#print()
else:
#print(" non-rewarding")
reward = params.cost
return link, reward
######################################################################################
def NeuralWalk(env, params, eps, candidates, visited, langsVisited, sess, qnA):
numLangs, langRequested, mask, qValues, maxQ, action = qnA.PredictAll(env, sess, params.langIds, langsVisited, candidates)
#print("action", action, langRequested, qValues)
if action >= 0:
if np.random.rand(1) < eps:
#print("actions", type(actions), actions)
action = np.random.randint(0, numLangs)
maxQ = qValues[0, action]
#print("random")
#print("action", action, qValues)
#print("action", action, maxQ, qValues)
link, reward = GetNextState(env, params, action, visited, candidates, langRequested)
assert(link is not None)
#print("action", action, qValues, link.childNode.Debug(), reward)
return numLangs, langRequested, mask, qValues, maxQ, action, link, reward
######################################################################################
def Neural(env, params, candidates, visited, langsVisited, sess, qnA, qnB):
numLangs, langRequested, mask, qValues, maxQ, action, link, reward = NeuralWalk(env, params, params.eps, candidates, visited, langsVisited, sess, qnA)
assert(link is not None)
# calc nextMaxQ
nextVisited = visited.copy()
nextVisited.add(link.childNode.urlId)
nextCandidates = candidates.copy()
nextCandidates.AddLinks(link.childNode, nextVisited, params)
nextLangsVisited = langsVisited.copy()
nextLangsVisited[0, link.childNode.lang] += 1
if nextCandidates.Count() > 0:
_, _, _, _, _, nextAction = qnA.PredictAll(env, sess, params.langIds, nextLangsVisited, nextCandidates)
#print("nextAction", nextAction, nextLangRequested, nextCandidates.Debug())
_, _, _, nextQValuesB, _, _ = qnB.PredictAll(env, sess, params.langIds, nextLangsVisited, nextCandidates)
nextMaxQ = nextQValuesB[0, nextAction]
#print("nextMaxQ", nextMaxQ, nextMaxQB, nextQValuesA[0, nextAction])
else:
nextMaxQ = 0
newVal = reward + params.gamma * nextMaxQ
targetQ = (1 - params.alpha) * maxQ + params.alpha * newVal
qValues[0, action] = targetQ
transition = Transition(link.parentNode.urlId,
link.childNode.urlId,
numLangs,
langRequested,
mask,
params.langIds,
langsVisited,
qValues)
return transition
######################################################################################
def Trajectory(env, epoch, params, sess, qns):
ret = []
visited = set()
langsVisited = np.zeros([1, params.maxLangId + 1]) # langId -> count
candidates = Candidates(params, env)
node = env.nodes[sys.maxsize]
#stopNode = env.nodes[0]
#link = Link("", 0, stopNode, stopNode)
#candidates.AddLink(link)
while True:
tmp = np.random.rand(1)
if tmp > 0.5:
qnA = qns.q[0]
qnB = qns.q[1]
else:
qnA = qns.q[1]
qnB = qns.q[0]
assert(node.urlId not in visited)
#print("node", node.Debug())
visited.add(node.urlId)
langsVisited[0, node.lang] += 1
#print(" langsVisited", langsVisited)
candidates.AddLinks(node, visited, params)
numParallelDocs = NumParallelDocs(env, visited)
ret.append(numParallelDocs)
transition = Neural(env, params, candidates, visited, langsVisited, sess, qnA, qnB)
if transition.nextURLId == 0:
break
else:
qnA.corpus.AddTransition(transition)
node = env.nodes[transition.nextURLId]
if len(visited) > params.maxDocs:
break
return ret
######################################################################################
def Walk(env, params, sess, qns):
ret = []
visited = set()
langsVisited = np.zeros([1, params.maxLangId + 1]) # langId -> count
candidates = Candidates(params, env)
node = env.nodes[sys.maxsize]
#stopNode = env.nodes[0]
#link = Link("", 0, stopNode, stopNode)
#candidates.AddLink(link)
mainStr = "nodes:" + str(node.urlId)
rewardStr = "rewards:"
actionStr = "actions:"
i = 0
numAligned = 0
totReward = 0.0
totDiscountedReward = 0.0
discount = 1.0
while True:
qnA = qns.q[0]
assert(node.urlId not in visited)
#print("node", node.Debug())
visited.add(node.urlId)
#print("node.lang", node.lang, langsVisited.shape)
langsVisited[0, node.lang] += 1
#print(" langsVisited", langsVisited)
candidates.AddLinks(node, visited, params)
numParallelDocs = NumParallelDocs(env, visited)
ret.append(numParallelDocs)
#print("candidates", candidates.Debug())
_, _, _, _, _, action, link, reward = NeuralWalk(env, params, 0.0, candidates, visited, langsVisited, sess, qnA)
node = link.childNode
#print("action", action, qValues)
actionStr += str(action) + " "
totReward += reward
totDiscountedReward += discount * reward
mainStr += "->" + str(node.urlId)
rewardStr += "->" + str(reward)
if node.alignedNode is not None:
mainStr += "*"
numAligned += 1
discount *= params.gamma
i += 1
if node.urlId == 0:
break
if len(visited) > params.maxDocs:
break
mainStr += " " + str(i)
rewardStr += " " + str(totReward) + "/" + str(totDiscountedReward)
print(actionStr)
print(mainStr)
print(rewardStr)
return ret
######################################################################################
def Train(params, sess, saver, qns, envs, envsTest):
totRewards = []
totDiscountedRewards = []
for epoch in range(params.max_epochs):
#print("epoch", epoch)
for env in envs:
TIMER.Start("Trajectory")
_ = Trajectory(env, epoch, params, sess, qns)
TIMER.Pause("Trajectory")
TIMER.Start("Train")
qns.q[0].corpus.Train(sess, params)
qns.q[1].corpus.Train(sess, params)
TIMER.Pause("Train")
if epoch > 0 and epoch % params.walk == 0:
print("epoch", epoch)
SavePlots(sess, qns, params, envs, params.saveDirPlots, epoch, "train")
SavePlots(sess, qns, params, envsTest, params.saveDirPlots, epoch, "test")
return totRewards, totDiscountedRewards
######################################################################################
def main():
global TIMER
TIMER = Timer()
oparser = argparse.ArgumentParser(description="intelligent crawling with q-learning")
oparser.add_argument("--config-file", dest="configFile", required=True,
help="Path to config file (containing MySQL login etc.)")
oparser.add_argument("--language-pair", dest="langPair", required=True,
help="The 2 language we're interested in, separated by ,")
oparser.add_argument("--save-dir", dest="saveDir", default=".",
help="Directory that model WIP are saved to. If existing model exists then load it")
oparser.add_argument("--save-plots", dest="saveDirPlots", default="plot",
help="Directory ")
oparser.add_argument("--delete-duplicate-transitions", dest="deleteDuplicateTransitions",
default=False, help="If True then only unique transition are used in each batch")
options = oparser.parse_args()
np.random.seed()
np.set_printoptions(formatter={'float': lambda x: "{0:0.1f}".format(x)}, linewidth=666)
sqlconn = MySQL(options.configFile)
languages = Languages(sqlconn.mycursor)
params = LearningParams(languages, options.saveDir, options.saveDirPlots, options.deleteDuplicateTransitions, options.langPair, languages.maxLangId, languages.GetLang("None"))
if not os.path.exists(options.saveDirPlots): os.mkdir(options.saveDirPlots)
#hostName = "http://vade-retro.fr/"
hosts = ["http://www.buchmann.ch/"] #, "http://telasmos.org/", "http://tagar.es/"]
#hostName = "http://www.visitbritain.com/"
#hostNameTest = "http://vade-retro.fr/"
#hostNameTest = "http://www.buchmann.ch/"
hostsTest = ["http://www.visitbritain.com/", "http://chopescollection.be/", "http://www.bedandbreakfast.eu/"]
envs = GetEnvs(sqlconn, languages, hosts)
envsTest = GetEnvs(sqlconn, languages, hostsTest)
tf.reset_default_graph()
qns = Qnets(params)
init = tf.global_variables_initializer()
saver = None #tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
totRewards, totDiscountedRewards = Train(params, sess, saver, qns, envs, envsTest)
######################################################################################
main()