-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataModule.py
More file actions
274 lines (222 loc) · 13.4 KB
/
DataModule.py
File metadata and controls
274 lines (222 loc) · 13.4 KB
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
import os
import lightning.pytorch as pl
from torch.utils.data import DataLoader
from models.EncoderDataset import *
from utils.load_datasets import load_dataset
class TopSegDataModule(pl.LightningDataModule):
def __init__(self, data_dir: str = "./data",
dataset = "BBC",
encoder: str = "roberta_base_second_to_last_mean",
architecture: str = "BiLSTM",
max_length: int = None,
batch_size: int = 8,
embedding_directory: str = None,
online_encoding = False,
validation_percentage: float = 0.2,
test_percentage: float = 0.2):
super().__init__()
self.data_dir = data_dir
self.dataset = dataset
self.encoder = encoder
self.architecture = architecture
self.max_length = max_length
self.valid_percentage = validation_percentage
self.test_percentage = test_percentage
self.batch_size = batch_size
self.embeddings_directory = embedding_directory
self.online_encoding = online_encoding
def prepare_data(self):
# download
self.WordMatrix = None
try:
self.folds = load_dataset(self.dataset) # TODO: add the additional options from load_dataset function
if len(self.folds)>1:
raise NotImplementedError("Cross Validation is not currently supported in this version of the project! Working on this...")
elif len(self.folds)==1:
folds = self.folds[0]
assert len(folds), "The chosen dataset returned empty list! Check that you correctly implemented the dataset inclusion procedure, as explained in the README file in the utils subfolder!"
self.has_validation = True
self.has_test = True
if len(folds)==3:
pass
elif len(folds)==2:
self.has_validation = False
elif len(folds)==1:
self.has_validation = False
self.has_test = False
else:
raise ValueError("Your chosen dataset returned either too many splits (>3) or no split at all.")
else:
raise ValueError("The chosen dataset returned empty list! Check that you correctly implemented the dataset inclusion procedure, as explained in the README file in the utils subfolder!")
if self.architecture.lower()=="textseg":
import gensim.downloader
self.WordMatrix = gensim.downloader.load('word2vec-google-news-300')
self.word2index = {w:i for i, w in enumerate(WordMatrix.index_to_key)}
self.WordMatrix = createPreTrainedEmbedding(WordMatrix, word2index, False)
except ValueError:
raise ValueError("The name of the dataset was not recognised! Did you add a condition in the load_dataset function or did you pass a custom function to the dataset argument to return your dataset? If not, follow the rules in the README file inside the utils subfolder to use your custom dataset.")
def setup(self, stage: str):
# Assign train/val datasets for use in dataloaders
if self.architecture.lower().startswith('text'):
encoder = None
elif self.architecture.lower().startswith('crossencoder'):
encoder, _ = self.encoder.split("+")
else:
encoder = get_model(self.encoder, max_length = self.max_length)[0] # TODO: add the additional options from get_model function
in_dim = encoder.get_sentence_embedding_dimension()
precompute = not self.online_encoding
CRF = True if self.architecture.lower().endswith('crf') else False
tag_to_ix = {0:0, 1:1, '<START>':2, '<STOP>':3}
if stage == "fit":
if self.architecture.lower().startswith('text'):
for fold in self.folds:
if self.has_validation:
train_samples = fold[0]
valid_samples = fold[2]
else:
print('performing train/validation split')
valid_num = int(self.valid_percentage*100)
train_samples = []
valid_samples = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
valid_samples.append(sample)
else:
train_samples.append(sample)
self.train_dataset = WordLevelDataset(train_samples, tag_to_ix, self.word2index, self.WordMatrix)
self.valid_dataset = WordLevelDataset(valid_samples, tag_to_ix, self.word2index, self.WordMatrix)
elif self.architecture.lower().startswith('crossencoder'):
for fold in self.folds:
if self.has_validation:
train_samples = fold[0]
valid_samples = fold[2]
else:
print('performing train/validation split')
valid_num = int(self.valid_percentage*100)
train_samples = []
valid_samples = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
valid_samples.append(sample)
else:
train_samples.append(sample)
self.train_dataset = CrossEncoderDataset(train_samples, enc=encoder, minus = CRF, longformer = False)
self.valid_dataset = CrossEncoderDataset(valid_samples, enc=encoder, minus = CRF, longformer = False)
elif self.embeddings_directory is None:
for fold in self.folds:
if self.has_validation:
train_samples = fold[0]
valid_samples = fold[2]
else:
print('performing train/validation split')
valid_num = int(self.valid_percentage*100)
train_samples = []
valid_samples = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
valid_samples.append(sample)
else:
train_samples.append(sample)
# TODO: Add additional options for SentenceDataset among the ones available
self.train_dataset = SentenceDataset(train_samples, tag_to_ix, encoder = encoder, precompute = precompute, CRF =CRF)
self.valid_dataset = SentenceDataset(valid_samples, tag_to_ix, encoder = encoder, precompute = precompute, CRF =CRF)
else:
if self.has_validation:
train_embeddings = load_embeddings(self.encoder + '_train', self.dataset, self.embeddings_directory)
valid_embeddings = load_embeddings(self.encoder + '_valid', self.dataset, self.embeddings_directory)
for fold in self.folds:
self.train_dataset = SentenceDataset(fold[0], tag_to_ix, encoder = encoder, embeddings = train_embeddings, CRF =CRF)
self.valid_dataset = SentenceDataset(fold[2], tag_to_ix, encoder = encoder, embeddings = valid_embeddings, CRF =CRF)
else:
train_embeddings = load_embeddings(self.encoder + '_train', self.dataset, self.embeddings_directory)
valid_num = int(self.valid_percentage*100)
train_samples = []
valid_samples = []
train_embeddings_new = []
valid_embeddings = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
valid_samples.append(sample)
valid_embeddings.append(train_embeddings[index])
else:
train_samples.append(sample)
train_embeddings_new.append(train_embeddings[index])
for fold in self.folds:
self.train_dataset = SentenceDataset(train_samples, tag_to_ix, encoder = encoder, embeddings = train_embeddings_new, CRF =CRF)
self.valid_dataset = SentenceDataset(valid_samples, tag_to_ix, encoder = encoder, embeddings = valid_embeddings, CRF =CRF)
if stage == "test":
if self.architecture.lower().startswith('text'):
import gensim.downloader
WordMatrix = gensim.downloader.load('word2vec-google-news-300')
word2index = {w:i for i, w in enumerate(WordMatrix.index_to_key)}
WordMatrix = createPreTrainedEmbedding(WordMatrix, word2index, False)
for fold in self.folds:
if self.has_test:
train_samples = fold[0]
test_samples = fold[1]
else:
print('performing train/validation split')
valid_num = int(self.test_percentage*100)
train_samples = []
valid_samples = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
test_samples.append(sample)
self.test_dataset = WordLevelDataset(test_samples, tag_to_ix, word2index, WordMatrix)
elif self.architecture.lower().startswith('crossencoder'):
for fold in self.folds:
if self.has_test:
train_samples = fold[0]
test_samples = fold[1]
else:
print('performing train/validation split')
valid_num = int(self.test_percentage*100)
train_samples = []
valid_samples = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
test_samples.append(sample)
self.test_dataset = CrossEncoderDataset(test_samples, enc=encoder, minus = CRF, longformer = False)
elif self.embeddings_directory is None:
for fold in self.folds:
if self.has_test:
train_samples = fold[0]
test_samples = fold[1]
else:
print('performing train/validation split')
valid_num = int(self.test_percentage*100)
train_samples = []
valid_samples = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
test_samples.append(sample)
self.test_dataset = SentenceDataset(test_samples, tag_to_ix, encoder = encoder, precompute = precompute, CRF =CRF)
else:
if self.has_test:
if self.has_validation:
test_embeddings = load_embeddings(self.encoder + '_test', self.dataset, self.embeddings_directory)
for fold in self.folds:
self.test_dataset = SentenceDataset(fold[1], tag_to_ix, encoder = encoder, embeddings = test_embeddings, CRF =CRF)
else:
train_embeddings = load_embeddings(self.encoder + '_train', self.dataset, self.embeddings_directory)
for fold in self.folds:
valid_num = int(self.valid_percentage*100)
train_samples = []
test_samples = []
train_embeddings_new = []
test_embeddings = []
for index, sample in enumerate(fold[0]):
if index%valid_num==0:
test_samples.append(sample)
test_embeddings.append(train_embeddings[index])
self.test_dataset = SentenceDataset(test_samples, tag_to_ix, encoder = encoder, embeddings = test_embeddings, CRF =CRF)
if stage == "predict":
raise NotImplementedError()
def train_dataloader(self):
return DataLoader(self.train_dataset, batch_size=self.batch_size, collate_fn=self.train_dataset.collater)
def val_dataloader(self):
return DataLoader(self.valid_dataset, batch_size=self.batch_size, collate_fn=self.valid_dataset.collater)
def test_dataloader(self):
return DataLoader(self.test_dataset, batch_size=self.batch_size, collate_fn=self.test_dataset.collater)
def predict_dataloader(self):
raise NotImplementedError()