-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword2vec_numpy.py
243 lines (164 loc) · 8.01 KB
/
word2vec_numpy.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
from collections import namedtuple
import numpy as np
import extractor
import gc
raw_text_path = 'part.txt'
def generate_dictionary(text):
WordDict = namedtuple(
"WordDict", ['word_to_index', 'index_to_word', 'corpus'])
corpus = []
word_to_index = {}
index_to_word = {}
index = 0
for row in text:
for word in extractor.extract_word(row, 1):
corpus.append(word)
if word not in word_to_index:
word_to_index.update({word: index})
index_to_word.update({index: word})
index += 1
if index % 5000 == 0:
print(f"fetched {index} words")
return WordDict(word_to_index, index_to_word, corpus)
def loadTextStream(path: str):
with open(path, mode='r', encoding='utf-8') as f:
while f.readable():
ret = str(f.readline())
if len(ret) == 0:
return
yield ret.strip()
def loadTextDirect(path: str):
with open(path, mode='r', encoding='utf-8') as f:
return [str(line).strip() for line in f.readlines()]
def get_one_hot_vectors(target_word, context_words, vocab_size, word_to_index):
#Create an array of size = vocab_size filled with zeros
trgt_word_vector = np.zeros(vocab_size)
#Get the index of the target_word according to the dictionary word_to_index.
#If target_word = best, the index according to the dictionary word_to_index is 0.
#So the one hot vector will be [1, 0, 0, 0, 0, 0, 0, 0, 0]
index_of_word_dictionary = word_to_index.get(target_word)
#Set the index to 1
trgt_word_vector[index_of_word_dictionary] = 1
#Repeat same steps for context_words but in a loop
ctxt_word_vector = np.zeros(vocab_size)
for word in context_words:
index_of_word_dictionary = word_to_index.get(word)
ctxt_word_vector[index_of_word_dictionary] = 1
return trgt_word_vector, ctxt_word_vector
def generate_training_data(corpus,window_size,vocab_size,word_to_index,length_of_corpus,sample=None):
training_data = []
training_sample_words = []
for i,word in enumerate(corpus):
index_target_word = i
target_word = word
context_words = []
#when target word is the first word
if i == 0:
# trgt_word_index:(0), ctxt_word_index:(1,2)
context_words = [corpus[x] for x in range(i + 1 , window_size + 1)]
#when target word is the last word
elif i == len(corpus)-1:
# trgt_word_index:(9), ctxt_word_index:(8,7), length_of_corpus = 10
context_words = [corpus[x] for x in range(length_of_corpus - 2 ,length_of_corpus -2 - window_size , -1 )]
#When target word is the middle word
else:
#Before the middle target word
before_target_word_index = index_target_word - 1
for x in range(before_target_word_index, before_target_word_index - window_size , -1):
if x >=0:
context_words.extend([corpus[x]])
#After the middle target word
after_target_word_index = index_target_word + 1
for x in range(after_target_word_index, after_target_word_index + window_size):
if x < len(corpus):
context_words.extend([corpus[x]])
trgt_word_vector,ctxt_word_vector = get_one_hot_vectors(target_word,context_words,vocab_size,word_to_index)
training_data.append([trgt_word_vector,ctxt_word_vector])
if sample is not None:
training_sample_words.append([target_word,context_words])
return training_data,training_sample_words
def forward_prop(weight_inp_hidden,weight_hidden_output,target_word_vector):
#target_word_vector = x , weight_inp_hidden = weights for input layer to hidden layer
hidden_layer = np.dot(weight_inp_hidden.T, target_word_vector)
#weight_hidden_output = weights for hidden layer to output layer
u = np.dot(weight_hidden_output.T, hidden_layer)
y_predicted = softmax(u)
return y_predicted, hidden_layer, u
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
def calculate_error(y_pred,context_words):
total_error = [None] * len(y_pred)
index_of_1_in_context_words = {}
for index in np.where(context_words == 1)[0]:
index_of_1_in_context_words.update ( {index : 'yes'} )
number_of_1_in_context_vector = len(index_of_1_in_context_words)
for i,value in enumerate(y_pred):
if index_of_1_in_context_words.get(i) != None:
total_error[i]= (value-1) + ( (number_of_1_in_context_vector -1) * value)
else:
total_error[i]= (number_of_1_in_context_vector * value)
return np.array(total_error)
def backward_prop(weight_inp_hidden,weight_hidden_output,total_error, hidden_layer, target_word_vector,learning_rate):
dl_weight_inp_hidden = np.outer(target_word_vector, np.dot(weight_hidden_output, total_error.T))
dl_weight_hidden_output = np.outer(hidden_layer, total_error)
# Update weights
weight_inp_hidden = weight_inp_hidden - (learning_rate * dl_weight_inp_hidden)
weight_hidden_output = weight_hidden_output - (learning_rate * dl_weight_hidden_output)
return weight_inp_hidden,weight_hidden_output
def calculate_loss(u,ctx):
sum_1 = 0
for index in np.where(ctx==1)[0]:
sum_1 = sum_1 + u[index]
sum_1 = -sum_1
sum_2 = len(np.where(ctx==1)[0]) * np.log(np.sum(np.exp(u)))
total_loss = sum_1 + sum_2
return total_loss
# Input vector, returns nearest word(s)
def cosine_similarity(word,weight,word_to_index,vocab_size,index_to_word):
#Get the index of the word from the dictionary
index = word_to_index[word]
#Get the correspondin weights for the word
word_vector_1 = weight[index]
word_similarity = {}
for i in range(vocab_size):
word_vector_2 = weight[i]
theta_sum = np.dot(word_vector_1, word_vector_2)
theta_den = np.linalg.norm(word_vector_1) * np.linalg.norm(word_vector_2)
theta = theta_sum / theta_den
word = index_to_word[i]
word_similarity[word] = theta
return word_similarity #words_sorted
def train(word_embedding_dimension,vocab_size,epochs,training_data,learning_rate,disp = 'no',interval=-1):
weights_input_hidden = np.random.uniform(-1, 1, (vocab_size, word_embedding_dimension))
weights_hidden_output = np.random.uniform(-1, 1, (word_embedding_dimension, vocab_size))
#For analysis purposes
epoch_loss = []
weights_1 = []
weights_2 = []
for epoch in range(epochs):
loss = 0
for target,context in training_data:
y_pred, hidden_layer, u = forward_prop(weights_input_hidden,weights_hidden_output,target)
total_error = calculate_error(y_pred, context)
weights_input_hidden,weights_hidden_output = backward_prop(
weights_input_hidden,weights_hidden_output ,total_error, hidden_layer, target,learning_rate
)
loss_temp = calculate_loss(u,context)
loss += loss_temp
epoch_loss.append( loss )
weights_1.append(weights_input_hidden)
weights_2.append(weights_hidden_output)
if disp == 'yes':
if epoch ==0 or epoch % interval ==0 or epoch == epochs -1:
print('Epoch: %s. Loss:%s' %(epoch,loss))
return epoch_loss,np.array(weights_1),np.array(weights_2)
word_dict = generate_dictionary(loadTextStream(raw_text_path))
print("generating tranning set")
vocab_size = len(word_dict.word_to_index)
(tranning_sets,sample_word) = generate_training_data(word_dict.corpus,4,vocab_size,word_dict.word_to_index,len(word_dict.corpus))
del word_dict
gc.collect()
print("start to train")
(loss,target_word_model,context_model) = train(30,vocab_size,20,tranning_sets,0.05,'yes')
word_dict = generate_dictionary(loadTextStream(raw_text_path))