-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.py
288 lines (245 loc) · 10.8 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
import re
import os
import unidecode
import numpy as np
from keras.models import Model, load_model
from keras.layers import Input
from model import truncated_acc, truncated_loss
np.random.seed(1234)
SOS = '\t' # start of sequence.
EOS = '*' # end of sequence.
CHARS = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ')
REMOVE_CHARS = '[#$%"\+@<=>!&,-.?:;()*\[\]^_`{|}~/\d\t\n\r\x0b\x0c]'
class CharacterTable(object):
"""Given a set of characters:
+ Encode them to a one-hot integer representation
+ Decode the one-hot integer representation to their character output
+ Decode a vector of probabilities to their character output
"""
def __init__(self, chars):
"""Initialize character table.
# Arguments
chars: Characters that can appear in the input.
"""
self.chars = sorted(set(chars))
self.char2index = dict((c, i) for i, c in enumerate(self.chars))
self.index2char = dict((i, c) for i, c in enumerate(self.chars))
self.size = len(self.chars)
def encode(self, C, nb_rows):
"""One-hot encode given string C.
# Arguments
C: string, to be encoded.
nb_rows: Number of rows in the returned one-hot encoding. This is
used to keep the # of rows for each data the same via padding.
"""
x = np.zeros((nb_rows, len(self.chars)), dtype=np.float32)
for i, c in enumerate(C):
x[i, self.char2index[c]] = 1.0
return x
def decode(self, x, calc_argmax=True):
"""Decode the given vector or 2D array to their character output.
# Arguments
x: A vector or 2D array of probabilities or one-hot encodings,
or a vector of character indices (used with `calc_argmax=False`).
calc_argmax: Whether to find the character index with maximum
probability, defaults to `True`.
"""
if calc_argmax:
indices = x.argmax(axis=-1)
else:
indices = x
chars = ''.join(self.index2char[ind] for ind in indices)
return indices, chars
def sample_multinomial(self, preds, temperature=1.0):
"""Sample index and character output from `preds`,
an array of softmax probabilities with shape (1, 1, nb_chars).
"""
# Reshaped to 1D array of shape (nb_chars,).
preds = np.reshape(preds, len(self.chars)).astype(np.float64)
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probs = np.random.multinomial(1, preds, 1)
index = np.argmax(probs)
char = self.index2char[index]
return index, char
def read_text(data_path, list_of_books):
text = ''
for book in list_of_books:
file_path = os.path.join(data_path, book)
strings = unidecode.unidecode(open(file_path).read())
text += strings + ' '
return text
def tokenize(text):
tokens = [re.sub(REMOVE_CHARS, '', token)
for token in re.split("[-\n ]", text)]
return tokens
def add_speling_erors(token, error_rate):
"""Simulate some artificial spelling mistakes."""
assert(0.0 <= error_rate < 1.0)
if len(token) < 3:
return token
rand = np.random.rand()
# Here are 4 different ways spelling mistakes can occur,
# each of which has equal chance.
prob = error_rate / 4.0
if rand < prob:
# Replace a character with a random character.
random_char_index = np.random.randint(len(token))
token = token[:random_char_index] + np.random.choice(CHARS) \
+ token[random_char_index + 1:]
elif prob < rand < prob * 2:
# Delete a character.
random_char_index = np.random.randint(len(token))
token = token[:random_char_index] + token[random_char_index + 1:]
elif prob * 2 < rand < prob * 3:
# Add a random character.
random_char_index = np.random.randint(len(token))
token = token[:random_char_index] + np.random.choice(CHARS) \
+ token[random_char_index:]
elif prob * 3 < rand < prob * 4:
# Transpose 2 characters.
random_char_index = np.random.randint(len(token) - 1)
token = token[:random_char_index] + token[random_char_index + 1] \
+ token[random_char_index] + token[random_char_index + 2:]
else:
# No spelling errors.
pass
return token
def transform(tokens, maxlen, error_rate=0.3, shuffle=True):
"""Transform tokens into model inputs and targets.
All inputs and targets are padded to maxlen with EOS character.
"""
if shuffle:
print('Shuffling data.')
np.random.shuffle(tokens)
encoder_tokens = []
decoder_tokens = []
target_tokens = []
for token in tokens:
encoder = add_speling_erors(token, error_rate=error_rate)
encoder += EOS * (maxlen - len(encoder)) # Padded to maxlen.
encoder_tokens.append(encoder)
decoder = SOS + token
decoder += EOS * (maxlen - len(decoder))
decoder_tokens.append(decoder)
target = decoder[1:]
target += EOS * (maxlen - len(target))
target_tokens.append(target)
assert(len(encoder) == len(decoder) == len(target))
return encoder_tokens, decoder_tokens, target_tokens
def batch(tokens, maxlen, ctable, batch_size=128, reverse=False):
"""Split data into chunks of `batch_size` examples."""
def generate(tokens, reverse):
while(True): # This flag yields an infinite generator.
for token in tokens:
if reverse:
token = token[::-1]
yield token
token_iterator = generate(tokens, reverse)
data_batch = np.zeros((batch_size, maxlen, ctable.size),
dtype=np.float32)
while(True):
for i in range(batch_size):
token = next(token_iterator)
data_batch[i] = ctable.encode(token, maxlen)
yield data_batch
def datagen(encoder_iter, decoder_iter, target_iter):
"""Utility function to load data into required model format."""
inputs = zip(encoder_iter, decoder_iter)
while(True):
encoder_input, decoder_input = next(inputs)
target = next(target_iter)
yield ([encoder_input, decoder_input], target)
def decode_sequences(inputs, targets, input_ctable, target_ctable,
maxlen, reverse, encoder_model, decoder_model,
nb_examples, sample_mode='argmax', random=True):
input_tokens = []
target_tokens = []
if random:
indices = np.random.randint(0, len(inputs), nb_examples)
else:
indices = range(nb_examples)
for index in indices:
input_tokens.append(inputs[index])
target_tokens.append(targets[index])
input_sequences = batch(input_tokens, maxlen, input_ctable,
nb_examples, reverse)
input_sequences = next(input_sequences)
# Procedure for inference mode (sampling):
# 1) Encode input and retrieve initial decoder state.
# 2) Run one step of decoder with this initial state
# and a start-of-sequence character as target.
# Output will be the next target character.
# 3) Repeat with the current target character and current states.
# Encode the input as state vectors.
states_value = encoder_model.predict(input_sequences)
# Create batch of empty target sequences of length 1 character.
target_sequences = np.zeros((nb_examples, 1, target_ctable.size))
# Populate the first element of target sequence
# with the start-of-sequence character.
target_sequences[:, 0, target_ctable.char2index[SOS]] = 1.0
# Sampling loop for a batch of sequences.
# Exit condition: either hit max character limit
# or encounter end-of-sequence character.
decoded_tokens = [''] * nb_examples
for _ in range(maxlen):
# `char_probs` has shape
# (nb_examples, 1, nb_target_chars)
char_probs, h, c = decoder_model.predict(
[target_sequences] + states_value)
# Reset the target sequences.
target_sequences = np.zeros((nb_examples, 1, target_ctable.size))
# Sample next character using argmax or multinomial mode.
sampled_chars = []
for i in range(nb_examples):
if sample_mode == 'argmax':
next_index, next_char = target_ctable.decode(
char_probs[i], calc_argmax=True)
elif sample_mode == 'multinomial':
next_index, next_char = target_ctable.sample_multinomial(
char_probs[i], temperature=0.5)
else:
raise Exception(
"`sample_mode` accepts `argmax` or `multinomial`.")
decoded_tokens[i] += next_char
sampled_chars.append(next_char)
# Update target sequence with index of next character.
target_sequences[i, 0, next_index] = 1.0
stop_char = set(sampled_chars)
if len(stop_char) == 1 and stop_char.pop() == EOS:
break
# Update states.
states_value = [h, c]
# Sampling finished.
input_tokens = [re.sub('[%s]' % EOS, '', token)
for token in input_tokens]
target_tokens = [re.sub('[%s]' % EOS, '', token)
for token in target_tokens]
decoded_tokens = [re.sub('[%s]' % EOS, '', token)
for token in decoded_tokens]
return input_tokens, target_tokens, decoded_tokens
def restore_model(path_to_full_model, hidden_size):
"""Restore model to construct the encoder and decoder."""
model = load_model(path_to_full_model, custom_objects={
'truncated_acc': truncated_acc, 'truncated_loss': truncated_loss})
encoder_inputs = model.input[0] # encoder_data
encoder_lstm1 = model.get_layer('encoder_lstm_1')
encoder_lstm2 = model.get_layer('encoder_lstm_2')
encoder_outputs = encoder_lstm1(encoder_inputs)
_, state_h, state_c = encoder_lstm2(encoder_outputs)
encoder_states = [state_h, state_c]
encoder_model = Model(inputs=encoder_inputs, outputs=encoder_states)
decoder_inputs = model.input[1] # decoder_data
decoder_state_input_h = Input(shape=(hidden_size,))
decoder_state_input_c = Input(shape=(hidden_size,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_lstm = model.get_layer('decoder_lstm')
decoder_outputs, state_h, state_c = decoder_lstm(
decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_softmax = model.get_layer('decoder_softmax')
decoder_outputs = decoder_softmax(decoder_outputs)
decoder_model = Model(inputs=[decoder_inputs] + decoder_states_inputs,
outputs=[decoder_outputs] + decoder_states)
return encoder_model, decoder_model