-
Notifications
You must be signed in to change notification settings - Fork 0
/
word2vec_thread.py
executable file
·643 lines (479 loc) · 17.4 KB
/
word2vec_thread.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
import os
import numpy as np
import _pickle as pickle
import random as rn
import time
import math as m
import threading
class vocab_word:
#trace from the root
point=[]
#huffman code
code=[]
def __init__(self, word, count):
self.count = count
self.word = word
#object print
def __repr__(self):
return repr(( self.word,self.count))
hs=0
negative=10
table_size=int(1e7)
EXP_TABLE_SIZE=1000
MAX_EXP=6
MAX_SENTENCE_LENGTH=1000
layer_size=200
vocab_size=np.int64(0)
vocab=[]
vocab_dic={}
train_words=np.int64(0)
min_count=5
table=[]
iteration=2
power=0.75
window=7
cbow=1
alpha = 0.025
sample=1e-4
worker=4
_filename_ = 'output.txt'
file_size= os.path.getsize(_filename_)
#making a exponential table
expTable=[]
for i in range(EXP_TABLE_SIZE):
expTable.append(m.exp((float(i)/EXP_TABLE_SIZE*2-1)*MAX_EXP))
expTable[i]=expTable[i]/(expTable[i]+1)
#print expTable[i]
#in case of load
vocab_dic= pickle.load( open( "vocab_dic.p", "rb" ) )
vocab=pickle.load( open( "vocab.p", "rb" ) )
table=pickle.load( open( "table.p", "rb" ) )
vocab_size=len(vocab)
#text8:16718844
train_words=765887525
print(vocab[0].code)
print(vocab[-1].word)
print("vocabulary loaded")
def ReadWord(text_file):
# Reads a single word from a file
# assuming SPACE + TAB + EOL to be word boundaries
a=0
one_word=''
MAX_STRING=100
for one_char in iter(lambda: text_file.read(1), ""):
if(one_char==13):continue
if(one_char==' ' or one_char=='\t' or one_char=='\n'):
if(a>0):
break
if(one_char=='\n'):
return "</s>"
else: continue
a+=1
one_word+=one_char
if(a==MAX_STRING): return one_word
return one_word
def ReadWordIndex(pfile):
word=ReadWord(pfile)
#End of File
if( word==''):return -1
#whether it is the key in the vocab
if(word in vocab_dic):return vocab_dic[word]
#not then word=-2
else: return -2
"""
def AddWordToVocab(word):
global vocab_size
vocab.append(vocab_word(word, 0))
vocab_dic[word]=vocab_size
vocab_size+=1
return vocab_size-1
def SortVocab():
global vocab_size,train_words,vocab
train_words=0
vocab_dic.clear()
vocab=vocab[0:1]+sorted(vocab[1:], key=lambda vocabulary: vocabulary.count,reverse=True)
size=vocab_size
print(len(vocab))
del_idx=0
print(vocab[0].word)
vocab_dic[vocab[0]]=0
#remove unneccessary list element
for i in range(1,size):
if(vocab[i].count<min_count):
del_idx=i
break
else:
word=vocab[i].word
#re build vocab_dict
vocab_dic[word]=i
train_words+=vocab[i].count
vocab[:]=vocab[0:del_idx]
vocab_size=len(vocab)
def LearnVocabFromTrainFile():
global train_words
word=''
global vocab_size
AddWordToVocab("</s>")
print(vocab[0].word)
print("flag1")
with open('text8', 'r') as train_file:
while(1):
word=ReadWord(train_file)
if(word==''):break
train_words+=1
if(vocab_dic.has_key(word)):
vocab[vocab_dic[word]].count+=1
else:
a=AddWordToVocab(word)
vocab[a].count=1
print("flag2")
SortVocab()
print("flag3")
#if(debug_mode>0):
# print("Vocab_size",vocabsize)
# print("Train_words",train_words)
pickle.dump( vocab_dic, open( "vocab_dic.p", "wb" ) )
pickle.dump( vocab, open( "vocab.p", "wb" ) )
def InitUnigramTable():
train_words_pow=0
for a in range(vocab_size):
train_words_pow+=vocab[a].count**power
i=0
d1=(vocab[i].count**power)/train_words_pow
for a in range(int(table_size)):
table.append(i)
if(a/float(table_size)>d1):
i+=1
d1+=(vocab[i].count**power)/train_words_pow
if(i>vocab_size):
i=vocab_size-1
pickle.dump( table, open( "table.p", "wb" ) )
def CreateBinaryTree():
pos1=vocab_size-1
pos2=vocab_size
min1i=0
min2i=0
count=[vocab[i].count for i in range(vocab_size)]+[1e15 for i in range(vocab_size-1)]
binary=[0 for i in range(2*vocab_size)]
parent_node=[0 for i in range(2*vocab_size)]
#think of a as each non-leaf node index that are going to connet with other nodes
for a in range(vocab_size-1):
#next, find two smallest nodes 'min1i, min2i'. First, find the min1i
#if pos1 has not passed the left border of "count[]"
if(pos1>=0):
if(count[pos1]<count[pos2]):
min1i=pos1
pos1-=1
else:
min1i=pos2
pos2+=1
else:
min1i=pos2
pos2+=1
#second, find the min2i
if(pos1>=0):
if(count[pos1]<count[pos2]):
min2i=pos1
pos1-=1
else:
min2i=pos2
pos2+=1
else:
min2i=pos2
pos2+=1
#already found the two sons, add their counts as their father's count
count[vocab_size + a] = count[min1i] + count[min2i]
#record their father's position in "count[]"
parent_node[min1i] = vocab_size + a
parent_node[min2i] = vocab_size + a
# let the code choosing the second son is "1", the first son is naturally "0" for the initialization of "binary[]"
binary[min2i] = 1;
#think of each a as leaf node index
for a in range(vocab_size):
code=[] #huffman code
point=[] #trace
b = a; # "b" is used to find its father, starting from itself
i = 0; # "i" is for counting the number of its ancestors, namely the length of its code
#find all ancestors by down-up style
while(1):
code.append(binary[b])
point.append(b)
i+=1
b = parent_node[b]
if(b==2*vocab_size-2):break
vocab[a].code_len=i
vocab[a].point=[x-vocab_size for x in point]+[vocab_size-2]
vocab[a].point.reverse()
vocab[a].code=code
vocab[a].code.reverse()
"""
def InitNet():
global syn0
global syn1
global syn1neg
syn0=(np.random.rand( vocab_size,layer_size)-0.5)/float(layer_size)
if(hs):
syn1=np.zeros( (vocab_size,layer_size), dtype=np.float64 )
if(negative>0):
syn1neg=np.zeros( (vocab_size,layer_size), dtype=np.float64 )
def TrainModelThread(id):
global alpha
global syn0
global syn1
global syn1neg
global iteration
word=''
last_word=''
sent_pos = 0
sent_len=0
word_count = 0
word_count_actual=0
last_word_count = 0
sen=[]
l1=-1
l2=-1
target=-1
label=-1
starting_alpha=alpha
jj=0
neu1=np.zeros( layer_size, dtype=np.float64 )
neu1e=np.zeros( layer_size, dtype=np.float64 )
fi = open(_filename_, 'r',encoding='UTF8')
fi.seek(file_size*float(id)/worker,0)
print("id : " , id,"current pos: " , fi.tell())
while(1):
if(word_count - last_word_count > 10000):
word_count_actual += word_count - last_word_count
last_word_count = word_count
alpha = starting_alpha * (1 - word_count_actual / float(train_words + 1))
if (alpha < starting_alpha * 0.0001): alpha = starting_alpha * 0.0001
#make a new sen
if(sent_pos==0):
jj+=1
if(jj%1000==0):
print(id," : make sent 1000")
while(1):
word=ReadWordIndex(fi)
if(word==-1):break #EOF
if(word==-2): #not in vocab
continue
word_count+=1
if(word==0):break #<\s>
#sample is threshold
if(sample>0):
f=vocab[word].count/float(train_words)
p=1-m.sqrt(sample/(f))
if(rn.random()<p):continue
sen.append(word)
if(len(sen)>=MAX_SENTENCE_LENGTH):break
sent_len=len(sen)
##random
if (sent_len==0):continue
if(word==-1 or word_count > train_words / float(worker)):#EOF
word_count_actual+=word_count - last_word_count
iteration-=1
if(iteration==0):break
word_count=0
last_word_count=0
sent_pos=0
fi.seek(file_size*float(id)/worker,0)
continue
word=sen[sent_pos]
if(word==-2):continue
#initialize
neu1=np.zeros( layer_size, dtype=np.float64 )
neu1e=np.zeros( layer_size, dtype=np.float64 )
b = rn.randint(0,10**8)%window
#train the CBOW architecture
if(cbow):
#in->hidden
for a in range(b,window * 2 + 1 - b):
if(a!=window):
c=sent_pos-window+a
if (c < 0) : continue
if (c >= sent_len) :continue
#target word is sen[sentence_position]
last_word = sen[c]
#here, last_word should be a context word
if (last_word == -1) : continue
#sum the context words' embeddings
#for i in range(layer_size): neu1[i] += syn0[last_word][i]
neu1 += syn0[last_word]
#if it is hierachial softmax
if(hs):
for d in range(len(vocab[word].code)):
#f is similarity : v'(j)*h
f = 0
l2 = vocab[word].point[d]
# Propagate hidden -> output : similarity of v'(j)*h
f = np.dot(neu1,syn1[l2])
if (f <= -MAX_EXP): continue
elif (f >= MAX_EXP): continue
else: f = expTable[int(((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2)))]
g = (1 - vocab[word].code[d] - f) * alpha
# Propagate errors output -> hidden
neu1e += g * syn1[l2]
#Learn weights hidden -> output
syn1[l2] += g * neu1
#if it is negative sampling
if (negative>0):
for i in range(negative+1):
if i==0:
target=word
label=1
else:
rand_num=rn.randint(0,int(table_size-1))
target=table[rand_num]
if target == 0 : target = rand_num % (vocab_size - 1) + 1
if target == word : continue
label = 0
l2=target
f = 0
f = np.dot(neu1, syn1neg[l2])
if (f >= MAX_EXP) :
g = (label - 1) * alpha
elif (f <= -MAX_EXP) :
g = (label - 0) * alpha
else :
g = (label - expTable[int(((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2)))]) * alpha
neu1e += g * syn1neg[l2]
syn1neg[l2] += g * neu1
#hidden -> in update all the c words
for i in range(b,window * 2 + 1 - b):
if(i!=window):
c=sent_pos-window+i
if (c < 0) : continue
if (c >= len(sen)) :continue
#target word is sen[sentence_position]
last_word = sen[c]
#here, last_word should be a context word
if (last_word == -1) : continue
syn0[last_word]+=neu1e
else: #Train skip-gram
#in->hidden
for a in range(b,window * 2 + 1 - b):
if(a!=window):
c=sent_pos-window+a
if (c < 0) : continue
if (c >= len(sen)) :continue
#target word is sen[sentence_position]
last_word = sen[c]
#here, last_word should be a context word
if (last_word == -1) : continue
l1=last_word
#for i in range(layer_size): neu1[i] += syn0[last_word][i]
neu1e=np.zeros( layer_size, dtype=np.float64 )
#if it is hierachial softmax
if(hs):
for d in range(len(vocab[word].code)):
#f is similarity : v'(j)*h
f = 0
l2 = vocab[word].point[d]
# Propagate hidden -> output : similarity of v'(j)*h
f += np.dot(syn0[l1],syn1[l2])
if (f <= -MAX_EXP): continue
elif (f >= MAX_EXP): continue
else: f = expTable[int(((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2)))]
g = (1 - vocab[word].code[d] - f) * alpha
# Propagate errors output -> hidden
neu1e += g * syn1[l2]
#Learn weights hidden -> output
syn1[l2] += g * syn0[l1]
#if it is negative sampling
if (negative>0):
for i in range(negative+1):
if i==0:
target=word
label=1
else:
rand_num=rn.randint(0,int(table_size-1))
target=table[rand_num]
if target == 0 : target = rand_num % (vocab_size - 1) + 1
if target == word : continue
label = 0
f = 0
f = np.dot(syn0[l1], syn1neg[target])
if f > MAX_EXP : g = (label - 1) * alpha
elif f < -MAX_EXP : g = (label - 0) * alpha
else : g = (label - expTable[int(((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2)))]) * alpha;
neu1e += g * syn1neg[target]
syn1neg[target] += g * syn0[l1]
#hidden -> in update words
syn0[l1]+=neu1e
sent_pos+=1
if(sent_pos>=sent_len):
del sen[:]
sent_pos=0
def TrainModel():
global vocab_index
vocab_index={}
global vocab_invindex
vocab_invindex={}
threadlist=[]
#LearnVocabFromTrainFile()
InitNet()
print("initialize finished")
if(negative>0):
table=pickle.load( open( "table.p", "rb" ) )
#InitUnigramTable()
print("Table finished")
print("train starts")
try:
"""
thread1=threading.Thread(target=TrainModelThread, args=(0,))
thread2=threading.Thread(target=TrainModelThread, args=(1,))
thread3=threading.Thread(target=TrainModelThread, args=(2,))
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
"""
for i in range(worker):
threadlist.append(threading.Thread(target=TrainModelThread, args=(i,)))
for i in range(worker):
threadlist[i].start()
for i in range(worker):
threadlist[i].join()
except:
print("Error: unable to start thread")
while 1:
pass
print("train finished")
#save word index and weight matrix
for i in range(vocab_size):
vocab_index[vocab[i].word]=i
#save word index and weight matrix
for i in range(vocab_size):
vocab_invindex[i]=vocab[i].word
#save with pickle
pickle.dump( syn0, open( "WeightMatrix.p", "wb" ) )
pickle.dump( vocab_index, open( "vocab_index.p", "wb" ) )
pickle.dump( vocab_invindex, open( "vocab_invindex.p", "wb" ) )
return vocab_index,vocab_invindex
#print top k
def print_topk(query,idx,inv_idx,k):
rank={}
topk=[]
for i in range(syn0.shape[0]):
if(i==idx[query]):continue
sim=np.dot(syn0[idx[query]],syn0[i])
rank[i]=sim
topk=sorted(rank.items(), key=lambda x: x[1],reverse=True)
for i in range(k):
print(inv_idx[topk[i][0]])
def main():
start_time = time.time()
#train model
idx,invidx=TrainModel()
print("---trainning time %s seconds ---" % (time.time() - start_time))
#find top-k related word vectors
while(1):
query=raw_input()
if(vocab_index.has_key(query)):
print_topk(query,idx,invidx,10)
else:
print("It's not in the vocab")
if __name__ == "__main__":
main()