-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
145 lines (128 loc) · 4.75 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
import torch
import pytorch_lightning as pl
from torch.nn import functional as F
from typing import Optional
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from torch.utils.data import DataLoader, random_split
from utils import dataset_iterator, TrainingConfig
class MultiModalDataset(torch.utils.data.Dataset):
"""
Assumes the following data format:
- a single text column
- a single label column
- a list of numerical columns
- a list of categorical columns
"""
def __init__(
self,
dataset_uri: str,
text_column: str,
label_column: str,
num_cols: list[str],
cat_cols: list[str],
):
it = dataset_iterator(dataset_uri)
self.texts = []
raw_labels = []
self.meta_num = []
self.meta_cat = []
for rec in it:
self.texts.append(rec[text_column])
raw_labels.append(rec[label_column])
self.meta_num.append([float(rec[col]) for col in num_cols])
self.meta_cat.append([str(rec[col]) for col in cat_cols])
self.label_names = sorted(list(set(raw_labels)))
labels_inverse = {lbl: i for i, lbl in enumerate(self.label_names)}
self.labels = [labels_inverse[lbl] for lbl in raw_labels]
self.num_encoder = None
self.cat_encoders = None
def __getitem__(self, i):
cat_meta = torch.concat([
F.one_hot(
torch.tensor(enc.transform([val])[0]),
num_classes=len(enc.classes_)
)
for val, enc in zip(self.meta_cat[i], self.cat_encoders)
])
num_meta = torch.tensor(self.num_encoder.transform([self.meta_num[i]])[0])
return (
self.texts[i],
torch.cat([cat_meta, num_meta]).float(),
self.labels[i],
)
def __len__(self):
return len(self.labels)
class MultiModalTestDataset(torch.utils.data.Dataset):
"""
The format of data is the same as for the MultiModalDataset
"""
def __init__(
self,
dataset_uri: str,
text_column: str,
num_cols: list[str],
cat_cols: list[str],
num_encoder: MinMaxScaler,
cat_encoders: list[LabelEncoder]
):
it = dataset_iterator(dataset_uri)
self.texts = []
self.meta_num = []
self.meta_cat = []
for rec in it:
self.texts.append(rec[text_column])
self.meta_num.append([float(rec[col]) for col in num_cols])
self.meta_cat.append([str(rec[col]) for col in cat_cols])
self.num_encoder = num_encoder
self.cat_encoders = cat_encoders
def __getitem__(self, i):
cat_meta = torch.concat([
F.one_hot(
torch.tensor(enc.transform([val])[0]),
num_classes=len(enc.classes_)
)
for val, enc in zip(self.meta_cat[i], self.cat_encoders)
])
num_meta = torch.tensor(self.num_encoder.transform([self.meta_num[i]])[0])
return (
self.texts[i],
torch.cat([cat_meta, num_meta]).float()
)
def __len__(self):
return len(self.texts)
class MultiModalModule(pl.LightningDataModule):
def __init__(
self,
dataset,
num_cols: list[str],
cat_cols: list[str],
config: TrainingConfig
):
super().__init__()
self.dataset = dataset
self.batch_size = config.batch_size
self.splits = config.splits
self.num_cols = num_cols
self.cat_cols = cat_cols
self.setup()
def setup(self, stage: Optional[str] = None):
assert len(self.splits) == 3, "'splits' must have exactly 3 elements"
train_len = int(len(self.dataset) * self.splits[0])
val_len = int(len(self.dataset) * self.splits[1])
test_len = len(self.dataset) - train_len - val_len
self.train_set, self.val_set, self.test_set = random_split(
self.dataset, [train_len, val_len, test_len]
)
train_idc = set(self.train_set.indices)
cat_encoders = []
for i in range(len(self.dataset.meta_cat[0])):
cat_list = [item[i] for j, item in enumerate(self.dataset.meta_cat) if j in train_idc]
cat_encoders.append(LabelEncoder().fit(cat_list))
self.dataset.cat_encoders = cat_encoders
self.dataset.num_encoder = MinMaxScaler().fit(self.dataset.meta_num)
def train_dataloader(self):
return DataLoader(self.train_set, batch_size=self.batch_size)
def val_dataloader(self):
return DataLoader(self.val_set, batch_size=self.batch_size)
def test_dataloader(self):
return DataLoader(self.test_set, batch_size=self.batch_size)