-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_loader.py
170 lines (139 loc) · 6.44 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
import os
import torch
from torch.utils import data
from transformers import BertTokenizer
class InputExample(object):
"""A single training/test example for simple sequence classification."""
def __init__(self, guid, text, label=None, segment_ids=None):
"""Constructs a InputExample.
Args:
guid: Unique id for the example.
text_a: string. The untokenized text of the first sequence. For single
sequence tasks, only this sequence must be specified.
label: (Optional) string. The label of the example. This should be
specified for train and dev examples, but not for test examples.
"""
self.guid = guid
self.text = text
self.label = label
self.segment_ids = segment_ids
class Dataset(data.Dataset):
def __init__(self, data_list, tokenizer, label_map, max_len, device):
self.max_len = max_len
self.label_map = label_map
self.data_list = data_list
self.tokenizer = tokenizer
self.device = device
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
input_example = self.data_list[idx]
text = input_example.text
label = input_example.label
word_tokens = ['[CLS]']
label_list = ['[CLS]']
label_mask = [0] # value in (0, 1) - 0 signifies invalid token
input_ids = [self.tokenizer.convert_tokens_to_ids('[CLS]')]
label_ids = [self.label_map['[CLS]']]
# iterate over individual tokens and their labels
for word, label in zip(text.split(), label):
tokenized_word = self.tokenizer.tokenize(word)
for token in tokenized_word:
word_tokens.append(token)
input_ids.append(self.tokenizer.convert_tokens_to_ids(token))
label_list.append(label)
label_ids.append(self.label_map[label])
label_mask.append(1)
# len(tokenized_word) > 1 only if it splits word in between, in which case
# the first token gets assigned NER tag and the remaining ones get assigned
# X
for i in range(1, len(tokenized_word)):
label_list.append('X')
label_ids.append(self.label_map['X'])
label_mask.append(0)
assert len(word_tokens) == len(label_list) == len(input_ids) == len(label_ids) == len(
label_mask)
if len(word_tokens) >= self.max_len:
word_tokens = word_tokens[:(self.max_len - 1)]
label_list = label_list[:(self.max_len - 1)]
input_ids = input_ids[:(self.max_len - 1)]
label_ids = label_ids[:(self.max_len - 1)]
label_mask = label_mask[:(self.max_len - 1)]
assert len(word_tokens) < self.max_len, len(word_tokens)
word_tokens.append('[SEP]')
label_list.append('[SEP]')
input_ids.append(self.tokenizer.convert_tokens_to_ids('[SEP]'))
label_ids.append(self.label_map['[SEP]'])
label_mask.append(0)
assert len(word_tokens) == len(label_list) == len(input_ids) == len(label_ids) == len(
label_mask)
sentence_id = [0 for _ in input_ids]
attention_mask = [1 for _ in input_ids]
while len(input_ids) < self.max_len:
input_ids.append(0)
label_ids.append(self.label_map['X'])
attention_mask.append(0)
sentence_id.append(0)
label_mask.append(0)
assert len(word_tokens) == len(label_list)
assert len(input_ids) == len(label_ids) == len(attention_mask) == len(sentence_id) == len(
label_mask) == self.max_len, len(input_ids)
input_ids, label_ids, label_mask = torch.LongTensor(input_ids), torch.LongTensor(label_ids), torch.BoolTensor(label_mask)
attention_mask, sentence_id = torch.LongTensor(attention_mask), torch.LongTensor(sentence_id)
input_ids, label_ids, label_mask = input_ids.to(self.device), label_ids.to(self.device), label_mask.to(self.device)
attention_mask, sentence_id = attention_mask.to(self.device), sentence_id.to(self.device)
return input_ids, label_ids, attention_mask, sentence_id, label_mask
class DataLoader:
def __init__(self, data_dir, bert_model_dir, params):
self.data_dir = data_dir
self.batch_size = params.batch_size
self.max_len = params.max_len
self.device = params.device
self.seed = params.seed
self.workers_num = params.num_workers
self.tags = ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "[CLS]", "[SEP]", "X"]
self.tag2idx = {tag: idx for idx, tag in enumerate(self.tags)}
self.idx2tag = {idx: tag for idx, tag in enumerate(self.tags)}
params.tag2idx = self.tag2idx
params.idx2tag = self.idx2tag
self.tokenizer = BertTokenizer.from_pretrained(bert_model_dir, do_lower_case=False)
def _create_examples(self, lines, set_type):
examples = []
for i, (sentence, label) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
text_a = ' '.join(sentence)
label = label
examples.append(InputExample(guid=guid, text=text_a, label=label))
return examples
def _readfile(self, filename):
f = open(filename)
data = []
sentence = []
label = []
for line in f:
if len(line) == 0 or line.startswith('-DOCSTART') or line[0] == "\n":
if len(sentence) > 0:
data.append((sentence, label))
sentence = []
label = []
continue
splits = line.split(' ')
sentence.append(splits[0])
label.append(splits[-1][:-1])
if len(sentence) > 0:
data.append((sentence, label))
sentence = []
label = []
return data
def load_data(self, data_type):
if data_type in ['train', 'val', 'test']:
examples = self._create_examples(self._readfile(os.path.join(self.data_dir, data_type + ".txt")), data_type)
return Dataset(examples, self.tokenizer, self.tag2idx, self.max_len, self.device)
else:
raise ValueError("data type not in ['train', 'val', 'test']")
def data_iterator(self, dataset, shuffle=False):
return data.DataLoader(
dataset=dataset,
batch_size=self.batch_size,
shuffle=shuffle
)