-
Notifications
You must be signed in to change notification settings - Fork 3
/
tf_loader.py
61 lines (49 loc) · 2 KB
/
tf_loader.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
import os
import pickle
import numpy as np
from operator import itemgetter
class text_dataloader(object):
def __init__(self, data_path, max_sent, max_doc, mode, batch_size):
with open(os.path.join(data_path, 'word_vocab.pkl'), 'rb') as f:
self.word_vocab = pickle.load(f)
with open(os.path.join(data_path, '{}_data.pkl'.format(mode)), 'rb') as f:
data_ = pickle.load(f)
self.x, self.y = data_['x'], data_['y']
self.n_classes = len(np.unique(self.y))
self.max_sent = max_sent
self.max_doc = max_doc
self.batch_size = batch_size
assert len(self.y) % batch_size == 0, \
"need to (data length % batch == 0)"
def get_batch(self, shuffle=True):
if shuffle:
indices = np.arange(len(self.y))
np.random.shuffle(indices)
x = itemgetter(*indices)(self.x)
y = itemgetter(*indices)(self.y)
else:
x, y = self.x, self.y
for i in range(0, len(y)-self.batch_size, self.batch_size):
sub_x = x[i:i+self.batch_size]
sub_y = y[i:i+self.batch_size]
batch_x = np.zeros([self.batch_size, self.max_doc, self.max_sent])
for di, doc in enumerate(sub_x):
doc_ = np.zeros([self.max_doc, self.max_sent])
if len(doc) > self.max_doc:
doc = doc[-self.max_doc:]
for si, sent in enumerate(doc):
if len(sent) > self.max_sent:
sent = sent[-self.max_sent:]
doc_[si][:len(sent)] = sent
batch_x[di] = doc_
batch_y = self.make_one_hot(sub_y)
batch_x.astype(int)
batch_y.astype(int)
yield batch_x, batch_y
def make_one_hot(self, y_list):
out = []
for y in y_list:
yi = [0] * self.n_classes
yi[y-1] = 1
out.append(yi)
return np.array(out)