forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathword2vec_theano.py
409 lines (307 loc) · 11.6 KB
/
word2vec_theano.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
# https://deeplearningcourses.com/c/natural-language-processing-with-deep-learning-in-python
# https://udemy.com/natural-language-processing-with-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import json
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import expit as sigmoid
from sklearn.utils import shuffle
from datetime import datetime
# from util import find_analogies
from scipy.spatial.distance import cosine as cos_dist
from sklearn.metrics.pairwise import pairwise_distances
from glob import glob
import os
import sys
import string
import theano
import theano.tensor as T
# unfortunately these work different ways
def remove_punctuation_2(s):
return s.translate(None, string.punctuation)
def remove_punctuation_3(s):
return s.translate(str.maketrans('','',string.punctuation))
if sys.version.startswith('2'):
remove_punctuation = remove_punctuation_2
else:
remove_punctuation = remove_punctuation_3
def get_wiki():
V = 20000
files = glob('../large_files/enwiki*.txt')
all_word_counts = {}
for f in files:
for line in open(f):
if line and line[0] not in '[*-|=\{\}':
s = remove_punctuation(line).lower().split()
if len(s) > 1:
for word in s:
if word not in all_word_counts:
all_word_counts[word] = 0
all_word_counts[word] += 1
print("finished counting")
V = min(V, len(all_word_counts))
all_word_counts = sorted(all_word_counts.items(), key=lambda x: x[1], reverse=True)
top_words = [w for w, count in all_word_counts[:V-1]] + ['<UNK>']
word2idx = {w:i for i, w in enumerate(top_words)}
unk = word2idx['<UNK>']
sents = []
for f in files:
for line in open(f):
if line and line[0] not in '[*-|=\{\}':
s = remove_punctuation(line).lower().split()
if len(s) > 1:
# if a word is not nearby another word, there won't be any context!
# and hence nothing to train!
sent = [word2idx[w] if w in word2idx else unk for w in s]
sents.append(sent)
return sents, word2idx
def train_model(savedir):
# get the data
sentences, word2idx = get_wiki() #get_text8()
# number of unique words
vocab_size = len(word2idx)
# config
window_size = 5
learning_rate = 0.025*128
final_learning_rate = 0.0001*128
num_negatives = 5 # number of negative samples to draw per input word
samples_per_epoch = int(1e5)
epochs = 1
D = 50 # word embedding size
# learning rate decay
learning_rate_delta = (learning_rate - final_learning_rate) / epochs
# learning_rate_delta = 0
# params
W = np.random.randn(vocab_size, D) / np.sqrt(D + vocab_size) # input-to-hidden
V = np.random.randn(D, vocab_size) / np.sqrt(D + vocab_size) # hidden-to-output
# theano variables
thW = theano.shared(W)
thV = theano.shared(V)
# theano placeholders
th_pos_word = T.ivector('pos_word')
th_neg_word = T.ivector('neg_word')
th_context = T.ivector('context')
th_lr = T.scalar('learning_rate')
# get the output and loss
input_words = T.concatenate([th_pos_word, th_neg_word])
W_subset = thW[input_words]
dbl_context = T.concatenate([th_context, th_context])
V_subset = thV[:, dbl_context]
logits = W_subset.dot(V_subset)
out = T.nnet.sigmoid(logits)
n = th_pos_word.shape[0]
th_cost = -T.log(out[:n]).mean() - T.log(1 - out[n:]).mean()
# specify the updates
gW = T.grad(th_cost, W_subset)
gV = T.grad(th_cost, V_subset)
W_update = T.inc_subtensor(W_subset, -th_lr*gW)
V_update = T.inc_subtensor(V_subset, -th_lr*gV)
updates = [(thW, W_update), (thV, V_update)]
# full update
# gW, gV = T.grad(th_cost, [thW, thV])
# vW = theano.shared(np.zeros_like(W))
# vV = theano.shared(np.zeros_like(V))
# new_vW = 0.9*vW - th_lr*gW
# new_vV = 0.9*vV - th_lr*gV
# W_update = thW + new_vW
# V_update = thV + new_vV
# updates = [(thW, W_update), (thV, V_update), (vW, new_vW), (vV, new_vV)]
# make callable functions
cost_op = theano.function(
inputs=[th_pos_word, th_neg_word, th_context],
outputs=th_cost,
# allow_input_downcast=True
)
cost_train_op = theano.function(
inputs=[th_pos_word, th_neg_word, th_context, th_lr],
outputs=th_cost,
updates=updates,
# allow_input_downcast=True
)
# distribution for drawing negative samples
p_neg = get_negative_sampling_distribution(sentences, vocab_size)
# save the costs to plot them per iteration
costs = []
# number of total words in corpus
total_words = sum(len(sentence) for sentence in sentences)
print("total number of words in corpus:", total_words)
# keep only certain words based on p_neg
threshold = 1e-5
p_drop = 1 - np.sqrt(threshold / p_neg)
# train the model
for epoch in range(epochs):
# randomly order sentences so we don't always see
# sentences in the same order
np.random.shuffle(sentences)
# accumulate the cost
cost = 0
counter = 0
inputs = []
targets = []
negwords = []
t0 = datetime.now()
for sentence in sentences:
# keep only certain words based on p_neg
sentence = [w for w in sentence \
if np.random.random() < (1 - p_drop[w])
]
if len(sentence) < 2:
continue
# randomly order words so we don't always see
# samples in the same order
randomly_ordered_positions = np.random.choice(
len(sentence),
size=len(sentence),
replace=False,
)
for pos in randomly_ordered_positions:
# the middle word
word = sentence[pos]
# get the positive context words/negative samples
context_words = get_context(pos, sentence, window_size)
neg_word = np.random.choice(vocab_size, p=p_neg)
n = len(context_words)
inputs += [word]*n
negwords += [neg_word]*n
targets += context_words
if len(inputs) >= 128:
c = cost_train_op(inputs, negwords, targets, learning_rate)
cost += c
if np.isnan(c):
print("c is nan:", c)
exit()
# reset
inputs = []
targets = []
negwords = []
counter += 1
if counter % 100 == 0:
sys.stdout.write("processed %s / %s, cost: %s\r" % (counter, len(sentences), c))
sys.stdout.flush()
# print stuff so we don't stare at a blank screen
dt = datetime.now() - t0
print("epoch complete:", epoch, "cost:", cost, "dt:", dt)
# save the cost
costs.append(cost)
# update the learning rate
learning_rate -= learning_rate_delta
# plot the cost per iteration
plt.plot(costs)
plt.show()
# save the model
if not os.path.exists(savedir):
os.mkdir(savedir)
with open('%s/word2idx.json' % savedir, 'w') as f:
json.dump(word2idx, f)
# don't forget to extract the weights from theano
W, V = thW.get_value(), thV.get_value()
np.savez('%s/weights.npz' % savedir, W, V)
# return the model
return word2idx, W, V
def get_negative_sampling_distribution(sentences, vocab_size):
# Pn(w) = prob of word occuring
# we would like to sample the negative samples
# such that words that occur more often
# should be sampled more often
word_freq = np.zeros(vocab_size)
word_count = sum(len(sentence) for sentence in sentences)
for sentence in sentences:
for word in sentence:
word_freq[word] += 1
# smooth it
p_neg = word_freq**0.75
# normalize it
p_neg = p_neg / p_neg.sum()
assert(np.all(p_neg > 0))
return p_neg
def get_context(pos, sentence, window_size):
# input:
# a sentence of the form: x x x x c c c pos c c c x x x x
# output:
# the context word indices: c c c c c c
start = max(0, pos - window_size)
end_ = min(len(sentence), pos + window_size)
context = []
for ctx_pos, ctx_word_idx in enumerate(sentence[start:end_], start=start):
if ctx_pos != pos:
# don't include the input word itself as a target
context.append(ctx_word_idx)
return context
# return np.concatenate([sentence[start:pos], sentence[pos+1:end_]])
def load_model(savedir):
with open('%s/word2idx.json' % savedir) as f:
word2idx = json.load(f)
npz = np.load('%s/weights.npz' % savedir)
W = npz['arr_0']
V = npz['arr_1']
return word2idx, W, V
def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W):
V, D = W.shape
# don't actually use pos2 in calculation, just print what's expected
print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
for w in (pos1, neg1, pos2, neg2):
if w not in word2idx:
print("Sorry, %s not in word2idx" % w)
return
p1 = W[word2idx[pos1]]
n1 = W[word2idx[neg1]]
p2 = W[word2idx[pos2]]
n2 = W[word2idx[neg2]]
vec = p1 - n1 + n2
distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V)
idx = distances.argsort()[:10]
# pick one that's not p1, n1, or n2
best_idx = -1
keep_out = [word2idx[w] for w in (pos1, neg1, neg2)]
# print("keep_out:", keep_out)
for i in idx:
if i not in keep_out:
best_idx = i
break
# print("best_idx:", best_idx)
print("got: %s - %s = %s - %s" % (pos1, neg1, idx2word[best_idx], neg2))
print("closest 10:")
for i in idx:
print(idx2word[i], distances[i])
print("dist to %s:" % pos2, cos_dist(p2, vec))
def test_model(word2idx, W, V):
# there are multiple ways to get the "final" word embedding
# We = (W + V.T) / 2
# We = W
idx2word = {i:w for w, i in word2idx.items()}
for We in (W, (W + V.T) / 2):
print("**********")
analogy('king', 'man', 'queen', 'woman', word2idx, idx2word, We)
analogy('king', 'prince', 'queen', 'princess', word2idx, idx2word, We)
analogy('miami', 'florida', 'dallas', 'texas', word2idx, idx2word, We)
analogy('einstein', 'scientist', 'picasso', 'painter', word2idx, idx2word, We)
analogy('japan', 'sushi', 'germany', 'bratwurst', word2idx, idx2word, We)
analogy('man', 'woman', 'he', 'she', word2idx, idx2word, We)
analogy('man', 'woman', 'uncle', 'aunt', word2idx, idx2word, We)
analogy('man', 'woman', 'brother', 'sister', word2idx, idx2word, We)
analogy('man', 'woman', 'husband', 'wife', word2idx, idx2word, We)
analogy('man', 'woman', 'actor', 'actress', word2idx, idx2word, We)
analogy('man', 'woman', 'father', 'mother', word2idx, idx2word, We)
analogy('heir', 'heiress', 'prince', 'princess', word2idx, idx2word, We)
analogy('nephew', 'niece', 'uncle', 'aunt', word2idx, idx2word, We)
analogy('france', 'paris', 'japan', 'tokyo', word2idx, idx2word, We)
analogy('france', 'paris', 'china', 'beijing', word2idx, idx2word, We)
analogy('february', 'january', 'december', 'november', word2idx, idx2word, We)
analogy('france', 'paris', 'germany', 'berlin', word2idx, idx2word, We)
analogy('week', 'day', 'year', 'month', word2idx, idx2word, We)
analogy('week', 'day', 'hour', 'minute', word2idx, idx2word, We)
analogy('france', 'paris', 'italy', 'rome', word2idx, idx2word, We)
analogy('paris', 'france', 'rome', 'italy', word2idx, idx2word, We)
analogy('france', 'french', 'england', 'english', word2idx, idx2word, We)
analogy('japan', 'japanese', 'china', 'chinese', word2idx, idx2word, We)
analogy('china', 'chinese', 'america', 'american', word2idx, idx2word, We)
analogy('japan', 'japanese', 'italy', 'italian', word2idx, idx2word, We)
analogy('japan', 'japanese', 'australia', 'australian', word2idx, idx2word, We)
analogy('walk', 'walking', 'swim', 'swimming', word2idx, idx2word, We)
if __name__ == '__main__':
word2idx, W, V = train_model('w2v_model')
# word2idx, W, V = load_model('w2v_model')
test_model(word2idx, W, V)