-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathword_embedding.py
More file actions
172 lines (150 loc) · 6.12 KB
/
word_embedding.py
File metadata and controls
172 lines (150 loc) · 6.12 KB
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
"""
Adapted from PyTorch's text library.
"""
import array
import os
import zipfile
import six
import torch
from six.moves.urllib.request import urlretrieve
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.nn.init as init
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence
import config
from config import qa_path
class TextProcessor(nn.Module):
def __init__(self, classes, embedding_features, lstm_features, drop=0.0, use_hidden=True, use_tanh=False, only_embed=False):
super(TextProcessor, self).__init__()
self.use_hidden = use_hidden # return last layer hidden, else return all the outputs for each words
self.use_tanh = use_tanh
self.only_embed = only_embed
classes = list(classes)
self.embed = nn.Embedding(len(classes)+1, embedding_features, padding_idx=len(classes))
weight_init = torch.from_numpy(np.load(qa_path+'/glove6b_init_300d.npy'))
assert weight_init.shape == (len(classes), embedding_features)
print('glove weight shape: ', weight_init.shape)
self.embed.weight.data[:len(classes)] = weight_init
print('word embed shape: ', self.embed.weight.shape)
self.drop = nn.Dropout(drop)
if self.use_tanh:
self.tanh = nn.Tanh()
if not self.only_embed:
self.lstm = nn.GRU(input_size=embedding_features,
hidden_size=lstm_features,
num_layers=1,
batch_first=not use_hidden,)
def forward(self, q, q_len):
embedded = self.embed(q)
embedded = self.drop(embedded)
if self.use_tanh:
embedded = self.tanh(embedded)
if self.only_embed:
return embedded
self.lstm.flatten_parameters()
if self.use_hidden:
packed = pack_padded_sequence(embedded, q_len, batch_first=True)
_, hid = self.lstm(packed)
return hid.squeeze(0)
else:
out, _ = self.lstm(embedded)
return out
#embed_vecs = obj_edge_vectors(classes, wv_dim=embedding_features)
#self.embed.weight.data = embed_vecs.clone()
def obj_edge_vectors(names, wv_type='glove.6B', wv_dir=qa_path, wv_dim=300):
wv_dict, wv_arr, wv_size = load_word_vectors(wv_dir, wv_type, wv_dim)
vectors = torch.Tensor(len(names), wv_dim)
vectors.normal_(0,1)
failed_token = []
for i, token in enumerate(names):
wv_index = wv_dict.get(token, None)
if wv_index is not None:
vectors[i] = wv_arr[wv_index]
else:
# Try the longest word (hopefully won't be a preposition
lw_token = sorted(token.split(' '), key=lambda x: len(x), reverse=True)[0]
#print("{} -> {} ".format(token, lw_token))
wv_index = wv_dict.get(lw_token, None)
if wv_index is not None:
vectors[i] = wv_arr[wv_index]
else:
failed_token.append(token)
if (len(failed_token) > 0):
print('Num of failed tokens: ', len(failed_token))
#print(failed_token)
return vectors
URL = {
'glove.42B': 'http://nlp.stanford.edu/data/glove.42B.300d.zip',
'glove.840B': 'http://nlp.stanford.edu/data/glove.840B.300d.zip',
'glove.twitter.27B': 'http://nlp.stanford.edu/data/glove.twitter.27B.zip',
'glove.6B': 'http://nlp.stanford.edu/data/glove.6B.zip',
}
def load_word_vectors(root, wv_type, dim):
"""Load word vectors from a path, trying .pt, .txt, and .zip extensions."""
if isinstance(dim, int):
dim = str(dim) + 'd'
fname = os.path.join(root, wv_type + '.' + dim)
if os.path.isfile(fname + '.pt'):
fname_pt = fname + '.pt'
print('loading word vectors from', fname_pt)
return torch.load(fname_pt)
if os.path.isfile(fname + '.txt'):
fname_txt = fname + '.txt'
cm = open(fname_txt, 'rb')
cm = [line for line in cm]
elif os.path.basename(wv_type) in URL:
url = URL[wv_type]
print('downloading word vectors from {}'.format(url))
filename = os.path.basename(fname)
if not os.path.exists(root):
os.makedirs(root)
with tqdm(unit='B', unit_scale=True, miniters=1, desc=filename) as t:
fname, _ = urlretrieve(url, fname, reporthook=reporthook(t))
with zipfile.ZipFile(fname, "r") as zf:
print('extracting word vectors into {}'.format(root))
zf.extractall(root)
if not os.path.isfile(fname + '.txt'):
raise RuntimeError('no word vectors of requested dimension found')
return load_word_vectors(root, wv_type, dim)
else:
raise RuntimeError('unable to load word vectors')
wv_tokens, wv_arr, wv_size = [], array.array('d'), None
if cm is not None:
for line in tqdm(range(len(cm)), desc="loading word vectors from {}".format(fname_txt)):
entries = cm[line].strip().split(b' ')
word, entries = entries[0], entries[1:]
if wv_size is None:
wv_size = len(entries)
try:
if isinstance(word, six.binary_type):
word = word.decode('utf-8')
except:
print('non-UTF8 token', repr(word), 'ignored')
continue
wv_arr.extend(float(x) for x in entries)
wv_tokens.append(word)
wv_dict = {word: i for i, word in enumerate(wv_tokens)}
wv_arr = torch.Tensor(wv_arr).view(-1, wv_size)
ret = (wv_dict, wv_arr, wv_size)
torch.save(ret, fname + '.pt')
return ret
def reporthook(t):
"""https://github.com/tqdm/tqdm"""
last_b = [0]
def inner(b=1, bsize=1, tsize=None):
"""
b: int, optionala
Number of blocks just transferred [default: 1].
bsize: int, optional
Size of each block (in tqdm units) [default: 1].
tsize: int, optional
Total size (in tqdm units). If [default: None] remains unchanged.
"""
if tsize is not None:
t.total = tsize
t.update((b - last_b[0]) * bsize)
last_b[0] = b
return inner