Adding Data Loaders
AIDO.ModelGenerator uses Lightning DataModules for dataset management and loading. We also provide a few tools to make data management more convenient, and work with common file types out-of-the-box.
AIDO.ModelGenerator provides a DataInterface
class that hides boilerplate, along with a HFDatasetLoaderMixin
that combines Lightning DataModule structure and HuggingFace Datasets convenience together to quickly load data from HuggingFace or common file formats (e.g. tsv, csv, json, etc).
More convenient mixins and example usage are outlined below.
Many common tasks and data loaders are already implemented in AIDO.ModelGenerator, and only require setting new paths to run with new data. See the Data API Reference for all types of available data modules.
modelgenerator.data.DataInterface
Bases: LightningDataModule
, KFoldMixin
Base class for all data modules in this project. Handles the boilerplate of setting up data loaders.
Note
Subclasses must implement the setup method.
All datasets should return a dictionary of data items.
To use HF loading, add the HFDatasetLoaderMixin.
For any task-specific behaviors, implement transformations using torch.utils.data.Dataset
objects.
See MLM for an example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the dataset, can be (1) a local path to a data folder or (2) a Huggingface dataset identifier |
required |
config_name
|
Optional[str]
|
The name of the HF dataset configuration. Affects how the dataset is loaded. |
None
|
train_split_name
|
Optional[str]
|
The name of the training split. |
'train'
|
test_split_name
|
Optional[str]
|
The name of the test split. Also used for |
'test'
|
valid_split_name
|
Optional[str]
|
The name of the validation split. |
None
|
train_split_files
|
Optional[Union[str, List[str]]]
|
Create a split called "train" from these files. Not used unless referenced by the name "train" in one of the split_name arguments. |
None
|
test_split_files
|
Optional[Union[str, List[str]]]
|
Create a split called "test" from these files.
Not used unless referenced by the name "test" in one of the split_name arguments.
Also used for |
None
|
valid_split_files
|
Optional[Union[str, List[str]]]
|
Create a split called "valid" from these files. Not used unless referenced by the name "valid" in one of the split_name arguments. |
None
|
test_split_size
|
float
|
The size of the test split. If test_split_name is None, creates a test split of this size from the training split. |
0.2
|
valid_split_size
|
float
|
The size of the validation split. If valid_split_name is None, creates a validation split of this size from the training split. |
0.1
|
random_seed
|
int
|
The random seed to use for splitting the data. |
42
|
extra_reader_kwargs
|
Optional[dict]
|
Extra kwargs for dataset readers. |
None
|
batch_size
|
int
|
The batch size. |
128
|
shuffle
|
bool
|
Whether to shuffle the data. |
True
|
sampler
|
Optional[Sampler]
|
The sampler to use. |
None
|
num_workers
|
int
|
The number of workers to use for data loading. |
0
|
collate_fn
|
Optional[callable]
|
The function to use for collating data. |
None
|
pin_memory
|
bool
|
Whether to pin memory. |
True
|
persistent_workers
|
bool
|
Whether to use persistent workers. |
False
|
cv_num_folds
|
int
|
The number of cross-validation folds, disables cv when <= 1. |
1
|
cv_test_fold_id
|
int
|
The fold id to use for cross-validation evaluation. |
0
|
cv_enable_val_fold
|
bool
|
Whether to enable a validation fold. |
True
|
cv_replace_val_fold_as_test_fold
|
bool
|
Replace validation fold with test fold. Only used when cv_enable_val_fold is False. |
False
|
cv_fold_id_col
|
Optional[str]
|
The column name containing the fold id from a pre-split dataset. Setting to None to enable automatic splitting. |
None
|
cv_val_offset
|
int
|
The offset applied to cv_test_fold_id to determine val_fold_id. |
1
|
Source code in modelgenerator/data/base.py
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 |
|
setup(stage=None)
Set up the data module. This method should be overridden by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stage
|
Optional[str]
|
training, validation, or test if these need to be setup separately. Defaults to None. |
None
|
Source code in modelgenerator/data/base.py
279 280 281 282 283 284 285 286 287 |
|
Useful Mixins
modelgenerator.data.HFDatasetLoaderMixin
Provides methods for loading datasets using the Huggingface datasets library.
Source code in modelgenerator/data/base.py
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 |
|
modelgenerator.data.KFoldMixin
Provides methods for splitting datasets into k-folds for cross-validation
Source code in modelgenerator/data/base.py
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 |
|
Implementing a DataModule
To transform datasets for task-specific behaviors (e.g. masking for masked language modeling), use torch.utils.data.Dataset
objects to implement the transformation.
Below is an example.
modelgenerator.data.MLMDataModule
Bases: SequenceClassificationDataModule
Data module for continuing pretraining on a masked language modeling task.
Note
Each sample includes a single sequence under key 'sequences' and a single target sequence under key 'target_sequences'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the dataset, can be (1) a local path to a data folder or (2) a Huggingface dataset identifier |
required |
config_name
|
Optional[str]
|
The name of the HF dataset configuration. Affects how the dataset is loaded. |
None
|
masking_rate
|
float
|
The masking rate. Defaults to 0.15. |
0.15
|
x_col
|
str
|
The name of the column containing the sequences. |
'sequence'
|
y_col
|
str | List[str]
|
The name of the column(s) containing the labels. |
'label'
|
extra_cols
|
List[str] | None
|
Additional columns to include in the dataset. |
None
|
extra_col_aliases
|
List[str] | None
|
The name of the columns to use as the alias for the extra columns. |
None
|
class_filter
|
int | List[int] | None
|
Filter the dataset to only include samples with the specified class(es). |
None
|
generate_uid
|
bool
|
Whether to generate a unique ID for each sample. |
False
|
train_split_name
|
Optional[str]
|
The name of the training split. |
'train'
|
test_split_name
|
Optional[str]
|
The name of the test split. Also used for |
'test'
|
valid_split_name
|
Optional[str]
|
The name of the validation split. |
None
|
train_split_files
|
Optional[Union[str, List[str]]]
|
Create a split called "train" from these files. Not used unless referenced by the name "train" in one of the split_name arguments. |
None
|
test_split_files
|
Optional[Union[str, List[str]]]
|
Create a split called "test" from these files.
Not used unless referenced by the name "test" in one of the split_name arguments.
Also used for |
None
|
valid_split_files
|
Optional[Union[str, List[str]]]
|
Create a split called "valid" from these files. Not used unless referenced by the name "valid" in one of the split_name arguments. |
None
|
test_split_size
|
float
|
The size of the test split. If test_split_name is None, creates a test split of this size from the training split. |
0.2
|
valid_split_size
|
float
|
The size of the validation split. If valid_split_name is None, creates a validation split of this size from the training split. |
0.1
|
random_seed
|
int
|
The random seed to use for splitting the data. |
42
|
extra_reader_kwargs
|
Optional[dict]
|
Extra kwargs for dataset readers. |
None
|
batch_size
|
int
|
The batch size. |
128
|
shuffle
|
bool
|
Whether to shuffle the data. |
True
|
sampler
|
Optional[Sampler]
|
The sampler to use. |
None
|
num_workers
|
int
|
The number of workers to use for data loading. |
0
|
collate_fn
|
Optional[callable]
|
The function to use for collating data. |
None
|
pin_memory
|
bool
|
Whether to pin memory. |
True
|
persistent_workers
|
bool
|
Whether to use persistent workers. |
False
|
cv_num_folds
|
int
|
The number of cross-validation folds, disables cv when <= 1. |
1
|
cv_test_fold_id
|
int
|
The fold id to use for cross-validation evaluation. |
0
|
cv_enable_val_fold
|
bool
|
Whether to enable a validation fold. |
True
|
cv_replace_val_fold_as_test_fold
|
bool
|
Replace validation fold with test fold. Only used when cv_enable_val_fold is False. |
False
|
cv_fold_id_col
|
Optional[str]
|
The column name containing the fold id from a pre-split dataset. Setting to None to enable automatic splitting. |
None
|
cv_val_offset
|
int
|
The offset applied to cv_test_fold_id to determine val_fold_id. |
1
|
**kwargs
|
Additional keyword arguments for the parent class. |
{}
|
Source code in modelgenerator/data/data.py
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
|
setup(stage=None)
Set up the data module by loading the whole datasets and splitting them into training, validation, and test sets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stage
|
Optional[str]
|
training, validation, or test if these need to be setup separately. Defaults to None. |
None
|
Source code in modelgenerator/data/data.py
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
|
modelgenerator.data.MLMDataset
Bases: Dataset
Masked Language Modeling Dataset
Note
Each sample includes a single sequence under key 'sequences' and a single target sequence under key 'target_sequences'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The dataset to mask |
required |
masking_rate
|
float
|
The masking rate |
required |
Source code in modelgenerator/data/data.py
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 |
|