-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword2Vec.py
295 lines (260 loc) · 11.9 KB
/
word2Vec.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
"""
Tensorflow implementation of WV-DR algorithm wrapper class(sklearn style)
:authro: Fido Wang ([email protected])
:refer: http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf
"""
import tensorflow as tf
import os
import numpy as np
from option import Option
from functools import reduce
import math
import time
import collections
import random
import pickle
from error import NotTrainedError
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)
data_index = (data_index + 1)
if i == batch_size:
yield batch, labels[:, None], doc_ids[data_index]
i = 0
labels = np.ndarray(shape=(batch_size), dtype=np.int32)
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
class Skipgram(object):
"""
Skip-Gram word embedder
Support Functional Style setting thus all function begin with 'set' and 'build'
will return an object,the Embedding Estimator itself.
"""
def __init__(self, options, vocab=None):
assert isinstance(options, Option)
self._options = options
self.__inputs, self.__labels, self.__lr = None, None, None
self.word_embeddings = None
self.__normalized_word_embeddings = None
self.__cost, self.__optimizer, self. __summary = None, None ,None
self.vocab = vocab
self.reversed_vocab = None if vocab is None else {v: k for k,v in vocab.items()}
self.use_subsampling = False
self._session = None
self.saver = None
def set_vocab(self, vocab):
self.vocab = vocab
self.reversed_vocab = None if vocab is None else {v: k for k, v in vocab.items()}
return self
def use_subsampling(self, switch=True):
self.use_subsampling = switch
return self
def _get_inputs(self):
"""
Create TF Placeholders for input, targets, and learning rate.
:return: Tuple of Placeholders (input, targets, learning rate)
"""
opts = self._options
inputs_ = tf.placeholder(tf.int32, [opts.batch_size], name='input')
labels_ = tf.placeholder(tf.int32, [opts.batch_size, 1], name='target')
lr_ = tf.placeholder(tf.float32, name='learning_rate')
return inputs_, labels_, lr_
def _get_embedding_layer(self, input_data):
"""
Create embedding for <input_data>.
:param input_data: TF placeholder for text input.
:return: Embedded input.
"""
opts = self._options
embedding = tf.Variable(tf.random_uniform((opts.vocab_size, opts.embed_dim), -1, 1))
return embedding, tf.nn.embedding_lookup(embedding, input_data)
def build_graph(self):
"""
Create Graph and Initialize tf Session for training
"""
train_graph = tf.Graph()
self.graph = train_graph
opts = self._options
with train_graph.as_default():
self.__inputs, self.__labels, self.__lr = self._get_inputs()
embeddings, embed = self._get_embedding_layer(self.__inputs)
norm_w = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
self.__normalized_word_embeddings = embeddings / norm_w
weights = tf.Variable(
tf.truncated_normal((opts.vocab_size, opts.embed_dim),
stddev=1.0 / math.sqrt(opts.embed_dim))
)
biases = tf.Variable(tf.zeros(opts.vocab_size))
if opts.loss == 'softmax':
loss = tf.nn.sampled_softmax_loss(weights=weights,
biases=biases,
labels=self.__labels,
inputs=embed,
num_sampled=opts.negative_sample_size,
num_classes=opts.vocab_size)
elif opts.loss == 'nce':
loss = tf.nn.nce_loss(weights=weights,
biases=biases,
labels=self.__labels,
inputs=embed,
num_sampled=opts.negative_sample_size,
num_classes=opts.vocab_size)
self.__cost = tf.reduce_mean(loss)
tf.summary.scalar("w2v_loss", self.__cost)
if opts.train_method == 'Adam':
self.__optimizer = tf.train.AdamOptimizer(self.__lr).minimize(self.__cost)
else:
self.__optimizer = tf.train.GradientDescentOptimizer(self.__lr).minimize(self.__cost)
self.__summary = tf.summary.merge_all()
self._session = tf.Session(graph=train_graph)
self.saver = tf.train.Saver()
return self
@staticmethod
def _get_batches(self, doc_ids, word_ids, batch_size, skip_size, window_size):
n_batches = len(word_ids) // batch_size
words = word_ids[:n_batches * batch_size]
for idx in range(0, len(words), batch_size):
x, y = [], []
batch = words[idx:idx + batch_size]
for ii in range(len(batch)):
batch_x = batch[ii]
batch_y = self._get_target(batch, ii, window_size)
y.extend(batch_y)
x.extend([batch_x] * len(batch_y))
yield np.array(x), np.array(y)[:, None]
@staticmethod
def _get_target(words, idx, window_size=5):
R = np.random.randint(1, window_size + 1)
start = idx - R if (idx - R) > 0 else 0
stop = idx + R
target_words = set(words[start:idx] + words[idx + 1:stop + 1])
return list(target_words)
def _choose_batch_generator(self, chosen='words'):
if chosen == 'words':
self._generate_batches = self._get_batches
if chosen == 'docs':
self._generate_batches = generate_batch_para
def fit(self, train_data):
opts = self._options
iteration = 1
loss = 0
if type(train_data[0]) is int:
word_ids = train_data
doc_ids = [0] * len(train_data)
self._choose_batch_generator(chosen='words')
elif type(train_data[0]) is list:
doc_ids = [[i] * len(j) for i, j in enumerate(train_data)]
doc_ids = [item for sublist in doc_ids for item in sublist]
word_ids = [item for sublist in train_data for item in sublist]
self._choose_batch_generator(chosen='docs')
with self._session as session:
session.run(tf.global_variables_initializer())
for e in range(1, opts.epochs_to_train):
batches = self._generate_batches(doc_ids, word_ids, opts.batch_size, opts.window_size, opts.window_size)
start = time.time()
learning_rate = opts.learning_rate
for x, y, train_idx in batches:
period_loss = 1000000000
learning_rate = opts.learning_rate if learning_rate < period_loss else learning_rate * 0.1
feed = {self.__inputs: x,
self.__labels: y,
self.__lr: learning_rate}
train_loss, _ = session.run([self.__cost, self.__optimizer], feed_dict=feed)
loss += train_loss
if iteration % opts.statistics_interval == 0:
period_loss = loss / opts.statistics_interval
end = time.time()
print("Epoch {}/{}".format(e, opts.epochs_to_train - 1),
"Iteration: {}".format(iteration),
"Current Doc: {}".format(train_idx),
"Avg. Training loss: {:.4f}".format(period_loss),
"{:.4f} sec/batch".format((end - start) * 1.0 / opts.statistics_interval))
loss = 0
start = time.time()
if iteration % opts.checkpoint_interval == 0:
self.saver.save(self._session,
"word2vec",
global_step=iteration)
iteration += 1
self.word_embeddings = self.__normalized_word_embeddings.eval()
self.saver.save(self._session, "final_word2vec")
def transform_w(self, word_index):
if self.word_embeddings is None:
raise NotTrainedError
return self.word_embeddings[word_index, :]
def transform_doc(self, word_indexes):
if self.word_embeddings is None:
raise NotTrainedError
opts = self._options
sample = opts.subsample
doc_len = 0
doc = []
for idx in word_indexes:
#ran = (math.sqrt(self.vocab[idx] / sample * len(self.vocab)) + 1) * (sample * len(self.vocab)) / self.vocab[idx]
#if ran > random.random():
doc_len += 1
doc.append(self.word_embeddings[idx, :])
doc_embeddings = reduce(lambda x,y: x+y, doc)/doc_len
return doc_embeddings
def save(self, path):
"""
To save trained model and its params.
"""
save_path = self.saver.save(self._session,'model.data')
# save parameters of the model
params = self._options
pickle.dump(params,
open(os.path.join(path, 'model_params.json'), 'wb'), pickle.HIGHEST_PROTOCOL)
# save dictionary, reverse_dictionary
pickle.dump(self.vocab,
open(os.path.jooin(path, 'model_dict.json'), 'wb'), pickle.HIGHEST_PROTOCOL)
print("Model saved in file: %s" % save_path)
return save_path
def _restore(self, path):
with self.graph.as_default():
self.saver.restore(self._session, path)
with self._session as session:
session.run(tf.global_variables_initializer())
self.word_embeddings = self.__normalized_word_embeddings.eval()
@classmethod
def restore(cls, path):
"""
To restore a saved model.
"""
# load params of the model
path_dir = os.path.dirname(path)
params = pickle.load(open(os.path.join(path_dir, 'model_params.json'), 'rb'))
# init an instance of this class
estimator = Skipgram(params)
estimator.build_graph();
estimator._restore(os.path.join(path_dir, 'model.data'))
# bind dictionaries
estimator.set_vocab(pickle.load(open(os.path.join(path_dir, 'model_dict.json'), 'rb')))
return estimator