-
Notifications
You must be signed in to change notification settings - Fork 1
/
word2vec.py
150 lines (119 loc) · 4.33 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
import torch, torchvision
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from sklearn.feature_extraction.text import CountVectorizer
import pickle
class Vocab(CountVectorizer):
r"""
We use Count Vectorizer as a Tokenizer. A good extension will give us a Tokenizer with built-in id2word and word2id functions
"""
def __init__(self, texts, **kwargs):
super(Vocab, self).__init__(**kwargs)
self.fit(texts)
self.id2w = self.get_feature_names()
def __len__(self):
return len(self.vocabulary_)
def _to_indices(self, sent):
analyzer = self.build_analyzer()
seq = analyzer(sent)
indices = []
for w in seq:
try:
indices.append(self.vocabulary_[w])
except KeyError:
continue
return indices
def __getitem__(self, id):
return self.id2w[id]
def save_model(model, filename):
print('saving model to %s ...' % filename)
pickle.dump(model, open(filename, 'wb'))
def load_model(filename):
print('loading model from %s ...' % filename)
return pickle.load(open(filename, 'rb'))
CONTEXT_SIZE = 2
class MyWord2Vec(nn.Module):
r"""
Implement model CBOW
"""
def __init__(self, vocab_size, embed_size):
super(MyWord2Vec, self).__init__()
self.embeds = nn.Embedding(vocab_size, embed_size)
self.linear1 = nn.Linear(2 * CONTEXT_SIZE * embed_size, 128)
self.linear2 = nn.Linear(128, vocab_size)
def forward(self, x):
embeds = self.embeds(x).view((x.shape[0], -1))
out = F.relu(self.linear1(embeds))
out = self.linear2(out)
log_probs = F.log_softmax(out, dim=1)
return log_probs
def get_embeds(self, wordid):
return self.embeds(torch.tensor([wordid], dtype=torch.long)).view(-1)
def word_embeds(vocab, w2v):
for w in vocab.vocabulary_:
yield w2v.get_embeds(vocab.vocabulary_[w])
if __name__ == '__main__':
# load dataset
fname = 'data/new-york-times-articles/nytimes_news_articles.txt'
articles = []
errs = 0
with open(fname, 'r') as f:
art = []
while True:
line = f.readline()
if not line:
break
if 'URL' in line:
if art != []:
paper = ' '.join(art)
if 'No corrections appeared in print' not in paper: #error papers
articles.append(paper)
else:
errs += 1
art = []
elif line not in {'\n', ' '}:
art.append(line.rstrip('\n'))
# generate vocab
vocab_size = 8000
kwargs = {'stop_words': 'english', 'max_df': 0.5, 'min_df': 3, 'ngram_range': (1, 1),
'max_features': vocab_size, 'strip_accents': 'unicode'}
vocab = Vocab(articles, **kwargs)
# preprocess data
ctx = []
trg = []
for text in articles:
raw_text = vocab._to_indices(text)
for i in range(2, len(raw_text) - 2):
context = [raw_text[i - 2], raw_text[i - 1],
raw_text[i + 1], raw_text[i + 2]]
target = raw_text[i]
ctx.append(context)
trg.append(target)
ctx = torch.tensor(ctx, dtype=torch.long)
trg = torch.tensor(trg, dtype=torch.long)
# define our model
epochs = 10
embed_size = 50
batch_size = 16
model = MyWord2Vec(vocab_size, embed_size)
optimizer = optim.SGD(model.parameters(), lr=1e-3)
loss_function = nn.NLLLoss()
# training
losses = []
for epoch in range(epochs):
total_loss = 0
for idx in range(0, ctx.shape[0], batch_size):
#for idx, (context, target) in enumerate(data):
context_idxs = ctx[idx: idx+batch_size]
model.zero_grad()
log_probs = model(context_idxs)
loss = loss_function(log_probs, trg[idx: idx+batch_size])
# backward pass and update the gradient
loss.backward()
optimizer.step()
total_loss += loss.item()
if idx % 10 == 0:
print('Ep {}/{} {}/{} loss={:.4f}'.format(epoch, epochs, idx, trg.shape[0], total_loss), flush=True, end='\r')
losses.append(total_loss)
print('\n Loss: %.4f' % losses)