-
Notifications
You must be signed in to change notification settings - Fork 15
/
data_loader.py
188 lines (153 loc) Β· 6.46 KB
/
data_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
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
import os
import copy
import json
import logging
import torch
from torch.utils.data import TensorDataset
logger = logging.getLogger(__name__)
class InputExample(object):
"""
A single training/test example for simple sequence classification.
"""
def __init__(self, guid, text_a, text_b, label):
self.guid = guid
self.text_a = text_a
self.text_b = text_b
self.label = label
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self, input_ids, attention_mask, token_type_ids, label):
self.input_ids = input_ids
self.attention_mask = attention_mask
self.token_type_ids = token_type_ids
self.label = label
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
def convert_examples_to_features(
args,
examples,
tokenizer,
max_length,
):
processor = GoEmotionsProcessor(args)
label_list_len = len(processor.get_labels())
def convert_to_one_hot_label(label):
one_hot_label = [0] * label_list_len
for l in label:
one_hot_label[l] = 1
return one_hot_label
labels = [convert_to_one_hot_label(example.label) for example in examples]
batch_encoding = tokenizer.batch_encode_plus(
[(example.text_a, example.text_b) for example in examples], max_length=max_length, pad_to_max_length=True
)
features = []
for i in range(len(examples)):
inputs = {k: batch_encoding[k][i] for k in batch_encoding}
feature = InputFeatures(**inputs, label=labels[i])
features.append(feature)
for i, example in enumerate(examples[:10]):
logger.info("*** Example ***")
logger.info("guid: {}".format(example.guid))
logger.info("sentence: {}".format(example.text_a))
logger.info("tokens: {}".format(" ".join([str(x) for x in tokenizer.tokenize(example.text_a)])))
logger.info("input_ids: {}".format(" ".join([str(x) for x in features[i].input_ids])))
logger.info("attention_mask: {}".format(" ".join([str(x) for x in features[i].attention_mask])))
logger.info("token_type_ids: {}".format(" ".join([str(x) for x in features[i].token_type_ids])))
logger.info("label: {}".format(" ".join([str(x) for x in features[i].label])))
return features
class GoEmotionsProcessor(object):
"""Processor for the GoEmotions data set """
def __init__(self, args):
self.args = args
def get_labels(self):
labels = []
with open(os.path.join(self.args.data_dir, self.args.label_file), "r", encoding="utf-8") as f:
for line in f:
labels.append(line.rstrip())
return labels
@classmethod
def _read_file(cls, input_file):
"""Reads a tab separated value file."""
with open(input_file, "r", encoding="utf-8") as f:
return f.readlines()
def _create_examples(self, lines, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, line) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
line = line.strip()
items = line.split("\t")
text_a = items[0]
label = list(map(int, items[1].split(",")))
if i % 5000 == 0:
logger.info(line)
examples.append(InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
return examples
def get_examples(self, mode):
"""
Args:
mode: train, dev, test
"""
file_to_read = None
if mode == 'train':
file_to_read = self.args.train_file
elif mode == 'dev':
file_to_read = self.args.dev_file
elif mode == 'test':
file_to_read = self.args.test_file
logger.info("LOOKING AT {}".format(os.path.join(self.args.data_dir, file_to_read)))
return self._create_examples(self._read_file(os.path.join(self.args.data_dir,
file_to_read)), mode)
def load_and_cache_examples(args, tokenizer, mode):
processor = GoEmotionsProcessor(args)
# Load data features from cache or dataset file
cached_features_file = os.path.join(
args.data_dir,
"cached_{}_{}_{}_{}".format(
str(args.task),
list(filter(None, args.model_name_or_path.split("/"))).pop(),
str(args.max_seq_len),
mode
)
)
if os.path.exists(cached_features_file):
logger.info("Loading features from cached file %s", cached_features_file)
features = torch.load(cached_features_file)
else:
logger.info("Creating features from dataset file at %s", args.data_dir)
if mode == "train":
examples = processor.get_examples("train")
elif mode == "dev":
examples = processor.get_examples("dev")
elif mode == "test":
examples = processor.get_examples("test")
else:
raise ValueError("For mode, only train, dev, test is available")
features = convert_examples_to_features(
args, examples, tokenizer, max_length=args.max_seq_len
)
logger.info("Saving features into cached file %s", cached_features_file)
torch.save(features, cached_features_file)
# Convert to Tensors and build dataset
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)
all_labels = torch.tensor([f.label for f in features], dtype=torch.float)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids, all_labels)
return dataset