-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_gen.py
228 lines (171 loc) · 5.67 KB
/
data_gen.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
# -*- coding: utf-8 -*-
import time
import re
from random import shuffle
import string
import numpy as np
printable = set(string.printable)
class Corpus(object):
"""
A data generator that takes care of all the data loading and preparation
required for training.
"""
def __init__(self,
train_file,
val_file,
data_sample,
batch_size=64,
max_len=25,
skip=2):
"""
Args:
train_file: The training data.
val_file: The validation data.
data_sample: If < 1. then use a sample of the data.
batch_size: The size of the mini-batches for training.
max_len: The length of the input sequences.
skip: How many characters to skip when creating the
training sequences.
"""
self.train_file = train_file
self.val_file = val_file
self.data_sample = data_sample
self.batch_size= batch_size
self.max_len = max_len
self.skip = skip
self.read_data()
def read_data(self):
"""
Reads the raw data and splits them to wiki pages using </page>\n
as the delimiter.
Also filters out all then non-printable characters.
"""
with open(self.train_file) as f:
self.train_data = f.read()
self.train_data = filter(lambda x: x in printable, self.train_data)
self.train_data = re.split('(?<=</page>)\n+',self.train_data)
shuffle(self.train_data)
self.train_data = self.train_data[:int(len(self.train_data) * self.data_sample)]
with open(self.val_file) as f:
self.val_data = f.read()
self.val_data = filter(lambda x: x in printable, self.val_data)
self.val_data = re.split('(?<=</page>)\n+',self.val_data)
shuffle(self.val_data)
self.val_data = self.val_data[:int(len(self.val_data) * self.data_sample)]
self.build_vocabulary(self.train_data)
self.find_size()
return
def find_size(self):
"""
Estimate the number of training and validation batches for training.
"""
num_train_batches = 0
for item in self.train_data:
batches = [item[i:i+self.batch_size] for i in range(0, len(item), self.batch_size)]
num_train_batches += len(batches)
self.train_size = num_train_batches
num_val_batches = 0
for item in self.val_data:
batches = [item[i:i+self.batch_size] for i in range(0, len(item), self.batch_size)]
num_val_batches += len(batches)
self.val_size = num_val_batches
return
def build_vocabulary(self,
data):
"""
Builds the corpus vocabulary of characters.
Also creates the char2id and id2char dictionaries.
"""
char_vocab = []
for item in data:
chars = list(set(item))
for char in chars:
if char not in char_vocab:
char_vocab.append(char)
char_vocab = sorted(char_vocab)
self.vocab_size = len(char_vocab)
self.char2id = dict((c,i) for i,c in enumerate(char_vocab))
self.id2char = dict((i,c) for i,c in enumerate(char_vocab))
print('Found %s unique tokens' % self.vocab_size)
return
def get_train(self):
"""
Get the next training batch. Splits a wiki page into batches
of batch_size and the batches into one-hot encoded
sequence,next_character pairs of max_len length.
Once a full pass over the whole training set is comleted, the
wiki pages are shuffled before the next pass.
Yields:
train_batch: A batch of encoded sequences.
train_targets: The encoded next character for each sequence.
"""
while True:
for item in self.train_data:
batches = [item[i:i+self.batch_size] for i in range(0, len(item), self.batch_size)]
for batch in batches:
sections = []
next_chars = []
for i in range(0,len(batch)-self.max_len,self.skip):
sections.append(batch[i: i + self.max_len])
next_chars.append(batch[i + self.max_len])
if len(sections) == 0:
continue
train_batch = np.zeros((len(sections),self.max_len,self.vocab_size))
train_targets = np.zeros((len(sections),self.vocab_size))
for i,section in enumerate(sections):
for j,char in enumerate(section):
train_batch[i,j,self.char2id[char]] = 1
train_targets[i,self.char2id[next_chars[i]]] = 1
yield train_batch, train_targets
self.shuffle_examples('train')
def get_val(self):
"""
Get the next validation batch. Splits a wiki page into batches
of batch_size and the batches into one-hot encoded
sequence,next_character pairs of max_len length.
Once a full pass over the whole validation set is comleted, the
wiki pages are shuffled before the next pass.
Yields:
train_batch: A batch of encoded sequences.
train_targets: The encoded next character for each sequence.
"""
while True:
for item in self.val_data:
batches = [item[i:i+self.batch_size] for i in range(0, len(item), self.batch_size)]
for batch in batches:
sections = []
next_chars = []
for i in range(0,len(batch)-self.max_len,self.skip):
sections.append(batch[i: i + self.max_len])
next_chars.append(batch[i + self.max_len])
if len(sections) == 0:
continue
val_batch = np.zeros((len(sections),self.max_len,self.vocab_size))
val_targets = np.zeros((len(sections),self.vocab_size))
for i,section in enumerate(sections):
for j,char in enumerate(section):
try:
val_batch[i,j,self.char2id[char]] = 1
except:
pass
try:
val_targets[i,self.char2id[next_chars[i]]] = 1
except:
pass
yield val_batch, val_targets
self.shuffle_examples('val')
def shuffle_examples(self,
flag):
"""
Shuffles the wiki pages in the training or validation set.
This is normaly done at the end of each epoch.
Args:
flag: train or val that specifies the data set to shuffle.
"""
if flag == 'train':
print 'Shuffling training set'
shuffle(self.train_data)
else:
print 'Shuffling validation set'
shuffle(self.val_data)
return