-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.py
364 lines (289 loc) · 10.6 KB
/
data.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import io
import h5py
import torch
from torch import nn
from torch import cuda
import numpy as np
import ujson
from util import *
class Data():
def __init__(self, opt, data_file, res_files=None, triple_mode=False):
self.opt = opt
self.data_name = data_file
print('loading data from {0}'.format(data_file))
f = h5py.File(data_file, 'r')
self.source = f['source'][:] # indices to glove tokens
self.target = f['target'][:] # indices to glove tokens
self.source_l = f['source_l'][:].astype(np.int32)
self.target_l = f['target_l'][:].astype(np.int32)
self.all_source = f['all_source'][:]
self.all_target = f['all_target'][:]
self.label = f['label'][:]
self.batch_l = f['batch_l'][:].astype(np.int32)
self.batch_idx = f['batch_idx'][:].astype(np.int32)
self.bert_tok_idx1 = f['bert_tok_idx1'][:].astype(np.int32)
self.bert_tok_idx2 = f['bert_tok_idx2'][:].astype(np.int32)
self.ex_idx = f['ex_idx'][:].astype(np.int32)
self.length = self.batch_l.shape[0]
self.source = torch.from_numpy(self.source)
self.target = torch.from_numpy(self.target)
self.all_source = torch.from_numpy(self.all_source)
self.all_target = torch.from_numpy(self.all_target)
self.bert_tok_idx1 = torch.from_numpy(self.bert_tok_idx1)
self.bert_tok_idx2 = torch.from_numpy(self.bert_tok_idx2)
self.label = torch.from_numpy(self.label)
# if triple presents
self.third = None
self.all_third = None
self.third_l = None
self.bert_tok_idx3 = None
if 'third' in f and triple_mode is True:
print('third sentence detected.')
self.third = f['third'][:]
self.all_third = f['all_third'][:]
self.third_l = f['third_l'][:].astype(np.int32)
self.bert_tok_idx3 = f['bert_tok_idx3'][:].astype(np.int32)
self.third = torch.from_numpy(self.third)
self.all_third = torch.from_numpy(self.all_third)
self.bert_tok_idx3 = torch.from_numpy(self.bert_tok_idx3)
# postpone the transfer to gpu to the batch running stage
#if self.opt.gpuid != -1:
# self.source = self.source.cuda()
# self.target = self.target.cuda()
# self.span = self.span.cuda()
# load char_idx file
if opt.use_char_emb == 1:
print('loading char idx from {0}'.format(opt.char_idx))
f = h5py.File(opt.char_idx, 'r')
self.char_idx = f['char_idx'][:]
self.char_idx = torch.from_numpy(self.char_idx)
assert(self.char_idx.shape[1] == opt.token_l)
assert(self.char_idx.max()+1 == opt.num_char)
print('{0} chars found'.format(self.char_idx.max()+1))
self.batches = []
for i in range(self.length):
start = self.batch_idx[i]
end = start + self.batch_l[i]
# get example token indices
all_source_i = self.all_source[start:end, 0:self.source_l[i]]
all_target_i = self.all_target[start:end, 0:self.target_l[i]]
source_i = self.source[start:end, 0:self.source_l[i]]
target_i = self.target[start:end, 0:self.target_l[i]]
label_i = self.label[start:end]
bert_tok1_i = self.bert_tok_idx1[start:end, 0:self.source_l[i]]
bert_tok2_i = self.bert_tok_idx2[start:end, 0:self.target_l[i]]
# sanity check
assert(self.source[start:end, self.source_l[i]:].sum() == 0)
assert(self.target[start:end, self.target_l[i]:].sum() == 0)
if self.third is None:
# src, tgt, all_src, all_tgt, batch_l, src_l, tgt_l, label, raw info
self.batches.append((source_i, target_i, all_source_i, all_target_i, bert_tok1_i, bert_tok2_i,
int(self.batch_l[i]), int(self.source_l[i]), int(self.target_l[i]), label_i))
else:
third_i = self.third[start:end, 0:self.third_l[i]]
all_third_i = self.all_third[start:end, 0:self.third_l[i]]
bert_tok3_i = self.bert_tok_idx3[start:end, 0:self.third_l[i]]
assert(self.third[start:end, self.third_l[i]:].sum() == 0)
# src, tgt, third, all_src, all_tgt, all_third, batch_l, src_l, tgt_l, third_l, label, raw info
self.batches.append((source_i, target_i, third_i, all_source_i, all_target_i, all_third_i, bert_tok1_i, bert_tok2_i, bert_tok3_i,
int(self.batch_l[i]), int(self.source_l[i]), int(self.target_l[i]), int(self.third_l[i]), label_i))
# count examples
self.num_ex = 0
for i in range(self.length):
self.num_ex += self.batch_l[i]
# load resource files
self.res_names = []
if res_files is not None:
for f in res_files:
if f.endswith('txt'):
res_names = self.__load_txt(f)
elif f.endswith('json'):
res_names = self.__load_json_res(f)
else:
assert(False)
self.res_names.extend(res_names)
def subsample(self, ratio, minimal_num=0):
target_num_ex = int(float(self.num_ex) * ratio)
target_num_ex = max(target_num_ex, minimal_num)
sub_idx = torch.LongTensor(range(self.size()))
sub_num_ex = 0
if ratio != 1.0:
rand_idx = torch.randperm(self.size())
i = 0
while sub_num_ex < target_num_ex and i < self.batch_l.shape[0]:
sub_num_ex += self.batch_l[rand_idx[i]]
i += 1
sub_idx = rand_idx[:i]
else:
sub_num_ex = self.batch_l.sum()
return sub_idx, sub_num_ex
def split(self, sub_idx, ratio):
num_ex = sum([self.batch_l[i] for i in sub_idx])
target_num_ex = int(float(num_ex) * ratio)
cur_num_ex = 0
cur_pos = 0
for i in range(len(sub_idx)):
cur_pos = i
cur_num_ex += self.batch_l[sub_idx[i]]
if cur_num_ex >= target_num_ex:
break
return sub_idx[:cur_pos+1], sub_idx[cur_pos+1:], cur_num_ex, num_ex - cur_num_ex
def __load_txt(self, path):
lines = []
print('loading resource from {0}'.format(path))
# read file in unicode mode!!!
with io.open(path, 'r+', encoding="utf-8") as f:
for l in f:
lines.append(l.rstrip())
# the second last extension is the res name
res_name = path.split('.')[-2]
res_data = lines[:]
# some customized parsing
parsed = []
parsed = res_data
setattr(self, res_name, parsed)
return [res_name]
def __load_json_res(self, path):
print('loading resource from {0}'.format(path))
f_str = None
with open(path, 'r') as f:
f_str = f.read()
j_obj = ujson.loads(f_str)
# get key name of the file
assert(len(j_obj) == 2)
res_type = next(iter(j_obj))
res_name = None
if j_obj[res_type] == 'map':
res_name = self.__load_json_map(path)
elif j_obj[res_type] == 'list':
res_name = self.__load_json_list(path)
else:
assert(False)
return [res_name]
def __load_json_map(self, path):
f_str = None
with open(path, 'r') as f:
f_str = f.read()
j_obj = ujson.loads(f_str)
assert(len(j_obj) == 2)
res_name = None
for k, v in j_obj.items():
if k != 'type':
res_name = k
# optimize indices
res = {}
for k, v in j_obj[res_name].items():
lut = {}
for i, j in v.items():
if i == res_name:
lut[res_name] = [int(l) for l in j]
else:
lut[int(i)] = ([l for l in j[0]], [l for l in j[1]])
res[int(k)] = lut
setattr(self, res_name, res)
return res_name
def __load_json_list(self, path):
f_str = None
with open(path, 'r') as f:
f_str = f.read()
j_obj = ujson.loads(f_str)
assert(len(j_obj) == 2)
res_name = None
for k, v in j_obj.items():
if k != 'type':
res_name = k
# optimize indices
res = {}
for k, v in j_obj[res_name].items():
p = v['p']
h = v['h']
# for token indices, shift by 1 to incorporate the nul-token at the beginning
res[int(k)] = ([l for l in p], [l for l in h])
setattr(self, res_name, res)
return res_name
def size(self):
return self.length
def __getitem__(self, idx):
if self.third is None:
(source, target, all_source, all_target, bert1, bert2,
batch_l, source_l, target_l, label) = self.batches[idx]
else:
(source, target, third, all_source, all_target, all_third, bert1, bert2, bert3,
batch_l, source_l, target_l, third_l, label) = self.batches[idx]
token_l = self.opt.token_l
# get char indices
# the back forth data transfer should be eliminated
char1, char2 = None, None
if self.opt.use_char_emb == 1:
char1 = self.char_idx[all_source.contiguous().view(-1)].view(batch_l, source_l, token_l)
char2 = self.char_idx[all_target.contiguous().view(-1)].view(batch_l, target_l, token_l)
# transfer to gpu if needed
if self.opt.gpuid != -1:
if self.opt.use_char_emb == 1:
char1 = char1.cuda(self.opt.gpuid)
char2 = char2.cuda(self.opt.gpuid)
source = source.cuda(self.opt.gpuid)
target = target.cuda(self.opt.gpuid)
label = label.cuda(self.opt.gpuid)
bert1 = bert1.long().cuda(self.opt.gpuid)
bert2 = bert2.long().cuda(self.opt.gpuid)
if self.third is not None:
char3 = None
if self.opt.use_char_emb == 1:
char3 = char3.cuda(self.opt.gpuid)
char3 = self.char_idx[all_third.contiguous().view(-1)].view(batch_l, third_l, token_l)
if self.opt.gpuid != -1:
if self.opt.use_char_emb == 1:
char3 = char3.cuda(self.opt.gpuid)
third = third.cuda(self.opt.gpuid)
bert3 = bert3.long().cuda(self.opt.gpuid)
# get batch ex indices
batch_ex_idx = [self.ex_idx[i] for i in range(self.batch_idx[idx], self.batch_idx[idx] + self.batch_l[idx])]
res_map = self.__get_res(idx)
if self.third is None:
return (self.data_name, source, target, char1, char2, bert1, bert2,
batch_ex_idx, batch_l, source_l, target_l, label, res_map)
else:
return (self.data_name, source, target, third, char1, char2, char3, bert1, bert2, bert3,
batch_ex_idx, batch_l, source_l, target_l, third_l, label, res_map)
def __get_res_elmo(self, res_name, idx, batch_ex_idx):
if res_name == 'elmo_concated':
embs = torch.from_numpy(self.elmo_file['{0}.concated_batch'.format(idx)][:])
if self.opt.gpuid != -1:
embs = embs.cuda(self.opt.gpuid)
return embs
else:
raise Exception('unrecognized res {0}'.format(res_name))
def __get_res_bert(self, res_name, idx, batch_ex_idx):
if res_name == 'bert_concated':
embs = torch.from_numpy(self.bert_file['{0}.concated_batch'.format(idx)][:])
if self.opt.gpuid != -1:
embs = embs.cuda(self.opt.gpuid)
return embs
else:
raise Exception('unrecognized res {0}'.format(res_name))
def __get_res(self, idx):
# if there is no resource presents, return None
if len(self.res_names) == 0:
return None
batch_ex_idx = [self.ex_idx[i] for i in range(self.batch_idx[idx], self.batch_idx[idx] + self.batch_l[idx])]
all_res = {}
for res_n in self.res_names:
# some customization for elmo is needed here for lazy loading
if 'elmo' in res_n:
batch_res = self.__get_res_elmo(res_n, idx, batch_ex_idx)
all_res[res_n] = batch_res
elif 'bert' in res_n:
batch_res = self.__get_res_bert(res_n, idx, batch_ex_idx)
all_res[res_n] = batch_res
else:
res = getattr(self, res_n)
batch_res = [res[ex_id] for ex_id in batch_ex_idx]
all_res[res_n] = batch_res
return all_res
# something at the beginning of each pass of training/eval
# e.g. setup preloading
def begin_pass(self):
pass
def end_pass(self):
pass