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
|
The name of the HF dataset configuration. Affects how the dataset is loaded. |
None
|
train_split_name
|
Optional
|
The name of the training split. |
'train'
|
test_split_name
|
Optional
|
The name of the test split. Also used for |
'test'
|
valid_split_name
|
Optional
|
The name of the validation split. |
None
|
train_split_files
|
Union
|
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
|
Union
|
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
|
Union
|
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
|
Extra kwargs for dataset readers. |
None
|
batch_size
|
int
|
The batch size. |
128
|
shuffle
|
bool
|
Whether to shuffle the data. |
True
|
sampler
|
Optional
|
The sampler to use. |
None
|
num_workers
|
int
|
The number of workers to use for data loading. |
0
|
collate_fn
|
Optional
|
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
|
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
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 | |
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
275 276 277 278 279 280 281 282 283 | |
Useful Mixins
modelgenerator.data.HFDatasetLoaderMixin
Provides methods for loading datasets using the Huggingface datasets library.
Source code in modelgenerator/data/base.py
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 | |
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 | |
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
|
The name of the HF dataset configuration. Affects how the dataset is loaded. |
None
|
x_col
|
str
|
The name of the column containing the sequences. Defaults to "sequences". |
'sequences'
|
y_col
|
Union
|
The name of the column(s) containing the labels. |
'labels'
|
masking_rate
|
float
|
The masking rate. Defaults to 0.15. |
0.15
|
rename_cols
|
dict[str, str] | None
|
A dictionary mapping the original column names to the new column names. |
None
|
class_filter
|
Union
|
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
|
The name of the training split. |
'train'
|
test_split_name
|
Optional
|
The name of the test split. Also used for |
'test'
|
valid_split_name
|
Optional
|
The name of the validation split. |
None
|
train_split_files
|
Union
|
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
|
Union
|
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
|
Union
|
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
|
Extra kwargs for dataset readers. |
None
|
batch_size
|
int
|
The batch size. |
128
|
shuffle
|
bool
|
Whether to shuffle the data. |
True
|
sampler
|
Optional
|
The sampler to use. |
None
|
num_workers
|
int
|
The number of workers to use for data loading. |
0
|
collate_fn
|
Optional
|
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
|
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/data.py
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
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
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
modelgenerator.data.MLMDataset
Bases: Dataset
Masked Language Modeling Dataset
Note
Each sample includes a single sequence under sequence_col 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 |
sequence_col
|
str
|
The name of the column containing sequences |
'sequences'
|
Source code in modelgenerator/data/data.py
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 | |