-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword2vec.py
262 lines (213 loc) · 7.62 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
#%%
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector
import os
import socket
import collections
import math
import time
#%%
mp_dir = "/Users/puyangchen/go/src/github.com/agilab/cedric/tensorflow"
deepbox_dir = "/home/chenpuyang/Projects/keyword/keyword_nlp"
vocabulary_size = 1000000
filename = 'words.txt'
num_steps = 1000000
if 'macbook' in socket.gethostname().lower():
assert(os.path.exists(mp_dir))
os.chdir(mp_dir)
curdir = mp_dir
log_dir = os.path.abspath(os.path.join(curdir, '..', 'log'))
if not os.path.exists(log_dir):
os.makedirs(log_dir)
input_file = os.path.abspath(os.path.join(curdir, '..', filename))
assert os.path.exists(input_file)
elif 'deepbox' in socket.gethostname().lower():
assert(os.path.exists(deepbox_dir))
os.chdir(deepbox_dir)
curdir = deepbox_dir
log_dir = os.path.abspath(os.path.join(curdir, '..', 'log'))
if not os.path.exists(log_dir):
os.makedirs(log_dir)
input_file = os.path.abspath(os.path.join(curdir, '..', 'data', filename))
assert os.path.exists(input_file)
else:
print('Unknown host.')
#%%
from util import record_cost
#%%
window_size = 2
batch_size = 128
embedding_size = 200
num_sampled = 64
learning_rate = 1.0
#%%
@record_cost
def read_file(filename):
with open(filename, "r", encoding='UTF-8') as f:
file = []
line = f.readline()
while line:
file.append(line.strip().split(" "))
line = f.readline()
return file
words = read_file(input_file)
#%%
@record_cost
def build_dataset(words, n_words):
index = dict()
inverted_index = dict()
raw_data = list()
count = [["UNK", -1]]
count.extend(collections.Counter([y for x in words for y in x]).most_common(n_words))
for word, _ in count:
n = len(index)-1
index[word] = n
inverted_index[n] = word
unk_count = 0
for i in words:
ids = list()
for j in i:
id = index.get(j, -1)
ids.append(id)
if id == -1:
unk_count += 1
raw_data.append(ids)
count[0][1] = unk_count
return index, inverted_index, raw_data, count
index, inverted_index, raw_data, count = build_dataset(words, vocabulary_size)
del words
#%%
line_index = 0
element_index = 0
window_index = -window_size
# @record_cost
def generate_input(raw_data, window_size, batch_size):
global line_index, element_index, window_index
word = np.ndarray(shape=(batch_size), dtype=np.int32)
label = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
iid = 0
while line_index < len(raw_data):
line = raw_data[line_index]
while element_index < len(line):
element = line[element_index]
# positive cases
if element == -1:
element_index += 1
window_index += 1
continue
while window_index <= window_size:
# print('line', line_index, 'element', element_index, 'window', window_index)
if element_index+window_index<0 or element_index+window_index>=len(line) or window_index==0:
window_index += 1
continue
if line[element_index+window_index] == -1:
window_index += 1
continue
# print('word', element, 'label', line[element_index+window_index])
word[iid] = element
label[iid][0] = line[element_index+window_index]
window_index += 1
iid += 1
if iid == batch_size:
return word, label
element_index += 1
window_index = -window_size
line_index += 1
element_index = 0
if line_index == len(raw_data):
line_index = 0
# #%%
# word, label = generate_input(raw_data, window_size, 10)
# print(word)
# print(label)
#%%
graph = tf.Graph()
with graph.as_default():
with tf.name_scope('inputs'):
train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
with tf.device('/cpu:0'):
with tf.name_scope('embeddings'):
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
with tf.name_scope('weights'):
nce_weights = tf.Variable(
tf.truncated_normal(
[vocabulary_size, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)
)
)
with tf.name_scope('biases'):
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
with tf.name_scope('loss'):
loss = tf.reduce_mean(
tf.nn.nce_loss(
weights=nce_weights,
biases=nce_biases,
labels=train_labels,
inputs=embed,
num_sampled=num_sampled,
num_classes=vocabulary_size
)
)
tf.summary.scalar('loss', loss)
with tf.name_scope('optimizer'):
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keepdims=True))
normalized_embeddings = embeddings/norm
merged = tf.summary.merge_all()
init = tf.global_variables_initializer()
saver = tf.train.Saver()
#%%
with tf.Session(graph=graph) as sess:
writer = tf.summary.FileWriter(log_dir, sess.graph)
if os.path.exists(os.path.join(log_dir, 'model.ckpt.index')):
saver.restore(sess, os.path.join(log_dir, 'model.ckpt'))
print('restore from checkpoint file')
else:
init.run()
print('initialized')
average_loss = 0
last_checkpoint_time = 0
for step in range(num_steps):
batch_inputs, batch_labels = generate_input(raw_data, window_size, batch_size)
feed_dict = {
train_inputs: batch_inputs,
train_labels: batch_labels,
}
run_metadata = tf.RunMetadata()
_, summary, loss_val = sess.run(
[optimizer, merged, loss],
feed_dict=feed_dict,
run_metadata=run_metadata
)
average_loss += loss_val
writer.add_summary(summary, step)
if step == (num_steps-1):
writer.add_run_metadata(run_metadata, 'step%d' % step)
if step % 2000 == 0:
if step > 0:
average_loss /= 2000
print('Average loss at step ', step, ': ', average_loss)
average_loss = 0
now = time.time()
if now - last_checkpoint_time > 20 * 1:
saver.save(sess, os.path.join(log_dir, 'model.ckpt'))
last_checkpoint_time = time.time()
final_embeddings = normalized_embeddings.eval()
with open(log_dir+'/metadata.tsv', 'w', encoding='utf-8') as f:
for i in range(vocabulary_size):
out = ['{:.6f}'.format(a) for a in final_embeddings[i]]
f.write(inverted_index[i]+' '+' '.join(out)+'\n')
# Create a configuration for visualizeing embeddings with the labels in TensorBoard
config = projector.ProjectorConfig()
embedding_conf = config.embeddings.add()
embedding_conf.tensor_name = embeddings.name
embedding_conf.metadata_path = os.path.join(log_dir, 'metadata.tsv')
projector.visualize_embeddings(writer, config)
writer.close()