-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc2vec.py
353 lines (291 loc) · 11.9 KB
/
doc2vec.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
import batch_generators
import numpy as np
import model
import optimizers
import preprocess
import word2vec
"""Simple paragraph 2 vector implementation."""
def read_file(self, fname):
with open(fname, 'r') as f:
data = f.read()
paras = data.split('\n\n')
docs = [para.split() for para in paras]
return docs
def tokenize_paragraphs(paragraphs):
tokenized = []
for para in paragraphs:
z = para.split('\n')
lines = []
for line in z:
em = line.split('.')
lines.extend(em)
words = []
for l in lines:
em = l.split(' ')
words.extend(em)
tokenized.append(filter(lambda w: w != '', words))
return tokenized
def doc2id(doc, word2id):
indexed_doc = []
for word in doc:
if word in word2id:
indexed_doc.append(word2id[word])
else:
indexed_doc.append(word2id['UNK'])
return indexed_doc
def docs2id(docs, word2id):
return [doc2id(doc, word2id) for doc in docs]
def id2doc(doc, id2word):
return ' '.join(id2word[word] for word in doc)
def ids2doc(docs, id2word):
return [id2doc(doc, id2word) for doc in docs]
class Doc2Vec(model.SupervisedModel):
def __init__(
self, n_docs, doc_embedding_dim, word_embedding_dim, n_skips,
window_size, vocabulary_size, neg_k, word2vec_model=None,
sample_vocabulary=None):
"""doc2vec model.
Currently supports PV-DM and PV-DBOW version of doc2vec.
Args:-
batch_size: int. Batch size of mini batch training.
n_docs: int. Number of documents for training.
doc_embedding_dim: int. Dimension of document embeddings.
word_embedding_dim: int. Dimension of word embeddings.
n_skips: int. Number of words used in input to predict next output
word.
window_size: int. Size of the word window for word2vec model.
vocabulary_size: int. Size of vocabulary.
neg_k: int. Negative sampling size.
word2vec_model. Word2Vec. A pretrained word2vec model
(if available).
sample_vocabulary: list of str. Words from which samples will be
extracted for negative sampling.
"""
self.doc_embedding_dim = doc_embedding_dim
self.word_embedding_dim = word_embedding_dim
self.n_skips = n_skips
self.window_size = window_size
self.vocabulary_size = vocabulary_size
self.neg_k = neg_k
self.is_word2vec_trained = False
self.gen = None
self.sample_vocabulary = sample_vocabulary
self.n_docs = n_docs
self.total_embeddings = preprocess.xavier_init(
(n_docs, 2 * doc_embedding_dim))
# Parameters for pv-dm model.
self.doc_embeddings = self.total_embeddings[:, :doc_embedding_dim]
self.softmax_w = preprocess.xavier_init((
doc_embedding_dim + n_skips * word_embedding_dim,
vocabulary_size))
self.softmax_b = np.zeros(vocabulary_size)
# Parameters for pv-dbow model.
self.dbow_embeddings = self.total_embeddings[:, doc_embedding_dim:]
self.dbow_softmax_w = preprocess.xavier_init(
(doc_embedding_dim, vocabulary_size))
self.dbow_softmax_b = np.zeros(vocabulary_size)
self.params = self.get_params()
if word2vec_model is None:
self.w2v = word2vec.Word2Vec(
word_embedding_dim, vocabulary_size, window_size,
n_skips, neg_k)
self.word_embeddings = self.w2v.embeddings
else:
self.w2v = word2vec_model
self.word_embeddings = word2vec_model.embeddings
self.word2id = word2vec_model.word2id
self.id2word = word2vec_model.id2word
self.is_word2vec_trained = True
def _encode_paragraph(self, z):
"""Given a text paragraph convert it into equivalent numeric paragraph.
"""
para = tokenize_paragraphs([z])[0]
return doc2id(z, self.word2id)
def softmax(self, z):
ez = z - z.max()
ez = np.exp(ez)
return ez / ez.sum()
def set_sample_vocabulary(self):
self.sample_vocabulary = self.w2v.sample_vocabulary
def negative_samples(self, labels):
return self.w2v.negative_samples(labels)
def reset_total_embeddings(self):
self.total_embeddings[:, :self.doc_embedding_dim] = self.doc_embeddings
self.total_embeddings[:, self.doc_embedding_dim:] = self.dbow_embeddings
def train_word2vec(self, data, max_epochs, learning_rate):
opti = optimizers.Adam(self.w2v, 20*self.n_skips, learning_rate)
opti.train(self.data)
self.word2id = self.w2v.word2id
self.id2word = self.w2v.id2word
self.is_word2vec_trained = True
def forward(self, data, labels):
# Forward pass for pv-dm model.
hidden = np.zeros((data.shape[0], self.softmax_w.shape[0]))
hidden[:, :self.doc_embedding_dim] = self.doc_embeddings[data[:, 0], :]
for i in range(self.n_skips):
hidden[:, self.doc_embedding_dim + (i * self.word_embedding_dim) : self.doc_embedding_dim + ((i+1) * self.word_embedding_dim)] = self.word_embeddings[data[:, i+1], :]
samples = np.zeros((data.shape[0], self.neg_k), dtype=np.int32)
samples[:, :-1] = self.negative_samples(labels)
samples[:, -1] = labels
output = np.zeros((self.batch_size, self.neg_k))
for i in range(self.batch_size):
output[i] = self.softmax(np.dot(hidden[i], self.softmax_w[:, samples[i]]) + self.softmax_b[samples[i]])
# Forward pass for pv-dbow model.
dbow_hidden = self.dbow_embeddings[data[:, 0], :]
dbow_output = np.zeros((self.batch_size, self.neg_k))
for i in range(self.batch_size):
dbow_output[i] = self.softmax(np.dot(dbow_hidden[i], self.dbow_softmax_w[:, samples[i]]) + self.dbow_softmax_b[samples[i]])
cache = {
'data': data,
'hidden': hidden,
'dbow_hidden': dbow_hidden,
'output': output,
'dbow_output': dbow_output,
'samples': samples
}
return cache
def backward(self, dout, dout_dbow, cache):
data = cache['data']
hidden = cache['hidden']
dbow_hidden = cache['dbow_hidden']
samples = cache['samples']
# Gradient for pv-dm model.
d_hidden = np.zeros(hidden.shape)
d_doc_embeddings = np.zeros(self.doc_embeddings.shape)
d_softmax_w = np.zeros(self.softmax_w.shape)
d_softmax_b = np.zeros(self.softmax_b.shape)
# Gradient for pv-dbow model.
d_dbow_hidden = np.zeros(dbow_hidden.shape)
d_dbow_embeddings = np.zeros(self.dbow_embeddings.shape)
d_dbow_softmax_w = np.zeros(self.dbow_softmax_w.shape)
d_dbow_softmax_b = np.zeros(self.dbow_softmax_b.shape)
for i in range(self.batch_size):
# Backward pass for pv-dm model.
d_hidden[i] = np.dot(dout[i], self.softmax_w[:, samples[i]].T)
d_softmax_w[:, samples[i]] += np.dot(hidden[i][:, np.newaxis], dout[i, np.newaxis])
d_softmax_b[samples[i]] += dout[i]
d_doc_embeddings[data[i, 0], :] += d_hidden[i, :self.doc_embedding_dim]
# Backward pass for pv-dbow model.
d_dbow_hidden[i] = np.dot(dout_dbow[i], self.dbow_softmax_w[:, samples[i]].T)
d_dbow_softmax_w[:, samples[i]] += np.dot(dbow_hidden[i][:, np.newaxis], dout_dbow[i, np.newaxis])
d_dbow_softmax_b[samples[i]] += dout_dbow[i]
d_dbow_embeddings[data[i, 0], :] += d_dbow_hidden[i]
d_cache = {
'd_doc_embeddings': d_doc_embeddings,
'd_softmax_w': d_softmax_w,
'd_softmax_b': d_softmax_b,
'd_dbow_embeddings': d_dbow_embeddings,
'd_dbow_softmax_w': d_dbow_softmax_w,
'd_dbow_softmax_b': d_dbow_softmax_b
}
return d_cache
def compute_loss_and_gradient(self, outputs):
# Xentropy loss function.
loss = np.sum(-1 * np.log(outputs)[:, -1])
dout = outputs.copy()
dout[:, -1] = outputs[:, -1] - 1
return loss, dout
def get_batch_generator(self, batch_size, data, labels=None):
self.data = reduce(lambda x, y: x+y, data)
w2i, _, _, _ = word2vec.word2id(data, self.vocabulary_size)
data = docs2id(tokenize_paragraphs(data), w2i)
self.set_sample_vocabulary()
return batch_generators.Doc2VecBatchGenerator(
batch_size, data, self.n_skips)
def get_params_mapping(self):
mappings = {
'doc_embeddings': ['d_doc_embeddings', self.doc_embeddings.shape],
'softmax_w': ['d_softmax_w', self.softmax_w.shape],
'softmax_b': ['d_softmax_b', self.softmax_b.shape],
'dbow_embeddings': ['d_dbow_embeddings', self.dbow_embeddings.shape],
'dbow_softmax_w': ['d_dbow_softmax_w', self.dbow_softmax_w.shape],
'dbow_softmax_b': ['d_dbow_softmax_b', self.dbow_softmax_b.shape]
}
return mappings
def get_params(self):
params = {
'doc_embeddings': self.doc_embeddings,
'softmax_w': self.softmax_w,
'softmax_b': self.softmax_b,
'dbow_embeddings': self.dbow_embeddings,
'dbow_softmax_w': self.dbow_softmax_w,
'dbow_softmax_b': self.dbow_softmax_b
}
return params
def train(self, batch_input, batch_output):
"""Train document 2 vector model."""
# If word2vec model is not trained then train that first.
if (not self.is_word2vec_trained) or train_word2vec:
self.train_word2vec(max_epochs, learning_rate)
# Normalize embeddings.
self.doc_embeddings /= np.sqrt((self.doc_embeddings ** 2).sum(axis=1)[:, np.newaxis])
self.dbow_embeddings /= np.sqrt((self.dbow_embeddings ** 2).sum(axis=1)[:, np.newaxis])
cache = self.forward(batch_input, batch_output)
loss, dout = self.compute_loss_and_gradient(cache['output'])
dbow_loss, dout_dbow = self.compute_loss_and_gradient(cache['dbow_output'])
d_cache = self.backward(dout, dout_dbow, cache)
return self.params, d_cache, (loss + dbow_loss) / 2
def vectorize_paragraphs(
self, paragraphs, max_epochs, learning_rate, momentum):
"""Create vector representation of a new paragraph.
This is used at time of testing / inference to make decisions about
unseen paragraphs.
"""
new_embeddings = preprocess.xavier_init(
(len(paragraphs) + self.doc_embeddings.shape[0],
self.doc_embeddings.shape[1]))
new_dbow_embeddings = preprocess.xavier_init(
(len(paragraphs) + self.dbow_embeddings.shape[0],
self.dbow_embeddings.shape[1]))
new_embeddings[len(paragraphs):, :] = self.doc_embeddings
new_dbow_embeddings[len(paragraphs):, :] = self.dbow_embeddings
original_embeddings = self.doc_embeddings.copy()
original_dbow_embeddings = self.dbow_embeddings.copy()
self.doc_embeddings = new_embeddings
self.dbow_embeddings = new_dbow_embeddings
previous_d_doc_embeddings = np.zeros(new_embeddings.shape)
previous_d_dbow_embeddings = np.zeros(new_dbow_embeddings.shape)
gen = self.get_batch_generator(len(paragraphs), paragraphs)
for epoch in xrange(max_epochs):
# Forward pass.
batch_input, batch_output = gen.next_batch()
cache = self.forward(batch_input, batch_output)
# Compute output gradients.
loss, dout = self.compute_loss_and_gradient(cache['output'])
dbow_loss, dout_dbow = self.compute_loss_and_gradient(cache['dbow_output'])
# Backward pass.
d_cache = self.backward(dout, dout_dbow, cache)
d_doc_embeddings = d_cache['d_doc_embeddings'] / len(paragraphs)
previous_d_doc_embeddings = momentum * previous_d_doc_embeddings - learning_rate * d_doc_embeddings
self.doc_embeddings = self.doc_embeddings + previous_d_doc_embeddings
d_dbow_embeddings = d_cache['d_dbow_embeddings'] / len(paragraphs)
previous_d_dbow_embeddings = momentum * previous_d_dbow_embeddings - learning_rate * d_dbow_embeddings
self.dbow_embeddings = self.dbow_embeddings + previous_d_dbow_embeddings
# Normalize embeddings.
self.doc_embeddings /= np.sqrt((self.doc_embeddings ** 2).sum(axis=1)[:, np.newaxis])
self.dbow_embeddings /= np.sqrt((self.dbow_embeddings ** 2).sum(axis=1)[:, np.newaxis])
self.doc_embeddings = original_embeddings
self.dbow_embeddings = original_dbow_embeddings
self.reset_total_embeddings()
return new_embeddings[:len(paragraphs), :], new_dbow_embeddings[:len(paragraphs), :]
def cosine_similarity(
self, para, max_epochs, learning_rate, momentum, top=3):
"""Find similarity between given paragraph and some other paragraph.
Args:-
para: str. Paragraph for which similar paragraphs are to be found.
learning_rate: float. Learning rate during inference.
momentum: float. Momentum during inference.
top: int. Number of top similar paragraphs.
"""
try:
self.id2word
self.word2id
except Exception:
print "Please train doc2vec model with data first."
return None
para = self._encode_paragraph(para)
p_dm_vector, p_dbow_vector = self.vectorize_paragraphs(
[para], max_epochs, learning_rate, momentum)
p_vector = np.concatenate([p_dm_vector, p_dbow_vector], axis=1)
simi = np.sum(p_vector * self.total_embeddings, 1)
return np.argsort(simi)[-1:-top-1:-1]