-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
378 lines (306 loc) · 14.9 KB
/
dataset.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import os
from typing import Optional, List
from tqdm.auto import tqdm
from transformers import PreTrainedTokenizer
import torch
from torch.nn import CrossEntropyLoss
from torch.utils.data import Dataset, TensorDataset, DataLoader
from pytorch_lightning import LightningDataModule
from util import get_tokenizer
from util.utils import load_txt
class IntentClassificationDataset(Dataset):
def __init__(
self,
data_dir: str,
tokenizer: PreTrainedTokenizer,
max_seq_length: int,
intent_vocab: dict,
mode: str = "train"
):
"""
`IntentClassificationDataset` read data files and convert to tensor dataset.
Args:
data_dir (str): path of dataset directory.
tokenizer (PreTrainedTokenizer): huggingface's PreTrainedTokenizer object.
max_seq_length (int): the maximum length (in number of tokens) for the inputs to the transformer model.
intent_vocab (dict): vocabulary of intent.
mode (str, optional): type of current dataset, e.g. "train", "valid", "test". defaults to "train".
"""
# load text data and label
text_data = load_txt(os.path.join(data_dir, mode, "seq.in"))
intent_data = load_txt(os.path.join(data_dir, mode, "label"))
# check number of data
assert len(text_data) == len(intent_data), "text and intent must have the same number of elements!"
# convert data to features
features = []
for idx, (text, intent) in tqdm(enumerate(zip(text_data, intent_data)), total=len(text_data)):
if tokenizer.__class__.__name__ in [
"RobertaTokenizer",
"LongformerTokenizer",
"BartTokenizer",
"RobertaTokenizerFast",
"LongformerTokenizerFast",
"BartTokenizerFast",
]:
tokens = tokenizer(text, padding='max_length', max_length=max_seq_length, truncation=True, add_prefix_space=True)
else:
tokens = tokenizer(text, padding='max_length', max_length=max_seq_length, truncation=True)
intent_id = intent_vocab[intent]
if "token_type_ids" in tokens.keys():
feature = {
"input_ids" : tokens["input_ids"],
"token_type_ids" : tokens["token_type_ids"],
"attention_mask" : tokens["attention_mask"],
"intent_id" : intent_id
}
else:
feature = {
"input_ids": tokens["input_ids"],
"attention_mask": tokens["attention_mask"],
"intent_id": intent_id
}
feature.update({"token_type_ids": [0]})
features.append(feature)
self.features = features
# convert features to TensorDataset
all_input_ids = torch.tensor([f["input_ids"] for f in features], dtype=torch.long)
all_attention_masks = 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_intent_ids = torch.tensor([f["intent_id"] for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_masks, all_token_type_ids, all_intent_ids)
self.dataset = dataset
def __getitem__(self, idx: int):
return self.dataset[idx]
def __len__(self):
return len(self.dataset)
class IntentClassificationDataModule(LightningDataModule):
def __init__(
self,
data_name: str,
model_type: str,
model_name: str,
max_seq_length: int = 128,
batch_size: int = 8
):
"""
`IntentClassificationDataModule` prepare data and data loaders for intent classification.
Args:
data_name (str): dataset name, e.g., "atis"
model_type (str): model type, e.g., "bert"
model_name (str): name of the specific model, e.g., "bert-base-cased"
max_seq_length (int, optional): the maximum length (in number of tokens) for the inputs to the transformer model.
defaults to 128.
batch_size (int, optional): batch size for single GPU. defaults to 8.
"""
super().__init__()
# configures
self.data_name = data_name.lower()
if self.data_name not in self.get_supported_dataset():
raise NotImplementedError(self.data_name)
self.model_type = model_type
self.model_name = model_name
self.do_lower_case = True if "uncased" in model_name else False
self.max_seq_length = max_seq_length
self.batch_size = batch_size
# for balancing between CPU and GPU
self.num_workers = 4 * torch.cuda.device_count()
def prepare_data(self) -> None:
# prepare data directory
self.data_dir = os.path.join("./", "data", self.data_name)
# load tokenizer
self.tokenizer = get_tokenizer(self.model_type, self.model_name, self.do_lower_case)
# load vocab of intent
self.intent_vocab = self._load_vocab(os.path.join(self.data_dir, "intent.vocab"))
self.num_intents = len(self.intent_vocab)
def _load_vocab(self, file_path: str) -> dict:
vocab = {}
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
line = line.rstrip()
label, id = line.split("\t")
vocab[label] = int(id)
return vocab
def setup(self, stage: Optional[str] = None) -> None:
# load train/val dataset objects
if stage == "fit" or stage is None:
train_dataset = IntentClassificationDataset(self.data_dir, self.tokenizer,
self.max_seq_length, self.intent_vocab, "train")
val_dataset = IntentClassificationDataset(self.data_dir, self.tokenizer,
self.max_seq_length, self.intent_vocab, "valid")
self.train_dataset = train_dataset
self.val_dataset = val_dataset
# load test dataset objects
if stage == 'test' or stage is None:
test_dataset = IntentClassificationDataset(self.data_dir, self.tokenizer,
self.max_seq_length, self.intent_vocab, "test")
self.test_dataset = test_dataset
def train_dataloader(self) -> DataLoader:
return DataLoader(self.train_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=True)
def val_dataloader(self) -> DataLoader:
return DataLoader(self.val_dataset, batch_size=self.batch_size, num_workers=self.num_workers)
def test_dataloader(self) -> DataLoader:
return DataLoader(self.test_dataset, batch_size=self.batch_size, num_workers=self.num_workers)
@staticmethod
def get_supported_dataset() -> List[str]:
"""
This function returns list of supported dataset name by this data module.
Returns:
List[str]: list of supported dataset name.
"""
return ["atis", "snips"]
class EntityRecognitionDataset(Dataset):
def __init__(
self,
data_dir: str,
tokenizer: PreTrainedTokenizer,
max_seq_length: int,
entity_vocab: dict,
mode: str = "train"
):
"""
`EntityRecognitionDataset` read data files and convert to tensor dataset.
Args:
data_dir (str): path of dataset directory.
tokenizer (PreTrainedTokenizer): huggingface's PreTrainedTokenizer object.
max_seq_length (int): the maximum length (in number of tokens) for the inputs to the transformer model.
entity_vocab (dict): vocabulary of entity.
mode (str, optional): type of current dataset, e.g. "train", "valid", "test". defaults to "train".
"""
# load text data and label
text_data = load_txt(os.path.join(data_dir, mode, "seq.in"))
entities_data = load_txt(os.path.join(data_dir, mode, "seq.out"))
# check number of data
assert len(text_data) == len(entities_data), "text and entity must have the same number of elements!"
# get ignore_index
pad_token_label_id = CrossEntropyLoss().ignore_index
# convert data to features
features = []
for idx, (text, entities_str) in tqdm(enumerate(zip(text_data, entities_data)), total=len(text_data)):
tokens = []
label_ids = []
words = text.split()
labels = entities_str.split()
for word, label in zip(words, labels):
word_tokens = tokenizer.tokenize(word)
if not word_tokens:
word_tokens = [tokenizer.unk_token] # For handling the bad-encoded word
tokens.extend(word_tokens)
# Use the real label id for the first token of the word, and padding ids for the remaining tokens
label_ids.extend([entity_vocab[label]] + [pad_token_label_id] * (len(word_tokens) - 1))
special_tokens_count = 2
if len(tokens) > max_seq_length - special_tokens_count:
tokens = tokens[:(max_seq_length - special_tokens_count)]
label_ids = label_ids[:(max_seq_length - special_tokens_count)]
# Add [SEP]
tokens += [tokenizer.sep_token]
label_ids += [pad_token_label_id]
# Add [CLS]
tokens = [tokenizer.cls_token] + tokens
label_ids = [pad_token_label_id] + label_ids
token_type_ids = [0] * len(tokens)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
attention_mask = [1] * len(input_ids)
padding_length = max_seq_length - len(input_ids)
input_ids += [tokenizer.pad_token_id] * padding_length
attention_mask += [0] * padding_length
token_type_ids += [0] * padding_length
label_ids += [pad_token_label_id] * padding_length
assert len(input_ids) == max_seq_length
assert len(attention_mask) == max_seq_length
assert len(token_type_ids) == max_seq_length
assert len(label_ids) == max_seq_length
entities = label_ids
feature = {
"input_ids" : input_ids,
"token_type_ids" : token_type_ids,
"attention_mask" : attention_mask,
"entities" : entities
}
features.append(feature)
self.features = features
# convert features to TensorDataset
all_input_ids = torch.tensor([f["input_ids"] for f in features], dtype=torch.long)
all_attention_masks = 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_entities = torch.tensor([f["entities"] for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_masks, all_token_type_ids, all_entities)
self.dataset = dataset
def __getitem__(self, idx: int):
return self.dataset[idx]
def __len__(self):
return len(self.dataset)
class EntityRecognitionDataModule(LightningDataModule):
def __init__(
self,
data_name: str,
model_type: str,
model_name: str,
max_seq_length: int = 128,
batch_size: int = 8
):
"""
`EntityRecognitionDataModule` prepare data and data loaders for entity recognition.
Args:
data_name (str): dataset name, e.g., "atis"
model_type (str): model type, e.g., "bert"
model_name (str): name of the specific model, e.g., "bert-base-cased"
max_seq_length (int, optional): the maximum length (in number of tokens) for the inputs to the transformer model.
defaults to 128.
batch_size (int, optional): batch size for single GPU. defaults to 8.
"""
super().__init__()
# configures
self.data_name = data_name.lower()
if self.data_name not in self.get_supported_dataset():
raise NotImplementedError(self.data_name)
self.model_type = model_type
self.model_name = model_name
self.do_lower_case = True if "uncased" in model_name else False
self.max_seq_length = max_seq_length
self.batch_size = batch_size
# for balancing between CPU and GPU
self.num_workers = 4 * torch.cuda.device_count()
def prepare_data(self) -> None:
# prepare data directory
self.data_dir = os.path.join("./", "data", self.data_name)
# load tokenizer
self.tokenizer = get_tokenizer(self.model_type, self.model_name, self.do_lower_case)
# load vocab of entity
self.entity_vocab = self._load_vocab(os.path.join(self.data_dir, "entity.vocab"))
self.num_entities = len(self.entity_vocab)
def _load_vocab(self, file_path: str) -> dict:
vocab = {}
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
line = line.rstrip()
label, id = line.split("\t")
vocab[label] = int(id)
return vocab
def setup(self, stage: Optional[str] = None) -> None:
# load train/val dataset objects
if stage == "fit" or stage is None:
train_dataset = EntityRecognitionDataset(self.data_dir, self.tokenizer,
self.max_seq_length, self.entity_vocab, "train")
val_dataset = EntityRecognitionDataset(self.data_dir, self.tokenizer,
self.max_seq_length, self.entity_vocab, "valid")
self.train_dataset = train_dataset
self.val_dataset = val_dataset
# load test dataset objects
if stage == 'test' or stage is None:
test_dataset = EntityRecognitionDataset(self.data_dir, self.tokenizer,
self.max_seq_length, self.entity_vocab, "test")
self.test_dataset = test_dataset
def train_dataloader(self) -> DataLoader:
return DataLoader(self.train_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=True)
def val_dataloader(self) -> DataLoader:
return DataLoader(self.val_dataset, batch_size=self.batch_size, num_workers=self.num_workers)
def test_dataloader(self) -> DataLoader:
return DataLoader(self.test_dataset, batch_size=self.batch_size, num_workers=self.num_workers)
@staticmethod
def get_supported_dataset() -> List[str]:
"""
This function returns list of supported dataset name by this data module.
Returns:
List[str]: list of supported dataset name.
"""
return ["atis", "snips"]