-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_utils.py
285 lines (238 loc) · 8.59 KB
/
data_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
import numpy as np
import torch
import constants
import pickle
import random
from collections import namedtuple
def argsort(seq):
"""
sort by length in reverse order
---
seq (list[array[int32]])
"""
return [x for x, y in sorted(enumerate(seq),
key=lambda x: len(x[1]),
reverse=True)]
def random_subseq(a, rate):
"""
Dropping some points between a[3:-2] randomly according to rate.
Input:
a (array[int])
rate (float)
"""
idx = np.random.rand(len(a)) < rate
idx[0], idx[-1] = True, True
return a[idx]
def pad_array(a, max_length, PAD=constants.PAD):
"""
a (array[int32]), padding with max_length
"""
return np.concatenate((a, [PAD]*(max_length - len(a))))
def pad_arrays(a):
'''
padding each a[i] with max_length, return LongTensor
'''
max_length = max(map(len, a))
a = [pad_array(a[i], max_length) for i in range(len(a))]
a = np.stack(a).astype(np.int)
return torch.LongTensor(a)
def pad_arrays_pair(src, trg, keep_invp=False):
"""
---
Input:
src (list[array[int32]]) (batch,variant_len)
trg (list[array[int32]]) (batch,variant_len)
---
Output:
src (seq_len1, batch) with decending length
trg (seq_len2, batch) with decending length
lengths (1, batch)
invp (batch,): inverse permutation, src.t()[invp] gets original order
e.g. if src length is [4,1,3,2,0], idx = [0,2,3,1,4], invp = [0,3,1,2,4], scr[invp] = [4,1,3,2,0]
"""
TD = namedtuple('TD', ['src', 'lengths', 'trg', 'invp'])
assert len(src) == len(
trg), "source and target should have the same length"
idx = argsort(src)
src = list(np.array(src, dtype=object)[idx])
trg = list(np.array(trg, dtype=object)[idx])
lengths = list(map(len, src))
lengths = torch.LongTensor(lengths)
src = pad_arrays(src)
trg = pad_arrays(trg)
if keep_invp == True:
invp = torch.LongTensor(invpermute(idx))
# (batch, seq_len) => (seq_len, batch)
return TD(src=src.t().contiguous(), lengths=lengths.view(1, -1), trg=trg.t().contiguous(), invp=invp)
else:
# (batch, seq_len) => (seq_len, batch)
return TD(src=src.t().contiguous(), lengths=lengths.view(1, -1), trg=trg.t().contiguous(), invp=[])
def invpermute(p):
"""
inverse permutation
p:[1,4,0,2,3]
invp:[2,0,3,4,1]
"""
p = np.asarray(p)
invp = np.empty_like(p)
for i in range(p.size):
invp[p[i]] = i
return invp
def pad_arrays_keep_invp(src):
"""
Pad arrays and return inverse permutation
Input:
src (list[array[int32]])
---
Output:
src (seq_len, batch)
lengths (1, batch)
invp (batch,): inverse permutation, src.t()[invp] gets original order
"""
idx = argsort(src)
src = list(np.array(src, dtype=object)[idx])
lengths = list(map(len, src))
lengths = torch.LongTensor(lengths)
src = pad_arrays(src)
invp = torch.LongTensor(invpermute(idx))
return src.t().contiguous(), lengths.view(1, -1), invp
def load_label(labelpath):
'''
Load label data: numpy array (datasize,)
'''
f = open(labelpath, 'rb')
y = pickle.load(f)
return y
class DataLoader():
"""
srcfile: source file name(with noise)
trgfile: target file name(original trajectory)
mtafile: meta file name(the centroid offset of the trip)
batch: batch size
validate: if validate = True return batch orderly otherwise return
batch randomly
"""
def __init__(self, srcfile, trgfile, mtafile, batch, validate=False):
self.srcfile = srcfile
self.trgfile = trgfile
self.mtafile = mtafile
self.batch = batch
self.validate = validate
def load(self):
'''
load src/target/meta trajectory
'''
self.start = 0
self.srcdata = []
self.trgdata = []
self.mtadata = []
srcstream, trgstream, mtastream = open(self.srcfile, 'r'), open(
self.trgfile, 'r'), open(self.mtafile, 'r')
num_line = 0
for (s, t, m) in zip(srcstream, trgstream, mtastream):
s = [int(x) for x in s.split()]
t = [constants.BOS] + [int(x) for x in t.split()] + [constants.EOS]
m = [float(x) for x in m.split()]
self.srcdata.append(np.array(s, dtype=np.int32))
self.trgdata.append(np.array(t, dtype=np.int32))
self.mtadata.append(np.array(m, dtype=np.float32))
num_line += 1
self.srcdata = np.array(self.srcdata, dtype=object)
self.trgdata = np.array(self.trgdata, dtype=object)
self.mtadata = np.array(self.mtadata, dtype=object)
self.size = num_line
srcstream.close(), trgstream.close(), mtastream.close()
# print("=> Loaded data size: ", num_line)
def getbatch_one(self):
'''
Get one batch size data
If training, randomly select, otherwise orderly
'''
if self.validate == True:
src = self.srcdata[self.start:self.start+self.batch]
trg = self.trgdata[self.start:self.start+self.batch]
mta = self.mtadata[self.start:self.start+self.batch]
# update `start` for next batch
self.start += self.batch
if self.start >= self.size:
self.start = 0
return list(src), list(trg), list(mta)
else:
# random select from training datasets
idx = np.random.choice(self.size, self.batch)
src = self.srcdata[idx]
trg = self.trgdata[idx]
mta = self.mtadata[idx]
return list(src), list(trg), list(mta)
def getbatch_generative(self):
'''
get batch src / trg data with padding length
'''
src, trg, _ = self.getbatch_one()
# src (seq_len1, batch), lengths (1, batch), trg (seq_len2, batch)
return pad_arrays_pair(src, trg, keep_invp=False)
class DataOrderScaner():
def __init__(self, srcfile, batch):
self.srcfile = srcfile
self.batch = batch
self.srcdata = []
self.trgdata = []
self.start = 0
def load(self):
num_line = 0
with open(self.srcfile, 'r') as f:
srcstream = f.readlines()
for s in srcstream:
s = [int(x) for x in s.strip().split()]
t = [constants.BOS] + s + [constants.EOS]
self.srcdata.append(np.array(s))
self.trgdata.append(np.array(t))
num_line += 1
self.size = num_line
self.start = 0
def reload(self):
self.start = 0
def getbatch(self, invp=True):
"""
get batch src / trg data(the same) with padding length
---
Output:
src (seq_len1, batch) with decending length
trg (seq_len2, batch) with decending length
lengths (1, batch)
invp (batch,): inverse permutation, src.t()[invp] gets original order
if [start:batch] > left, return left data
"""
if self.start >= self.size:
return None
src = self.srcdata[self.start:self.start+self.batch]
trg = self.trgdata[self.start:self.start+self.batch]
# update `start` for next batch
self.start += self.batch
return pad_arrays_pair(src, trg, invp)
def get_random_batch(self):
return random.sample(self.srcdata, self.batch)
def getbatch_discriminative(self):
'''
Get batch for anchor / negative data
Positive data get from anchor dropping
'''
a_src = self.get_random_batch()
n_src = self.get_random_batch()
p_src = []
for i in range(len(a_src)):
a = a_src[i]
if len(a) < 10:
p_src.append(a)
else:
a1, a3, a5 = 0, len(a)//2, len(a)
a2, a4 = (a1 + a3)//2, (a3 + a5)//2
rate = np.random.choice([0.5, 0.6, 0.7])
if np.random.rand() > 0.5:
p_src.append(random_subseq(a[a2:a5], rate))
else:
p_src.append(random_subseq(a[a1:a4], rate))
a = pad_arrays_pair(a_src, a_src, keep_invp=True)
p = pad_arrays_pair(p_src, p_src, keep_invp=True)
n = pad_arrays_pair(n_src, n_src, keep_invp=True)
return a, p, n