/
utils.py
188 lines (144 loc) · 5.53 KB
/
utils.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
import numpy as np
import random
from collections import deque
# PyTorch
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import torch.optim as optim
# With loaded embedding matrix, the padding vector will be initialized to zero
# and will not be trained. Hopefully this isn't a problem. It seems better than
# random initialization...
PADDING_TOKEN = "*PADDING*"
# Temporary hack: Map UNK to "_" when loading pretrained embedding matrices:
# it's a common token that is pretrained, but shouldn't look like any content words.
UNK_TOKEN = "_"
CORE_VOCABULARY = {PADDING_TOKEN: 0,
UNK_TOKEN: 1}
def BuildVocabularyForTextEmbeddingFile(path, types_in_data, core_vocabulary):
"""Quickly iterates through a GloVe-formatted text vector file to
extract a working vocabulary of words that occur both in the data and
in the vector file."""
vocabulary = {}
vocabulary.update(core_vocabulary)
next_index = len(vocabulary)
with open(path, 'rU') as f:
for line in f:
spl = line.split(" ", 1)
word = unicode(spl[0].decode('UTF-8'))
if word in types_in_data and word not in vocabulary:
vocabulary[word] = next_index
next_index += 1
return vocabulary
def LoadEmbeddingsFromText(vocabulary, embedding_dim, path):
"""Prepopulates a numpy embedding matrix indexed by vocabulary with
values from a GloVe - format vector file.
For now, values not found in the file will be set to zero."""
emb = np.zeros(
(len(vocabulary), embedding_dim), dtype=np.float32)
with open(path, 'r') as f:
for line in f:
spl = line.split(" ")
word = spl[0]
if word in vocabulary:
emb[vocabulary[word], :] = [float(e) for e in spl[1:]]
return emb
def MakeDataIterator(examples, batch_size, forever=True, smart_batching=True, num_buckets=10):
if smart_batching:
def data_iter():
def build_bucketed_batch_indices():
batches = []
lengths = [(i, len(e.tokens)) for i, e in enumerate(examples)]
# Shuffle before bucketing.
random.shuffle(lengths)
bucket_size = len(examples) // num_buckets
buckets = [lengths[i*bucket_size:(i+1)*bucket_size] for i in range(num_buckets)]
buckets = [sorted(b, key=lambda x: x[1]) for b in buckets]
for b in buckets:
num_batches = len(b) // batch_size
for i in range(num_batches):
_batch = b[i*batch_size:(i+1)*batch_size]
_batch = [x[0] for x in _batch]
batches.append(_batch)
# Shuffle after bucketing
random.shuffle(batches)
return batches
batch_indices = build_bucketed_batch_indices()
num_batches = len(batch_indices)
start = -1
while True:
start += 1
if start >= num_batches:
if not forever:
break
# Start another epoch.
batch_indices = build_bucketed_batch_indices()
num_batches = len(batch_indices)
start = 0
yield tuple(examples[i] for i in batch_indices[start])
else:
def data_iter():
dataset_size = len(examples)
start = -1 * batch_size
order = range(dataset_size)
random.shuffle(order)
while True:
start += batch_size
if start > dataset_size - batch_size:
if not forever:
break
# Start another epoch.
start = 0
random.shuffle(order)
batch_indices = order[start:start + batch_size]
yield tuple(examples[i] for i in batch_indices)
return data_iter
def Tokenize(examples, vocabulary):
for e in examples:
e.tokens = [vocabulary.get(w, vocabulary.get(UNK_TOKEN)) for w in e.tokens]
return examples
class Accumulator(object):
cache = dict()
def __init__(self, trail=100):
self.trail = trail
def add(self, key, val):
self.cache.setdefault(key, deque(maxlen=self.trail)).append(val)
def get(self, key):
ret = self.cache.get(key, [])
try:
del self.cache[key]
except:
pass
return ret
def get_avg(self, key):
return np.array(self.get(key)).mean()
class Args(object):
def __repr__(self):
s = "{}"
return s.format(self.__dict__)
def make_batch(examples, dynamic=True):
# Build lengths.
lengths = []
for e in examples:
lengths.append(len(e.tokens))
# Build input.
if dynamic: # dynamic: list of lists
data = []
for e in examples:
d = list(reversed(e.tokens[:]))
data.append(d)
else: # static: batch matrix
batch_size = len(examples)
max_len = max(len(e.tokens) for e in examples)
data = torch.zeros(batch_size, max_len).long()
for i, e in enumerate(examples):
l = len(e.tokens)
offset = max_len - l
data[i,offset:max_len] = torch.Tensor(e.tokens[:])
# Build labels.
target = []
for e in examples:
target.append(e.label)
target = torch.LongTensor(target)
return data, target, lengths