-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchifier.py
More file actions
67 lines (53 loc) · 1.99 KB
/
Copy pathbatchifier.py
File metadata and controls
67 lines (53 loc) · 1.99 KB
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
import numpy as np
import random
import torch
class Batchifier(object):
def __init__(self, idxs, pad_idx, batch_size, shuffle=False):
self.idxs = idxs
self.shuffle = shuffle
self.batch_size = batch_size
self.nbatches = len(idxs) // batch_size
self.pad_idx = pad_idx
self.current_idx = None
self.batches = None
self.reset()
def reset(self):
if self.shuffle:
random.shuffle(self.idxs)
self._build_data(self.idxs)
self.current_idx = 0
def _sort_batch(self, batch):
if not isinstance(batch, np.ndarray):
batch = np.array(batch)
# line contains sos, eos tokens additionally
lengths = np.array([len(line) - 1 for line in batch])
sorted_idxs = np.argsort(lengths)[::-1]
batch, lengths = batch[sorted_idxs], lengths[sorted_idxs]
maxlen = lengths[0]
src = np.full((self.batch_size, maxlen), self.pad_idx)
tgt = np.copy(src)
for idx, (line, length) in enumerate(zip(batch, lengths)):
src[idx][:length] = line[:-1]
tgt[idx][:length] = line[1:]
src, tgt, lengths = torch.LongTensor(src), torch.LongTensor(tgt), torch.LongTensor(lengths)
return src, tgt, lengths
def _build_data(self, idxs):
self.batches = []
for batch_idx in range(self.nbatches):
batch = idxs[batch_idx*self.batch_size: (batch_idx+1)*self.batch_size]
src, tgt, lengths = self._sort_batch(batch) # should sort batches to pass into lstm with padding
self.batches.append((src, tgt, lengths))
def __iter__(self):
return self
def __len__(self):
return self.nbatches
def __next__(self):
try:
result = self.batches[self.current_idx]
except IndexError:
self.reset()
raise StopIteration
self.current_idx += 1
return result
def get_random(self):
return random.choice(self.batches)