forked from LantaoYu/SeqGAN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataloader.py
320 lines (241 loc) · 11.2 KB
/
dataloader.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
import numpy as np
import os
class Gen_Data_loader(object):
def __init__(self, batch_size):
self.batch_size = batch_size
self.token_stream = []
def create_batches(self, data_file):
self.token_stream = []
with open(data_file, 'r') as f:
for line in f:
line = line.strip()
line = line.split()
parse_line = [int(x) for x in line]
if len(parse_line) == 20:
self.token_stream.append(parse_line)
self.num_batch = int(len(self.token_stream) / self.batch_size)
self.token_stream = self.token_stream[:self.num_batch * self.batch_size]
self.sequence_batch = np.split(np.array(self.token_stream), self.num_batch, 0)
self.pointer = 0
def next_batch(self):
ret = self.sequence_batch[self.pointer]
self.pointer = (self.pointer + 1) % self.num_batch
return ret
def reset_pointer(self):
self.pointer = 0
class Gen_Data_loader_text(Gen_Data_loader):
def __init__(self, batch_size, map, inv_map,seq_len=20, token_type='char'):
super(Gen_Data_loader_text, self).__init__(batch_size)
self.map, self.inv_map = map, inv_map
self.seq_len = seq_len
self.token_type = token_type
def create_batches(self, data_file, limit_num_samples=None):
if self.token_type == 'char':
seperator = ''
elif self.token_type == 'word':
seperator = ' '
else:
raise TypeError
cache_file = "%s_seqlen%0d.npy"%(data_file,self.seq_len)
if os.path.exists(cache_file):
self.token_stream = np.load(cache_file)
else:
self.token_stream = []
# with open(data_file, 'r') as f:
# line = f.read(self.seq_len)
# while len(line) == self.seq_len:
# tokens = [int(self.map[char]) for char in line]
# assert len(tokens) == self.seq_len
# self.token_stream.append(tokens)
#
# line = f.read(self.seq_len)
with open(data_file, 'r') as f:
# tokenize positive
if self.token_type == 'char':
line = f.read(self.seq_len)
while len(line) == self.seq_len:
tokens = [int(self.map[char]) for char in line]
assert len(tokens) == self.seq_len
self.token_stream.append(tokens)
line = f.read(self.seq_len)
elif self.token_type == 'word':
text = f.read().replace('\n','<eos>')
text = text.split(seperator)
tokens = [int(self.map[word]) for word in text]
while len(tokens) > self.seq_len:
self.token_stream.append(tokens[:self.seq_len])
tokens = tokens[self.seq_len:]
else:
raise TypeError
self.token_stream = np.array(self.token_stream)
np.save(cache_file.replace('.npy',''),self.token_stream)
assert self.token_stream.shape[1] == self.seq_len
if limit_num_samples is not None:
# choose only limit_num_samples from them
self.token_stream = np.array(self.token_stream)
permut = np.random.permutation(self.token_stream.shape[0])[:limit_num_samples]
self.token_stream = self.token_stream[permut]
else:
self.token_stream = np.array(self.token_stream)
self.num_batch = int(len(self.token_stream) / self.batch_size)
self.token_stream = self.token_stream[:self.num_batch * self.batch_size]
self.sequence_batch = np.split(np.array(self.token_stream), self.num_batch, 0)
self.pointer = 0
print("done create_batches - [num_batch=%0d]"%self.num_batch)
def reset_pointer(self):
self.shuffle()
self.pointer = 0
def shuffle(self):
print("GEN shuffling data...")
permut = np.random.permutation(self.token_stream.shape[0])
self.token_stream = self.token_stream[permut]
class Dis_dataloader(object):
def __init__(self, batch_size):
self.batch_size = batch_size
self.sentences = np.array([])
self.labels = np.array([])
def load_train_data(self, positive_file, negative_file):
# Load data
positive_examples = []
negative_examples = []
with open(positive_file)as fin:
for line in fin:
line = line.strip()
line = line.split()
parse_line = [int(x) for x in line]
positive_examples.append(parse_line)
with open(negative_file)as fin:
for line in fin:
line = line.strip()
line = line.split()
parse_line = [int(x) for x in line]
if len(parse_line) == 20:
negative_examples.append(parse_line)
self.sentences = np.array(positive_examples + negative_examples)
# Generate labels
positive_labels = [[0, 1] for _ in positive_examples]
negative_labels = [[1, 0] for _ in negative_examples]
self.labels = np.concatenate([positive_labels, negative_labels], 0)
# Shuffle the data
shuffle_indices = np.random.permutation(np.arange(len(self.labels)))
self.sentences = self.sentences[shuffle_indices]
self.labels = self.labels[shuffle_indices]
# Split batches
self.num_batch = int(len(self.labels) / self.batch_size)
self.sentences = self.sentences[:self.num_batch * self.batch_size]
self.labels = self.labels[:self.num_batch * self.batch_size]
self.sentences_batches = np.split(self.sentences, self.num_batch, 0)
self.labels_batches = np.split(self.labels, self.num_batch, 0)
self.pointer = 0
def next_batch(self):
ret = self.sentences_batches[self.pointer], self.labels_batches[self.pointer]
self.pointer = (self.pointer + 1) % self.num_batch
return ret
def reset_pointer(self):
self.pointer = 0
class Dis_dataloader_text(Dis_dataloader):
def __init__(self, batch_size, map, inv_map, seq_len=20, token_type='char'):
super(Dis_dataloader_text, self).__init__(batch_size)
self.map, self.inv_map = map, inv_map
self.seq_len = seq_len
self.token_type = token_type
def load_train_data(self, positive_file, negative_file):
# #LOAD NEGATIVE
# positive file is constant while negative file is changed during train!
# if os.path.exists(negative_file + '.npy'):
# negative_examples = np.load(negative_file + '.npy')
# else:
negative_examples = []
# remove \n
if self.token_type == 'char':
seperator = ''
elif self.token_type == 'word':
seperator = ' '
else:
raise TypeError
with open(negative_file, 'r') as f:
all_negative = f.read()
with open(negative_file, 'w') as f:
f.write(all_negative.replace('\n', seperator))
# tokenize examples
if self.token_type == 'char':
with open(negative_file, 'r') as f:
line = f.read(self.seq_len)
while len(line) == self.seq_len:
tokens = [int(self.map[char]) for char in line]
assert len(tokens) == self.seq_len
negative_examples.append(tokens)
line = f.read(self.seq_len)
elif self.token_type == 'word':
with open(negative_file, 'r') as f:
text = f.read().replace('\n','<eos>')
text = text.split(seperator)
tokens = [int(self.map[word]) for word in text]
while len(tokens) > self.seq_len:
negative_examples.append(tokens[:self.seq_len])
tokens = tokens[self.seq_len:]
else:
raise TypeError
# np.save(negative_file,np.array(negative_examples))
negative_examples = np.array(negative_examples)
num_positive_samples = negative_examples.shape[0]
#LOAD POSITIVE
cache_positive = "%s_seqlen%0d.npy"%(positive_file,self.seq_len)
if os.path.exists(cache_positive):
positive_examples = np.load(cache_positive)
else:
positive_examples = []
with open(positive_file, 'r') as f:
# tokenize positive
if self.token_type == 'char':
line = f.read(self.seq_len)
while len(line) == self.seq_len:
tokens = [int(self.map[char]) for char in line]
assert len(tokens) == self.seq_len
positive_examples.append(tokens)
line = f.read(self.seq_len)
elif self.token_type == 'word':
text = f.read().replace('\n','<eos>')
text = text.split(seperator)
tokens = [int(self.map[word]) for word in text]
while len(tokens) > self.seq_len:
positive_examples.append(tokens[:self.seq_len])
tokens = tokens[self.seq_len:]
else:
raise TypeError
positive_examples = np.array(positive_examples)
np.save(cache_positive.replace('.npy',''),positive_examples)
assert positive_examples.shape[1] == self.seq_len
#choose only num_positive_samples from them
permut = np.random.permutation(positive_examples.shape[0])[:num_positive_samples]
positive_examples = positive_examples[permut]
# CONCAT
negative_examples = np.array(negative_examples)
positive_examples = np.array(positive_examples)
assert negative_examples.shape == positive_examples.shape
self.sentences = np.concatenate((positive_examples,negative_examples),axis=0)
# Generate labels
positive_labels = [[0, 1]] * positive_examples.shape[0]
negative_labels = [[1, 0]] * negative_examples.shape[0]
self.labels = np.concatenate([positive_labels, negative_labels], 0)
# # Shuffle the data
# print "DISC shuffling data..."
# shuffle_indices = np.random.permutation(self.sentences.shape[0])
# self.sentences = self.sentences[shuffle_indices]
# self.labels = self.labels[shuffle_indices]
# Split batches
self.num_batch = int(len(self.labels) / self.batch_size)
self.sentences = self.sentences[:self.num_batch * self.batch_size]
self.labels = self.labels[:self.num_batch * self.batch_size]
self.sentences_batches = np.split(self.sentences, self.num_batch, 0)
self.labels_batches = np.split(self.labels, self.num_batch, 0)
self.pointer = 0
print("done create_batches - [num_batch=%0d]"%self.num_batch)
def reset_pointer(self):
self.shuffle()
self.pointer = 0
def shuffle(self):
print("DISC shuffling data...")
shuffle_indices = np.random.permutation(self.sentences.shape[0])
self.sentences = self.sentences[shuffle_indices]
self.labels = self.labels[shuffle_indices]