-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
420 lines (351 loc) · 12.4 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import collections, gzip, time
import numpy as np
import tensorflow as tf
class MediumConfig(object):
"""Medium config."""
init_scale = 0.05
learning_rate = 0.25
max_grad_norm = 20
num_layers = 3
num_steps = 50
hidden_size = 1500
max_epoch = 14
max_max_epoch = 50
keep_prob = 0.3
# correction: for wsj model, we use 0.9.
lr_decay = 0.9
batch_size = 20
class PTBModel(object):
def __init__(self, is_training, config):
self.batch_size = batch_size = config.batch_size
self.num_steps = num_steps = config.num_steps
size = config.hidden_size
vocab_size = config.vocab_size
self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps])
self._targets = tf.placeholder(tf.int32, [batch_size, num_steps])
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(size, forget_bias=1.0,
state_is_tuple=True)
if is_training and config.keep_prob < 1:
lstm_cell = tf.nn.rnn_cell.DropoutWrapper(
lstm_cell, output_keep_prob=config.keep_prob)
cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * config.num_layers,
state_is_tuple=True)
self._initial_state = cell.zero_state(batch_size, tf.float32)
with tf.device("/cpu:0"):
embedding = tf.get_variable("embedding", [vocab_size, size])
inputs = tf.nn.embedding_lookup(embedding, self._input_data)
if is_training and config.keep_prob < 1:
inputs = tf.nn.dropout(inputs, config.keep_prob)
inputs = [tf.squeeze(input_, [1])
for input_ in tf.split(1, num_steps, inputs)]
outputs, state = tf.nn.rnn(cell, inputs, initial_state=self._initial_state)
output = tf.reshape(tf.concat(1, outputs), [-1, size])
softmax_w = tf.get_variable("softmax_w", [size, vocab_size])
softmax_b = tf.get_variable("softmax_b", [vocab_size])
logits = tf.matmul(output, softmax_w) + softmax_b
loss = tf.nn.seq2seq.sequence_loss_by_example(
[logits],
[tf.reshape(self._targets, [-1])],
[tf.ones([batch_size * num_steps])])
cost = tf.reduce_sum(loss) / batch_size
self._cost = loss
self._final_state = state
if not is_training:
return
self._lr = tf.Variable(0.0, trainable=False)
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars),
config.max_grad_norm)
optimizer = tf.train.GradientDescentOptimizer(self.lr)
self._train_op = optimizer.apply_gradients(zip(grads, tvars))
def assign_lr(self, session, lr_value):
session.run(tf.assign(self.lr, lr_value))
@property
def input_data(self):
return self._input_data
@property
def targets(self):
return self._targets
@property
def initial_state(self):
return self._initial_state
@property
def cost(self):
return self._cost
@property
def final_state(self):
return self._final_state
@property
def lr(self):
return self._lr
@property
def train_op(self):
return self._train_op
def _build_vocab(filename):
data = _read_words(filename)
counter = collections.Counter(data)
count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))
words, _ = list(zip(*count_pairs))
word_to_id = dict(zip(words, range(len(words))))
return word_to_id
def _read_words(filename):
with open_file(filename) as f:
return f.read().replace('\n', '<eos>').split()
def chop(data, eos):
new_data = []
sent = []
for w in data:
sent.append(w)
if w == eos:
new_data.append(sent)
sent = []
return new_data
def open_file(path):
if path.endswith('.gz'):
return gzip.open(path, 'rb')
else:
return open(path, 'r')
def ptb_iterator(raw_data, batch_size, num_steps):
raw_data = np.array(raw_data, dtype=np.int32)
data_len = len(raw_data)
batch_len = data_len // batch_size
data = np.zeros([batch_size, batch_len], dtype=np.int32)
for i in range(batch_size):
data[i] = raw_data[batch_len * i:batch_len * (i + 1)]
epoch_size = (batch_len - 1) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
x = data[:, i*num_steps:(i+1)*num_steps]
y = data[:, i*num_steps+1:(i+1)*num_steps+1]
yield (x, y)
# iterator used for nbest data.
def ptb_iterator2(raw_data, batch_size, num_steps, idx2tree, eos):
dummy1 = 0
dummy2 = (-1, -1)
remainder = len(raw_data) % batch_size
if remainder != 0:
raw_data = raw_data + [dummy1 for x in xrange(batch_size - remainder)]
idx2tree = idx2tree + [dummy2 for x in xrange(batch_size - remainder)]
raw_data = np.array(raw_data, dtype=np.int32)
data_len = len(raw_data)
batch_len = data_len // batch_size
remainder = (data_len // batch_size) % num_steps
data = np.zeros([batch_size, batch_len + num_steps - remainder + 1],
dtype=np.int32)
for i in range(batch_size):
data[i, 1:batch_len+1] = raw_data[batch_len * i:batch_len * (i + 1)]
if i == 0:
data[i, 0] = eos
else:
data[i, 0] = raw_data[batch_len - 1]
idx2tree = np.array(idx2tree, dtype=np.dtype('int, int'))
tree = np.zeros([batch_size, batch_len + num_steps - remainder],
dtype=np.dtype('int, int'))
for i in range(batch_size):
tree[i, :batch_len] = idx2tree[batch_len * i:batch_len * (i + 1)]
tree[i, batch_len:] = [dummy2 for x in xrange(num_steps - remainder)]
epoch_size = (batch_len + num_steps - remainder) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
x = data[:, i*num_steps:(i+1)*num_steps]
y = data[:, i*num_steps+1:(i+1)*num_steps+1]
z = tree[:, i*num_steps:(i+1)*num_steps]
yield (x, y, z)
def run_epoch(session, m, data, eval_op, verbose=False):
"""Runs the model on the given data."""
epoch_size = ((len(data) // m.batch_size) - 1) // m.num_steps
start_time = time.time()
costs = 0.0
iters = 0
state = []
for c, h in m.initial_state: # initial_state: ((c1, m1), (c2, m2))
state.append((c.eval(), h.eval()))
for step, (x, y) in enumerate(ptb_iterator(data, m.batch_size,
m.num_steps)):
fetches = []
fetches.append(m.cost)
fetches.append(eval_op)
for c, h in m.final_state: # final_state: ((c1, m1), (c2, m2))
fetches.append(c)
fetches.append(h)
feed_dict = {}
feed_dict[m.input_data] = x
feed_dict[m.targets] = y
for i, (c, h) in enumerate(m.initial_state):
feed_dict[c], feed_dict[h] = state[i]
res = session.run(fetches, feed_dict)
cost = res[0]
state_flat = res[2:] # [c1, m1, c2, m2]
state = [state_flat[i:i+2] for i in range(0, len(state_flat), 2)]
costs += np.sum(cost) / m.batch_size
iters += m.num_steps
if verbose and step % (epoch_size // 10) == 10:
print("%.3f perplexity: %.3f speed: %.0f wps" %
(step * 1.0 / epoch_size, np.exp(costs / iters),
iters * m.batch_size / (time.time() - start_time)))
return np.exp(costs / iters)
def run_epoch2(session, m, nbest, eval_op, eos, verbose=False):
"""Runs the model on the given data."""
counts = []
loss = []
prev = (-1, -1)
for pair in nbest['idx2tree']:
if pair[0] != prev[0]:
counts.append([0])
loss.append([0.])
elif pair[1] == prev[1] + 1:
counts[-1].append(0)
loss[-1].append(0.)
counts[-1][-1] += 1
prev = pair
data = nbest['data']
epoch_size = ((len(data) // m.batch_size) - 1) // m.num_steps
start_time = time.time()
costs = 0.0
iters = 0
state = []
for c, h in m.initial_state: # initial_state: ((c1, m1), (c2, m2))
state.append((c.eval(), h.eval()))
for step, (x, y, z) in enumerate(
ptb_iterator2(data, m.batch_size, m.num_steps,
nbest['idx2tree'], eos)):
fetches = []
fetches.append(m.cost)
fetches.append(eval_op)
for c, h in m.final_state: # final_state: ((c1, m1), (c2, m2))
fetches.append(c)
fetches.append(h)
feed_dict = {}
feed_dict[m.input_data] = x
feed_dict[m.targets] = y
for i, (c, h) in enumerate(m.initial_state):
feed_dict[c], feed_dict[h] = state[i]
res = session.run(fetches, feed_dict)
cost = res[0]
state_flat = res[2:] # [c1, m1, c2, m2]
state = [state_flat[i:i+2] for i in range(0, len(state_flat), 2)]
costs += np.sum(cost) / m.batch_size
iters += m.num_steps
cost = cost.reshape((m.batch_size, m.num_steps))
for idx, val in np.ndenumerate(cost):
tree_idx = z[idx[0]][idx[1]]
if tree_idx[0] == -1: # dummy
continue
counts[tree_idx[0]][tree_idx[1]] -= 1
loss[tree_idx[0]][tree_idx[1]] += cost[idx[0]][idx[1]]
if verbose and step % (epoch_size // 10) == 10:
print("%.3f perplexity: %.3f speed: %.0f wps" %
(step * 1.0 / epoch_size, np.exp(costs / iters),
iters * m.batch_size / (time.time() - start_time)))
scores = nbest['scores']
num = 0
gold, test, matched = 0, 0, 0
bad = []
for i in xrange(len(scores)):
good = True
ag = 0
min_val = 10000000
for j in xrange(len(scores[i])):
if counts[i][j] != 0:
bad.append(i)
good = False
break
if loss[i][j] < min_val:
min_val = loss[i][j]
ag = j
if good:
num += 1
gold += scores[i][ag]['gold']
test += scores[i][ag]['test']
matched += scores[i][ag]['matched']
if bad:
print('bad: %s' % ', '.join([str(x) for x in bad]))
return 200. * matched / (gold + test), num
def unkify(ws):
uk = 'unk'
sz = len(ws)-1
if ws[0].isupper():
uk = 'c' + uk
if ws[0].isdigit() and ws[sz].isdigit():
uk = uk + 'n'
elif sz <= 2:
pass
elif ws[sz-2:sz+1] == 'ing':
uk = uk + 'ing'
elif ws[sz-1:sz+1] == 'ed':
uk = uk + 'ed'
elif ws[sz-1:sz+1] == 'ly':
uk = uk + 'ly'
elif ws[sz] == 's':
uk = uk + 's'
elif ws[sz-2:sz+1] == 'est':
uk = uk + 'est'
elif ws[sz-1:sz+1] == 'er':
uk = uk + 'ER'
elif ws[sz-2:sz+1] == 'ion':
uk = uk + 'ion'
elif ws[sz-2:sz+1] == 'ory':
uk = uk + 'ory'
elif ws[0:2] == 'un':
uk = 'un' + uk
elif ws[sz-1:sz+1] == 'al':
uk = uk + 'al'
else:
for i in xrange(sz):
if ws[i] == '-':
uk = uk + '-'
break
elif ws[i] == '.':
uk = uk + '.'
break
return '<' + uk + '>'
# iterator for nbest trees
def nbest_iterator(raw_data, batch_size, num_steps, idx2tree, eos):
dummy1 = 0
dummy2 = (-1, -1)
remainder = len(raw_data) % batch_size
if remainder != 0:
raw_data = raw_data + [dummy1 for x in xrange(batch_size - remainder)]
idx2tree = idx2tree + [dummy2 for x in xrange(batch_size - remainder)]
raw_data = np.array(raw_data, dtype=np.int32)
data_len = len(raw_data)
batch_len = data_len // batch_size
remainder = (data_len // batch_size) % num_steps
data = np.zeros([batch_size, batch_len + num_steps - remainder + 1],
dtype=np.int32)
for i in range(batch_size):
data[i, 1:batch_len+1] = raw_data[batch_len * i:batch_len * (i + 1)]
if i == 0:
data[i, 0] = eos
else:
data[i, 0] = raw_data[batch_len - 1]
idx2tree = np.array(idx2tree, dtype=np.dtype('int, int'))
tree = np.zeros([batch_size, batch_len + num_steps - remainder],
dtype=np.dtype('int, int'))
for i in range(batch_size):
tree[i, :batch_len] = idx2tree[batch_len * i:batch_len * (i + 1)]
tree[i, batch_len:] = [dummy2 for x in xrange(num_steps - remainder)]
epoch_size = (batch_len + num_steps - remainder) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
x = data[:, i*num_steps:(i+1)*num_steps]
y = data[:, i*num_steps+1:(i+1)*num_steps+1]
z = tree[:, i*num_steps:(i+1)*num_steps]
yield (x, y, z)
def ptb_iterator(raw_data, batch_size, num_steps):
raw_data = np.array(raw_data, dtype=np.int32)
data_len = len(raw_data)
batch_len = data_len // batch_size
data = np.zeros([batch_size, batch_len], dtype=np.int32)
for i in range(batch_size):
data[i] = raw_data[batch_len * i:batch_len * (i + 1)]
epoch_size = (batch_len - 1) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
x = data[:, i*num_steps:(i+1)*num_steps]
y = data[:, i*num_steps+1:(i+1)*num_steps+1]
yield (x, y)