-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
216 lines (176 loc) · 6.4 KB
/
data_utils.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
"""
Dataset ETL Utilities
References:
- https://lightning.ai/lightning-ai/studios/code-lora-from-scratch?tab=overview
"""
# Imports
import re
from datasets import load_dataset, DatasetDict
from transformers import AutoTokenizer
from torch.utils.data import DataLoader, Dataset
import transformers
class CustomDataset(Dataset):
def __init__(self, dataset_dict: DatasetDict, partition: str = "train"):
self.data = dataset_dict[partition]
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]
class DatasetUtils:
def __init__(
self,
dataset_uri: str = "stanfordnlp/imdb",
model_uri: str = "distilbert/distilbert-base-uncased",
batch_size: int = 64,
num_workers: int = 8,
seed: int = 42,
) -> None:
"""
Args:
dataset_uri: str
Huggingface URI of the dataset to be used
model_uri: str
URI of the pre-trained model to be used
batch_size: int
Batch size for the dataloaders
num_workers: int
Number of workers for the dataloaders
seed: int
Seed for reproducibility
"""
self.dataset_uri = dataset_uri
self.model_uri = model_uri
self.batch_size = batch_size
self.num_workers = num_workers
self.seed = seed
self.dataset = None
self.tokenized_dataset = None
self.train_loader = None
self.val_loader = None
self.test_loader = None
# Setup
self.__setup()
def __load(self):
"""
Method to load the dataset from Huggingface
and split into train, val, test
"""
# Load
print("[DEBUG]Loading the dataset...")
dataset = load_dataset(self.dataset_uri, split="train")
# Split data into 80-10-10 (train-val-test)
# Shuffled by default
print("[DEBUG]Splitting the dataset...")
## 1. Split into train-val (90%) and test (10%)
trainval_test = dataset.train_test_split(
test_size=0.1
) # trainval_test["train"] -> Train and val; trainval_test["test"] -> Test
## 2. Split train-val into train (90%) and val (10%)
train_val = trainval_test["train"].train_test_split(
test_size=0.1
) # train_val["train"] -> Train; train_val["test"] -> val
self.dataset = DatasetDict(
{
"train": train_val["train"],
"val": train_val["test"],
"test": trainval_test["test"],
}
)
def __preprocess(self):
"""
Method to preprocess the dataset.
Performs the following preprocessing steps:
- Removes extra whitespaces
- Removes special characters
- Removes html tags
"""
def __get_cleaned_text(text: str) -> str:
"""Method to clean the text"""
# Remove extra whitespaces across the text
text = " ".join(text.split())
# Remove html-like tags
text = re.sub(r"<[^>]+>", "", text)
# Remove special characters
text = re.sub(r"[^a-zA-Z0-9\s.,!?\-\"\']", "", text)
return text
def __batch_clean(batch):
"""Method that cleans a batch of data"""
batch["text"] = [__get_cleaned_text(text) for text in batch["text"]]
return batch
print("[DEBUG]Preprocessing the dataset...")
self.dataset = self.dataset.map(__batch_clean, batched=True)
def __tokenize(self):
"""Method to tokenize the dataset in batches"""
def __batch_tokenize(batch):
"""Method that tokenizes a batch of data"""
return self.tokenizer(batch["text"], truncation=True, padding="max_length")
print("[DEBUG]Tokenizing the dataset...")
# Setup the tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(self.model_uri)
self.tokenized_dataset = self.dataset.map(__batch_tokenize, batched=True)
self.tokenized_dataset.set_format(
"torch", columns=["input_ids", "attention_mask", "label"]
)
def __setup_dataloaders(self):
"""Method to setup all the dataloaders"""
print("[DEBUG]Setting up the dataloaders...")
# Define dataset objects for each partition
train_tokenized_dataset = CustomDataset(
self.tokenized_dataset, partition="train"
)
val_tokenized_dataset = CustomDataset(self.tokenized_dataset, partition="val")
test_tokenized_dataset = CustomDataset(self.tokenized_dataset, partition="test")
# Define the dataloaders for each partition
self.train_loader = DataLoader(
train_tokenized_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
)
self.val_loader = DataLoader(
val_tokenized_dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
)
self.test_loader = DataLoader(
test_tokenized_dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
)
def __setup(self):
"""
Method to setup the dataset and dataloaders
"""
# Set seeds
transformers.set_seed(self.seed)
# Load the data
self.__load()
# Pre-process the data
self.__preprocess()
# Tokenize the data
self.__tokenize()
# Setup the data loaders
self.__setup_dataloaders()
print("[DEBUG]Data setup complete.")
def get_data_loader(self, which: str = "train"):
"""
Method to get the dataloader for the specified partition
Args:
which: str
Partition to get the dataloader for
One of ("train", "val", "test")
Returns:
DataLoader: DataLoader for the specified partition
"""
allowed = ("train", "val", "test")
if which not in allowed:
raise ValueError(
f"Invalid value '{which}' received. Supported one of ({allowed})."
)
match which:
case "train":
return self.train_loader
case "val":
return self.val_loader
case "test":
return self.test_loader