-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathdata_loader.py
312 lines (224 loc) · 10.2 KB
/
data_loader.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
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import operator
import os
import random
import re
from nltk.tokenize import TweetTokenizer
import numpy as np
from hbconfig import Config
import tensorflow as tf
from tqdm import tqdm
tokenizer = TweetTokenizer()
class IteratorInitializerHook(tf.train.SessionRunHook):
"""Hook to initialise data iterator after Session is created."""
def __init__(self):
super(IteratorInitializerHook, self).__init__()
self.iterator_initializer_func = None
def after_create_session(self, session, coord):
"""Initialise the iterator after the session has been created."""
self.iterator_initializer_func(session)
def get_dataset_batch(data, buffer_size=10000, batch_size=64, scope="train"):
iterator_initializer_hook = IteratorInitializerHook()
def inputs():
with tf.name_scope(scope):
nonlocal data
enc_inputs, targets = data
# Define placeholders
enc_placeholder = tf.placeholder(
tf.int32, [None, None], name="enc_placeholder")
target_placeholder = tf.placeholder(
tf.int32, [None, None], name="target_placeholder")
# Build dataset iterator
dataset = tf.data.Dataset.from_tensor_slices(
(enc_placeholder, target_placeholder))
if scope == "train":
dataset = dataset.repeat(None) # Infinite iterations
else:
dataset = dataset.repeat(1) # one Epoch
dataset = dataset.shuffle(buffer_size=buffer_size)
dataset = dataset.batch(batch_size)
iterator = dataset.make_initializable_iterator()
next_enc, next_target = iterator.get_next()
tf.identity(next_enc[0], 'enc_0')
tf.identity(next_target[0], 'target_0')
# Set runhook to initialize iterator
iterator_initializer_hook.iterator_initializer_func = \
lambda sess: sess.run(
iterator.initializer,
feed_dict={enc_placeholder: enc_inputs,
target_placeholder: targets})
# Return batched (features, labels)
return {"enc_inputs": next_enc}, next_target
# Return function and hook
return inputs, iterator_initializer_hook
def prepare_dataset(questions, answers):
# random convos to create the test set
test_ids = random.sample([i for i in range(len(questions))], Config.data.testset_size)
filenames = ['train.enc', 'train.dec', 'test.enc', 'test.dec']
files = []
for filename in filenames:
files.append(open(os.path.join(Config.data.base_path, Config.data.processed_path, filename), 'wb'))
for i in tqdm(range(len(questions))):
question = questions[i]
answer = answers[i]
if i in test_ids:
files[2].write((question + "\n").encode('utf-8'))
files[3].write((answer + '\n').encode('utf-8'))
else:
files[0].write((question + '\n').encode('utf-8'))
files[1].write((answer + '\n').encode('utf-8'))
for file in files:
file.close()
def make_dir(path):
""" Create a directory if there isn't one already. """
try:
os.mkdir(path)
except OSError:
pass
def basic_tokenizer(line, normalize_digits=True):
""" A basic tokenizer to tokenize text into tokens.
Feel free to change this to suit your need. """
line = re.sub('<u>', '', line)
line = re.sub('</u>', '', line)
line = re.sub('\[', '', line)
line = re.sub('\]', '', line)
words = []
_WORD_SPLIT = re.compile("([.,!?\"'-<>:;)(])")
_DIGIT_RE = re.compile(r"\d")
for fragment in line.strip().lower().split():
for token in re.split(_WORD_SPLIT, fragment):
if not token:
continue
if normalize_digits:
token = re.sub(_DIGIT_RE, '#', token)
words.append(token)
return words
def build_vocab(in_fname, out_fname, normalize_digits=True):
print("Count each vocab frequency ...")
def count_vocab(fname):
vocab = {}
with open(fname, 'rb') as f:
for line in tqdm(f.readlines()):
line = line.decode('utf-8')
for token in tokenizer.tokenize(line):
if not token in vocab:
vocab[token] = 0
vocab[token] += 1
return vocab
in_path = os.path.join(Config.data.base_path, Config.data.raw_data_path, in_fname)
out_path = os.path.join(Config.data.base_path, Config.data.raw_data_path, out_fname)
source_vocab = count_vocab(in_path)
target_vocab = count_vocab(out_path)
print("total vocab size:", len(source_vocab), len(target_vocab))
def write_vocab(fname, sorted_vocab):
dest_path = os.path.join(Config.data.base_path, Config.data.processed_path, fname)
with open(dest_path, 'wb') as f:
f.write(('<pad>' + '\n').encode('utf-8'))
f.write(('<unk>' + '\n').encode('utf-8'))
f.write(('<s>' + '\n').encode('utf-8'))
f.write(('<\s>' + '\n').encode('utf-8'))
index = 4
for word, count in tqdm(sorted_vocab):
if count < Config.data.word_threshold:
break
f.write((word + '\n').encode('utf-8'))
index += 1
sorted_source_vocab = sorted(source_vocab.items(), key=operator.itemgetter(1), reverse=True)
sorted_target_vocab = sorted(target_vocab.items(), key=operator.itemgetter(1), reverse=True)
write_vocab("source_vocab", sorted_source_vocab)
write_vocab("target_vocab", sorted_target_vocab)
def load_vocab(vocab_fname):
print("load vocab ...")
with open(os.path.join(Config.data.base_path, Config.data.processed_path, vocab_fname), 'rb') as f:
words = f.read().decode('utf-8').splitlines()
print("vocab size:", len(words))
return {words[i]: i for i in range(len(words))}
def sentence2id(vocab, line):
return [vocab.get(token, vocab['<unk>']) for token in tokenizer.tokenize(line)]
def token2id(data, mode):
""" Convert all the tokens in the data into their corresponding
index in the vocabulary. """
vocab_path = 'vocab'
if mode == "enc":
vocab_path = 'source_' + vocab_path
elif mode == "dec":
vocab_path = 'target_' + vocab_path
in_path = data + '.' + mode
out_path = data + '_ids.' + mode
vocab = load_vocab(vocab_path)
in_file = open(os.path.join(Config.data.base_path, Config.data.raw_data_path, in_path), 'rb')
out_file = open(os.path.join(Config.data.base_path, Config.data.processed_path, out_path), 'wb')
lines = in_file.read().decode('utf-8').splitlines()
for line in tqdm(lines):
ids = []
sentence_ids = sentence2id(vocab, line)
ids.extend(sentence_ids)
if mode == 'dec':
ids.append(vocab['<\s>'])
ids.append(vocab['<pad>'])
out_file.write(b' '.join(str(id_).encode('utf-8') for id_ in ids) + b'\n')
def process_data():
print('Preparing data to be model-ready ...')
# create path to store all the train & test encoder & decoder
make_dir(Config.data.base_path + Config.data.processed_path)
build_vocab('train.enc', 'train.dec')
token2id('train', 'enc')
token2id('train', 'dec')
token2id('test', 'enc')
token2id('test', 'dec')
def make_train_and_test_set(shuffle=True):
print("make Training data and Test data Start....")
if Config.data.get('max_seq_length', None) is None:
set_max_seq_length(['train_ids.enc', 'train_ids.dec', 'test_ids.enc', 'test_ids.dec'])
train_enc, train_dec = load_data('train_ids.enc', 'train_ids.dec')
test_enc, test_dec = load_data('test_ids.enc', 'test_ids.dec', train=False)
assert len(train_enc) == len(train_dec)
assert len(test_enc) == len(test_dec)
print(f"train data count : {len(train_dec)}")
print(f"test data count : {len(test_dec)}")
if shuffle:
print("shuffle dataset ...")
train_p = np.random.permutation(len(train_dec))
test_p = np.random.permutation(len(test_dec))
return ((train_enc[train_p], train_dec[train_p]),
(test_enc[test_p], test_dec[test_p]))
else:
return ((train_enc, train_dec),
(test_enc, test_dec))
def load_data(enc_fname, dec_fname, train=True):
enc_input_data = open(os.path.join(Config.data.base_path, Config.data.processed_path, enc_fname), 'r')
dec_input_data = open(os.path.join(Config.data.base_path, Config.data.processed_path, dec_fname), 'r')
enc_data, dec_data = [], []
for e_line, d_line in tqdm(zip(enc_input_data.readlines(), dec_input_data.readlines())):
e_ids = [int(id_) for id_ in e_line.split()]
d_ids = [int(id_) for id_ in d_line.split()]
if len(e_ids) == 0 or len(d_ids) == 0:
continue
if len(e_ids) <= Config.data.max_seq_length and len(d_ids) < Config.data.max_seq_length:
enc_data.append(_pad_input(e_ids, Config.data.max_seq_length))
dec_data.append(_pad_input(d_ids, Config.data.max_seq_length))
print(f"load data from {enc_fname}, {dec_fname}...")
return np.array(enc_data, dtype=np.int32), np.array(dec_data, dtype=np.int32)
def _pad_input(input_, size):
return input_ + [Config.data.PAD_ID] * (size - len(input_))
def set_max_seq_length(dataset_fnames):
max_seq_length = Config.data.get('max_seq_length', 10)
for fname in dataset_fnames:
input_data = open(os.path.join(Config.data.base_path, Config.data.processed_path, fname), 'r')
for line in input_data.readlines():
ids = [int(id_) for id_ in line.split()]
seq_length = len(ids)
if seq_length > max_seq_length:
max_seq_length = seq_length
Config.data.max_seq_length = max_seq_length
print(f"Setting max_seq_length to Config : {max_seq_length}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--config', type=str, default='config',
help='config file name')
args = parser.parse_args()
Config(args.config)
process_data()