-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
157 lines (118 loc) · 5.08 KB
/
data_loader.py
File metadata and controls
157 lines (118 loc) · 5.08 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
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
#!/usr/bin/env python3
from collections import Counter
from torch.utils.data import DataLoader, Dataset
import torch.autograd as autograd
import torch
import json
import csv
import numpy as np
import pandas as pd
from tqdm import tqdm
import utils
import configparser
def get_sample_weights(labels):
counter = Counter(labels)
counter = dict(counter)
for k in counter:
counter[k] = 1 / counter[k]
sample_weights = np.array([counter[l] for l in labels])
return sample_weights
def load_data(args, mode='train'):
usecols = list(map(lambda x: int(x), args.get('Data', 'usecols').split(',')))
chunks = pd.read_csv(args.get('Data', 'dataset') + '/' + mode + '.csv',
usecols=usecols,
chunksize=args.getint('Data', 'chunk_size'),
encoding=args['Data'].get('encoding'),
nrows=args.getint('Data', 'max_csv_rows'),
sep=args['Data'].get('csv_sep'),
doublequote=True)
texts = []
labels = []
for chunk in tqdm(chunks):
curr_chunk = chunk.copy()
curr_chunk = curr_chunk.sample(frac=1)
# maybe concat with new line instead of space
if args.get('Data', 'dataset') == 'ag_news':
curr_chunk['merged_text'] = curr_chunk.iloc[:, 1] + ' ' + curr_chunk.iloc[:, 2]
texts += curr_chunk['merged_text'].tolist()
else:
texts += curr_chunk.iloc[:, 1].tolist()
labels += curr_chunk.iloc[:, 0].tolist()
# pre-processing data
if bool(args.getboolean('Data', 'preprocess_data')):
texts = list(map(lambda text: utils.process_text(args.get('Data', 'steps').split(','), text), texts))
# class balancing
if bool(args.getboolean('Data', 'balance_classes')):
counter = Counter(labels)
keys = list(counter.keys())
values = list(counter.values())
count_minority = np.min(values)
balanced_labels = []
balanced_texts = []
for key in keys:
balanced_texts += [text for text, label in zip(texts, labels) if label == key][
:int(args.getint('Data', 'ratio') * count_minority)]
balanced_labels += [label for text, label in zip(texts, labels) if label == key][
:int(args.getint('Data', 'ratio') * count_minority)]
texts = balanced_texts
labels = balanced_labels
#if args.get('Data', 'dataset') == 'yelp':
# labels = list(map(lambda x: 1 if x in [1,2] else (3 if x in [4,5] else 2), labels))
number_of_classes = len(set(labels))
sample_weights = get_sample_weights(labels)
return texts, torch.LongTensor(labels), number_of_classes, sample_weights
class MyDataset(Dataset):
def __init__(self, texts, labels, args):
self.data = texts
self.labels = labels
self.length = len(self.labels)
self.alphabet = args.get('DataSet', 'alphabet')
self.number_of_characters = args.getint('DataSet', 'char_num')
self.l0 = args.getint('DataSet', 'l0')
self.identity_mat = np.identity(self.number_of_characters)
def __len__(self):
return self.length
def __getitema__(self, idx):
raw_text = self.data[idx]
data = np.array([self.identity_mat[self.alphabet.index(i)] for i in list(raw_text)[::-1] if i in self.alphabet],
dtype=np.float32)
if len(data) > self.l0:
data = data[:self.l0]
elif 0 < len(data) < self.l0:
data = np.concatenate(
(data, np.zeros((self.l0 - len(data), self.number_of_characters), dtype=np.float32)))
elif len(data) == 0:
data = np.zeros(
(self.l0, self.number_of_characters), dtype=np.float32)
label = self.labels[idx]
data = torch.Tensor(data)
return data, label
def getClassWeight(self):
num_samples = self.__len__()
label_set = set(self.labels)
num_class = [self.labels.count(c) for c in label_set]
class_weight = [num_samples/float(self.labels.count(c)) for c in label_set]
return class_weight, num_class
def __getitem__(self, idx):
X = self.oneHotEncode(idx)
y = self.labels[idx]
return X, y
def oneHotEncode(self, idx):
# X = (batch, 70, sequence_length)
X = torch.zeros(len(self.alphabet), self.l0)
sequence = self.data[idx][:self.l0]
for index_char, char in enumerate(sequence[::-1]):
if self.char2Index(char) != -1:
X[self.char2Index(char)][index_char] = 1.0
return X
def char2Index(self, character):
return self.alphabet.find(character)
if __name__ == '__main__':
args = configparser.ConfigParser()
args.read('argsConfig.ini')
texts, labels, number_of_classes, sample_weights = load_data(args)
#Dat = MyDataset(texts, labels, args)
#gen = DataLoader(Dat, batch_size=128, num_workers=1, drop_last=True, shuffle=True)
#for i_batch, data in enumerate(gen, start=1):
# print(i_batch, data)
print()