-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucketmodel.py
288 lines (253 loc) · 12.5 KB
/
bucketmodel.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
# Author: M. Tong <demon386@gmail.com>
from __future__ import print_function
import random
import tensorflow as tf
import itertools
import numpy as np
import sys
from collections import defaultdict
from tensorflow.contrib.rnn import BasicRNNCell
from tensorflow.contrib.legacy_seq2seq import embedding_rnn_seq2seq
def seq2seq_pad(encoder_inputs,
encoder_length,
decoder_inputs,
decoder_length,
source_vocab,
target_vocab,
pad_symbol='<PAD>',
unk_symbol='<UNK>'):
"""
- encoder_input: A nested list of symbol str for encoding, length: batch_size
- encoder_length: max length of encoder input
- decoder_input: A nested list of symbol str for decoding, length: batch_size
- decoder_length: max length of decoder input
- vocab: vocabulary index, symbol (str) -> index (int)
Example:
["hello", "world"] -> ["<GO>", "hi", "<EOS>"]
["cover", "me"] -> ["<GO>", "roger", "<EOS>"]
seq2seq_pad([['hello', 'world'], ['cover', 'me']], 4, [['<GO>', hi', '<EOS>'], ['<GO>', 'roger', '<EOS>']], 5, vocab)
Assume that index of "<PAD>" is 0
Output:
[[0, 0, <index of 'hello'>, <index of 'world'>], [0, 0, <index of 'cover'>, <index of 'me'>]],
[[<index of '<GO>'>, <index of 'hi'>, <index of 'EOS'>, 0, 0], [<index of '<GO>'>, <index of 'roger'>, <index of 'EOS'>, 0, 0]]
"""
def to_index(inputs, length, vocab, pad_from_start=True):
inputs_to_index = []
unk_index = vocab[unk_symbol]
for cur_input in inputs:
cur_input_to_index = [vocab[pad_symbol]] * length
l = len(cur_input)
if l < length:
if pad_from_start:
cur_input_to_index[(length - l):] = [
vocab.get(i, unk_index) for i in cur_input
]
else:
cur_input_to_index[:l] = [vocab.get(i, unk_index)
for i in cur_input]
else:
cur_input_to_index = [vocab.get(i, unk_index)
for i in cur_input[:length]]
inputs_to_index.append(cur_input_to_index)
return inputs_to_index
return to_index(encoder_inputs, encoder_length, source_vocab,
True), to_index(decoder_inputs, decoder_length,
target_vocab, False)
def encoder_decoder_ids_to_word_pairs(encoder_ids,
decoder_ids,
reverse_source_vocab,
reverse_target_vocab,
decoder_eos_symbol="<EOS>",
pad_symbol="<PAD>"):
"""
Tensorflow seq2seq input format back to (encoder_words, deocder_words) pairs
"""
encoder_ids = zip(*encoder_ids)
decoder_ids = zip(*decoder_ids)
def ids_to_words(ids, reverse_vocab, remove_pad_from_start=True):
words = (reverse_vocab[i] for i in ids)
words = itertools.takewhile(lambda x: x != decoder_eos_symbol, words)
if remove_pad_from_start:
words = itertools.dropwhile(lambda x: x == pad_symbol, words)
return tuple(words)
encoder_words = map(
lambda ids: ids_to_words(ids, reverse_source_vocab, True), encoder_ids)
decoder_words = map(
lambda ids: ids_to_words(ids, reverse_target_vocab, False),
decoder_ids)
for (encoder, decoder) in zip(encoder_words, decoder_words):
yield (tuple(encoder), tuple(decoder))
class BucketSeq2Seq:
def __init__(self, buckets, num_encoder_symbols, num_decoder_symbols,
embedding_dim, rnn_state_dim):
"""
- buckets: A list of (encoder_length, decoder_length) bucket to put
sample in, with ascending order
- num_encoder_symbols: number of encoder symbols (for encoding word embedding)
- num_decoder_symbols: number of decoder symbols (for decoding word embedding)
- embedding_dim: embedding dimension for input & output
- rnn_state_dim: state dimension for BasicRNNCell
Example: BucketSeq2Seq([(5, 10), (10, 15), (20, 25), (40, 50)])
"""
self.buckets = buckets
self.num_encoder_symbols = num_encoder_symbols
self.num_decoder_symbols = num_decoder_symbols
self.embedding_dim = embedding_dim
self.rnn_state_dim = rnn_state_dim
def _find_bucket_id(self, encoder_size, decoder_size):
"""
Find the smallest bucket id compatible for the given encoder and decoder size
If not found, return None
"""
for bucket_id, (source_size, target_size) in enumerate(self.buckets):
if encoder_size <= source_size and decoder_size <= target_size:
return bucket_id
return None
def _tf_seq2seq_model(self,
encoder_inputs,
decoder_inputs,
do_decode=False):
model = embedding_rnn_seq2seq(encoder_inputs,
decoder_inputs,
self.cell,
self.num_encoder_symbols,
self.num_decoder_symbols,
self.embedding_dim,
output_projection=None,
feed_previous=do_decode)
return model
def create_model(self, optimizer):
max_encoder_size = self.buckets[-1][0]
max_decoder_size = self.buckets[-1][1]
self.encoder_placeholders = [tf.placeholder(tf.int32,
shape=[None],
name="encoder_%d" % i)
for i in range(max_encoder_size)]
self.decoder_placeholders = [tf.placeholder(tf.int32,
shape=[None],
name="decoder_%d" % i)
for i in range(max_decoder_size + 1)]
# number of weights is one less than decoder_placeholders
self.target_weights_placeholders = [tf.placeholder(
tf.float32,
shape=[None],
name="weight_%d" % i) for i in range(max_decoder_size)]
self.cell = BasicRNNCell(self.rnn_state_dim)
self.encoder_cell = BasicRNNCell(self.rnn_state_dim)
# Example:
# decoder_inputs: <GO>, hello, <EOS>
# targets: hello, <EOS>
# The last symbol in decoder_inputs is only used for targets
targets = self.decoder_placeholders[1:]
self.outputs, self.losses = tf.contrib.legacy_seq2seq.model_with_buckets(
self.encoder_placeholders, self.decoder_placeholders[:-1], targets,
self.target_weights_placeholders, self.buckets,
lambda x, y: self._tf_seq2seq_model(x, y, False))
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
self.predict_outputs, _ = tf.contrib.legacy_seq2seq.model_with_buckets(
self.encoder_placeholders, self.decoder_placeholders[:-1],
targets, self.target_weights_placeholders, self.buckets,
lambda x, y: self._tf_seq2seq_model(x, y, True))
self.optimizers = [optimizer.minimize(i) for i in self.losses]
def read_data(self,
data,
source_vocab,
target_vocab,
decoder_start_symbol="<GO>",
decoder_eos_symbol="<EOS>",
pad_symbol="<PAD>",
unk_symbol="<UNK>"):
"""
data: Iterable, the format of the item is ("source_word1", "source_word2") -> ("target_word1", "target_word2", "target_word3")
The function will process the input:
For decoder input:
- prepend "<GO>" to decoder symbols
- apend "<EOS>" and "<PAD>" to decoder symbols
For encoder input:
- prepend "<PAD>" to encoder symbols
"""
self.bucket_id_to_data = defaultdict(list)
self.target_pad_idx = target_vocab[pad_symbol]
for (raw_encoder_input, raw_decoder_input) in data:
encoder_length = len(raw_encoder_input)
# raw length + <GO> and <EOS>
decoder_length = len(raw_decoder_input) + 1
bucket_id = self._find_bucket_id(encoder_length, decoder_length)
if bucket_id is None:
print("Failed to find compatible bucket for input: (%s, %s)." %
(' '.join(raw_encoder_input),
' '.join(raw_decoder_input)),
file=sys.stderr)
continue
# we will prepend "<GO>" and "<EOS> later"
decoder_input_with_start_and_end = [
decoder_start_symbol
] + list(raw_decoder_input) + [decoder_eos_symbol]
self.bucket_id_to_data[bucket_id].append((
raw_encoder_input, decoder_input_with_start_and_end))
# pad the bucket
for (bucket_id, val) in self.bucket_id_to_data.iteritems():
encoder_length, decoder_length = self.buckets[bucket_id]
encoder_inputs, decoder_inputs = zip(*val)
inputs_with_pad = seq2seq_pad(encoder_inputs, encoder_length,
decoder_inputs, decoder_length + 1,
source_vocab, target_vocab,
pad_symbol, unk_symbol)
self.bucket_id_to_data[bucket_id] = (zip(*inputs_with_pad[0]),
zip(*inputs_with_pad[1]))
def train(self, session, num_iteration, callback=None):
# sort buckets by random
buckets_with_data = list(self.bucket_id_to_data.iterkeys())
random.shuffle(buckets_with_data)
bucketid_to_feed_dict = dict()
for bucket_id in buckets_with_data:
feed_dict = self._build_feed_dict(bucket_id)
bucketid_to_feed_dict[bucket_id] = feed_dict
for iteration in range(num_iteration):
for bucket_id in buckets_with_data:
feed_dict = bucketid_to_feed_dict[bucket_id]
# pick out optimizer from the corresponding seq2seq model
optimizer = self.optimizers[bucket_id]
loss = self.losses[bucket_id]
_, loss_val = session.run([optimizer, loss], feed_dict)
if callback:
callback({'iteration': iteration,
'bucketid': bucket_id,
'loss': loss_val})
def decode(self, session, by_bucket=True):
"""
- by_bucket: decode by bucket, will not guarantee the order, but
generally with better performance
"""
# @todo: support by_bucket=False
buckets_with_data = list(self.bucket_id_to_data.iterkeys())
# (input, output, prob_matrix)
output_tuples = [None] * len(self.buckets)
for bucket_id in buckets_with_data:
# pick out optimizer from the corresponding seq2seq model
feed_dict = self._build_feed_dict(bucket_id)
encoder_inputs = self.bucket_id_to_data[bucket_id][0]
predict_output = self.predict_outputs[bucket_id]
predict_labels = [tf.argmax(i, axis=1) for i in predict_output]
predict_labels_val, prob_matrices = session.run(
[predict_labels, predict_output], feed_dict)
output_tuples[bucket_id] = (encoder_inputs, predict_labels_val,
prob_matrices)
return output_tuples
def _build_feed_dict(self, bucket_id):
encoder_inputs, decoder_inputs = self.bucket_id_to_data[bucket_id]
feed_dict = {}
# encoder
for (placeholder, input_) in zip(self.encoder_placeholders,
encoder_inputs):
feed_dict[placeholder] = input_
# decoder
for (placeholder, input_) in zip(self.decoder_placeholders,
decoder_inputs):
feed_dict[placeholder] = input_
# target weights
for (placeholder, input_) in zip(self.target_weights_placeholders,
decoder_inputs[1:]):
# remember to shift by one
feed_dict[placeholder] = np.asarray(input_) != self.target_pad_idx
return feed_dict