-
Notifications
You must be signed in to change notification settings - Fork 0
/
GloVe.py
306 lines (264 loc) · 13.6 KB
/
GloVe.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
"""
Tensorflow implementation of WV-DR algorithm wrapper class(sklearn style)
:author: Fido Wang ([email protected]),
:refer: http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf
:refer: https://github.com/GradySimon/tensorflow-glove/blob/master/tf_glove.py
"""
from __future__ import division
from collections import Counter, defaultdict
import os
from random import shuffle
import tensorflow as tf
import numpy as np
from error import NotTrainedError, NotFitToCorpusError
from option import GloVeOption
import math
import time
import collections
import random
def _generate_batch_para(doc_ids, word_ids, batch_size, num_skips, window_size):
"""
batch generator for Skip-Gram Model(Distributed Representation of Word Vecotors)
:param doc_ids: list of document indices
:param word_ids: list of word indices
:param batch_size: number of words in each mini-batch
:param num_skips: number of sample for each target word window
:param window_size: number of words between the target word
"""
data_index = 0
assert batch_size % num_skips == 0
assert num_skips <= 2 * window_size
labels = np.ndarray(shape=(batch_size), dtype=np.int32)
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
span = 2 * window_size + 1
buffer = collections.deque(maxlen=span)
buffer_para = collections.deque(maxlen=span)
i = 0
while data_index < len(word_ids):
if len(buffer) == span and len(set(buffer_para)) == 1:
target = window_size
targets_to_avoid = [window_size]
for j in range(num_skips):
while target in targets_to_avoid:
target = random.randint(0, span - 1)
labels[i + j] = buffer[target]
batch[i + j] = buffer[window_size]
i += num_skips
buffer.append(word_ids[data_index])
buffer_para.append(doc_ids[data_index])
data_index = (data_index + 1) % len(word_ids)
if i == batch_size:
yield batch, labels[:, None]
i = 0
labels = np.ndarray(shape=(batch_size), dtype=np.int32)
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
class GloVeModel():
def __init__(self, embedding_size, context_size, max_vocab_size=100000, min_occurrences=1,
scaling_factor=3/4, cooccurrence_cap=100, batch_size=512, learning_rate=0.05):
self.embedding_size = embedding_size
if isinstance(context_size, tuple):
self.left_context, self.right_context = context_size
elif isinstance(context_size, int):
self.left_context = self.right_context = context_size
else:
raise ValueError("'context_size' should be an int or a tuple of two ints")
self.max_vocab_size = max_vocab_size
self.min_occurrences = min_occurrences
self.scaling_factor = scaling_factor
self.cooccurrence_cap = cooccurrence_cap
self.batch_size = batch_size
self.learning_rate = learning_rate
self.__words = None
self.__word_to_id = None
self.__cooccurrence_matrix = None
self.__embeddings = None
def fit_to_corpus(self, corpus):
self.__fit_to_corpus(corpus, self.max_vocab_size, self.min_occurrences,
self.left_context, self.right_context)
self.__build_graph()
def __fit_to_corpus(self, corpus, vocab_size, min_occurrences, left_size, right_size):
word_counts = Counter()
cooccurrence_counts = defaultdict(float)
for region in corpus:
word_counts.update(region)
for l_context, word, r_context in _context_windows(region, left_size, right_size):
for i, context_word in enumerate(l_context[::-1]):
# add (1 / distance from focal word) for this pair
cooccurrence_counts[(word, context_word)] += 1 / (i + 1)
for i, context_word in enumerate(r_context):
cooccurrence_counts[(word, context_word)] += 1 / (i + 1)
if len(cooccurrence_counts) == 0:
raise ValueError("No coccurrences in corpus. Did you try to reuse a generator?")
self.__words = [word for word, count in word_counts.most_common(vocab_size)
if count >= min_occurrences]
self.__word_to_id = {word: i for i, word in enumerate(self.__words)}
self.__cooccurrence_matrix = {
(self.__word_to_id[words[0]], self.__word_to_id[words[1]]): count
for words, count in cooccurrence_counts.items()
if words[0] in self.__word_to_id and words[1] in self.__word_to_id}
def __build_graph(self):
self.__graph = tf.Graph()
with self.__graph.as_default(), self.__graph.device(_device_for_node):
count_max = tf.constant([self.cooccurrence_cap], dtype=tf.float32,
name='max_cooccurrence_count')
scaling_factor = tf.constant([self.scaling_factor], dtype=tf.float32,
name="scaling_factor")
self.__focal_input = tf.placeholder(tf.int32, shape=[self.batch_size],
name="focal_words")
self.__context_input = tf.placeholder(tf.int32, shape=[self.batch_size],
name="context_words")
self.__cooccurrence_count = tf.placeholder(tf.float32, shape=[self.batch_size],
name="cooccurrence_count")
focal_embeddings = tf.Variable(
tf.random_uniform([self.vocab_size, self.embedding_size], 1.0, -1.0),
name="focal_embeddings")
context_embeddings = tf.Variable(
tf.random_uniform([self.vocab_size, self.embedding_size], 1.0, -1.0),
name="context_embeddings")
focal_biases = tf.Variable(tf.random_uniform([self.vocab_size], 1.0, -1.0),
name='focal_biases')
context_biases = tf.Variable(tf.random_uniform([self.vocab_size], 1.0, -1.0),
name="context_biases")
focal_embedding = tf.nn.embedding_lookup([focal_embeddings], self.__focal_input)
context_embedding = tf.nn.embedding_lookup([context_embeddings], self.__context_input)
focal_bias = tf.nn.embedding_lookup([focal_biases], self.__focal_input)
context_bias = tf.nn.embedding_lookup([context_biases], self.__context_input)
weighting_factor = tf.minimum(
1.0,
tf.pow(
tf.div(self.__cooccurrence_count, count_max),
scaling_factor))
embedding_product = tf.reduce_sum(tf.multiply(focal_embedding, context_embedding), 1)
log_cooccurrences = tf.log(tf.to_float(self.__cooccurrence_count))
distance_expr = tf.square(tf.add_n([
embedding_product,
focal_bias,
context_bias,
tf.negative(log_cooccurrences)]))
single_losses = tf.multiply(weighting_factor, distance_expr)
self.__total_loss = tf.reduce_sum(single_losses)
tf.summary.scalar("GloVe_loss", self.__total_loss)
self.__optimizer = tf.train.AdagradOptimizer(self.learning_rate).minimize(
self.__total_loss)
self.__summary = tf.summary.merge_all()
self.__combined_embeddings = tf.add(focal_embeddings, context_embeddings,
name="combined_embeddings")
def train(self, num_epochs, log_dir=None, summary_batch_interval=1000,
tsne_epoch_interval=None):
should_write_summaries = log_dir is not None and summary_batch_interval
should_generate_tsne = log_dir is not None and tsne_epoch_interval
batches = self.__prepare_batches()
total_steps = 0
loss = 0
with tf.Session(graph=self.__graph) as session:
if should_write_summaries:
print("Writing TensorBoard summaries to {}".format(log_dir))
summary_writer = tf.summary.FileWriter(log_dir, graph=session.graph)
tf.global_variables_initializer().run()
for epoch in range(num_epochs):
shuffle(batches)
start = time.time()
for batch_index, batch in enumerate(batches):
i_s, j_s, counts = batch
if len(counts) != self.batch_size:
continue
feed_dict = {
self.__focal_input: i_s,
self.__context_input: j_s,
self.__cooccurrence_count: counts}
_, train_loss = session.run([self.__optimizer, self.__total_loss], feed_dict=feed_dict)
loss += train_loss
if should_write_summaries and (total_steps + 1) % summary_batch_interval == 0:
period_loss = loss / summary_batch_interval
end = time.time()
print("Epoch {}/{}".format(epoch+1, num_epochs),
"Iteration: {}".format(total_steps + 1),
"Avg. Training loss: {:.4f}".format(period_loss),
"{:.4f} sec/batch".format((end - start) * 1.0 / summary_batch_interval))
summary_str = session.run(self.__summary, feed_dict=feed_dict)
summary_writer.add_summary(summary_str, total_steps)
loss = 0
start = time.time()
total_steps += 1
if should_generate_tsne and (epoch + 1) % tsne_epoch_interval == 0:
current_embeddings = self.__combined_embeddings.eval()
output_path = os.path.join(log_dir, "epoch{:03d}.png".format(epoch + 1))
self.generate_tsne(output_path, embeddings=current_embeddings)
self.__embeddings = self.__combined_embeddings.eval()
if should_write_summaries:
summary_writer.close()
def embedding(self, word_str_or_id):
if isinstance(word_str_or_id, str):
return self.embeddings[self.__word_to_id[word_str_or_id]]
elif isinstance(word_str_or_id, int):
return self.embeddings[word_str_or_id]
def __prepare_batches(self):
if self.__cooccurrence_matrix is None:
raise NotFitToCorpusError(
"Need to fit model to corpus before preparing training batches.")
cooccurrences = [(word_ids[0], word_ids[1], count)
for word_ids, count in self.__cooccurrence_matrix.items()]
i_indices, j_indices, counts = zip(*cooccurrences)
return list(_batchify(self.batch_size, i_indices, j_indices, counts))
@property
def vocab_size(self):
return len(self.__words)
@property
def words(self):
if self.__words is None:
raise NotFitToCorpusError("Need to fit model to corpus before accessing words.")
return self.__words
@property
def embeddings(self):
if self.__embeddings is None:
raise NotTrainedError("Need to train model before accessing embeddings")
return self.__embeddings
def id_for_word(self, word):
if self.__word_to_id is None:
raise NotFitToCorpusError("Need to fit model to corpus before looking up word ids.")
return self.__word_to_id[word]
def generate_tsne(self, path=None, size=(100, 100), word_count=1000, embeddings=None):
if embeddings is None:
embeddings = self.embeddings
from sklearn.manifold import TSNE
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
low_dim_embs = tsne.fit_transform(embeddings[:word_count, :])
labels = self.words[:word_count]
return _plot_with_labels(low_dim_embs, labels, path, size)
def _context_windows(region, left_size, right_size):
for i, word in enumerate(region):
start_index = i - left_size
end_index = i + right_size
left_context = _window(region, start_index, i - 1)
right_context = _window(region, i + 1, end_index)
yield (left_context, word, right_context)
def _window(region, start_index, end_index):
"""
Returns the list of words starting from `start_index`, going to `end_index`
taken from region. If `start_index` is a negative number, or if `end_index`
is greater than the index of the last word in region, this function will pad
its return value with `NULL_WORD`.
"""
last_index = len(region) + 1
selected_tokens = region[max(start_index, 0):min(end_index, last_index) + 1]
return selected_tokens
def _device_for_node(n):
if n.type == "MatMul":
return "/gpu:0"
else:
return "/cpu:0"
def _batchify(batch_size, *sequences):
for i in range(0, len(sequences[0]), batch_size):
yield tuple(sequence[i:i+batch_size] for sequence in sequences)
def _plot_with_labels(low_dim_embs, labels, path, size):
import matplotlib.pyplot as plt
assert low_dim_embs.shape[0] >= len(labels), "More labels than embeddings"
figure = plt.figure(figsize=size) # in inches
for i, label in enumerate(labels):
x, y = low_dim_embs[i, :]
plt.scatter(x, y)
plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right',
va='bottom')
if path is not None:
figure.savefig(path)
plt.close(figure)