-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafeed.py
278 lines (211 loc) · 8.67 KB
/
datafeed.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
from pprint import pprint, pformat
import logging
from pprint import pprint, pformat
logging.basicConfig(format="%(levelname)-8s:%(filename)s.%(funcName)20s >> %(message)s")
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
import random
from anikattu.utilz import tqdm
from anikattu.debug import memory_consumed
from collections import Counter
class DataFeed(object):
def __init__(self, name, datapoints, batchop, batch_size=1, sort_key=None):
self.name = name
self._offset = 0
self._size = len(datapoints)
self._batch_size = batch_size
self._batchop = batchop
self._batch_cache = {}
self._exhausted_count = 0
if len(datapoints):
if sort_key:
datapoints = sorted(datapoints, key=sort_key)
self.bind(datapoints)
log.info('built Datafeed: {} with the following props:'.format(self.name))
log.info(' size : {}'.format(self.size))
log.info(' batch_size : {}'.format(self.batch_size))
log.info(' num_batch : {}'.format(self.num_batch))
def bind(self, datapoints):
self._size = len(datapoints)
self._data = datapoints
self._data_dict = {}
if self.size > self.batch_size * self.num_batch:
log.info('batch bleeds')
self._size += self.batch_size
self.reset_offset()
for d in datapoints:
self._data_dict[d.id] = d
@property
def data(self):
return self._data
@property
def data_dict(self):
return self._data_dict
@property
def size(self):
return self._size
@property
def batch_size(self):
return self._batch_size
@property
def num_batch(self):
return int(self.size/self.batch_size)
@property
def offset(self):
return self._offset
def __repr__(self):
return 'DataFeed-{}:\n\t{}'.format(self.name, self.size)
def batch(self, batch_size=None, apply_batchop=True):
if not batch_size:
batch_size = self.batch_size
self._offset += batch_size
b = self.data[ self.offset - batch_size : self.offset ]
if len(b) < 1:
raise Exception
if apply_batchop:
return self._batchop(b)
return b
def next_batch(self, batch_size=None, apply_batchop=True, **kwargs):
try:
if not batch_size:
batch_size = self.batch_size
if self.offset + batch_size > self.size:
self._exhausted_count += 1
self.reset_offset()
log.debug('datafeed: {} over run - resetting offset to zero for {} time'
.format(self.name, self._exhausted_count))
return self.batch(batch_size=batch_size, apply_batchop=apply_batchop)
except KeyboardInterrupt:
raise KeyboardInterrupt
except SystemExit:
exit(1)
except:
log.exception('batch failed')
return self.next_batch(apply_batchop=apply_batchop)
def nth_batch(self, n, batch_size=None, apply_batchop=True):
if not batch_size:
batch_size = self.batch_size
b = self.data[ n * batch_size : (n+1) * batch_size ]
if len(b) < 1:
if not (n - 1) > 0:
return self.nth_batch(n-1, batch_size, apply_batchop)
else:
return self.nth_batch(0, batch_size, apply_batchop)
if apply_batchop:
return self._batchop(b)
return b
def reset_offset(self):
self._offset = 0
class MultiplexedDataFeed(DataFeed):
def __init__(self, name, datafeeds, batchop, batch_size=1, vocab=None, sort_key=None):
self.name = name
self._offset = 0
self._size = sum([feed.size for feed in datafeeds.values()])
self._batch_size = batch_size
self._batchop = batchop
self.vocab = vocab
self._batch_cache = {}
self._exhausted_count = 0
self.bind(datafeeds)
self.sampling_distribution_counter = Counter()
log.info('built MultiplexedDatafeed: {} with the following props:'.format(self.name))
log.info(' size : {}'.format(self.size))
log.info(' batch_size : {}'.format(self.batch_size))
log.info(' num_batch : {}'.format(self.num_batch))
log.info(pformat(self.datafeeds.items()))
def bind(self, datafeeds):
self.datafeeds = datafeeds
self._data_dict = {}
if self.size > self.batch_size * self.num_batch:
log.info('batch bleeds')
self._size += self.batch_size
self.reset_offset()
for fname, datafeed in self.datafeeds.items():
for d in datafeed.data:
self._data_dict[d.id] = d
@property
def data_dict(self):
return self._data_dict
@property
def size(self):
return self._size
@property
def batch_size(self):
return self._batch_size
@property
def num_batch(self):
return int(self.size/self.batch_size)
@property
def offset(self):
return self._offset
def batch(self, batch_size=None, apply_batchop=True, sampling_distribution=None):
if not batch_size:
batch_size = self.batch_size
b = []
if sampling_distribution:
#pprint(sampling_distribution)
sampling_distribution = {
k : v
for k,v in sampling_distribution.items()
if k in self.datafeeds.keys()
}
total = sum(sampling_distribution.values())
sampling_distribution = {
k : int( (v/total) * (batch_size/2) ) #Allocate half batch with distribution
for k,v in sampling_distribution.items()
}
#pprint(sampling_distribution)
#pprint(sampling_distribution)
self.sampling_distribution_counter.update(sampling_distribution)
sampling_distribution = sorted(sampling_distribution.items(),
key=lambda x: x[1],
reverse=True)
#pprint(sampling_distribution)
log.debug(pformat(sampling_distribution))
for fname, size in sampling_distribution:
b.extend(
self.datafeeds[fname].next_batch(batch_size=size,
apply_batchop=False))
for fname, feed in self.datafeeds.items():
if len(b) >= batch_size:
break
b.extend(
feed.next_batch(
batch_size = (batch_size//2) // len(self.datafeeds), #Allocate another half here
apply_batchop = False)
)
self._offset += batch_size
if apply_batchop:
return self._batchop(b)
return b
def next_batch(self, batch_size=None, apply_batchop=True, sampling_distribution=None):
super().next_batch(batch_size, apply_batchop, sampling_distribution=sampling_distribution)
def next_batch(self, batch_size=None, apply_batchop=True, sampling_distribution=None):
try:
if not batch_size:
batch_size = self.batch_size
if self.offset + batch_size > self.size:
self._exhausted_count += 1
self.reset_offset()
log.debug('datafeed: {} over run - resetting offset to zero for {} time'
.format(self.name, self._exhausted_count))
return self.batch(batch_size=batch_size, apply_batchop=apply_batchop, sampling_distribution=sampling_distribution)
except KeyboardInterrupt:
raise KeyboardInterrupt
except SystemExit:
exit(1)
except:
log.exception('batch failed')
return self.next_batch(apply_batchop=apply_batchop)
def nth_batch(self, n, apply_batchop=True):
b = []
for fname, feed in self.datafeeds.items():
b.append(
random.choice(
feed.nth_batch(
min(n, random.choice(range(feed.num_batch))),
apply_batchop=False)))
if len(b) == self.batch_size: break
if apply_batchop:
return self._batchop(b)
return b