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 one of: 1. a local path to a data folder 2. a Huggingface dataset identifier |
required |
config_name
|
str
|
Defining the name of the dataset configuration. it affects how the dataset is loaded. |
None
|
train_split_name
|
str
|
The name of the training split. Defaults to "train". |
'train'
|
test_split_name
|
str
|
The name of the test split. Defaults to "test". |
'test'
|
valid_split_name
|
str
|
The name of the validation split. Defaults to None. |
None
|
train_split_files
|
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
|
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. |
None
|
valid_split_files
|
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. Defaults to 42. |
42
|
batch_size
|
int
|
The batch size. Defaults to 128. |
128
|
shuffle
|
bool
|
Whether to shuffle the data. Defaults to True. |
True
|
sampler
|
Optional[Sampler]
|
The sampler to use. Defaults to None. |
None
|
num_workers
|
int
|
The number of workers to use for data loading. Defaults to 0. |
0
|
collate_fn
|
Optional[callable]
|
The function to use for collating data. Defaults to None. |
None
|
pin_memory
|
bool
|
Whether to pin memory. Defaults to True. |
True
|
persistent_workers
|
bool
|
Whether to use persistent workers. Defaults to False. |
False
|
cv_num_folds
|
int
|
The number of cross-validation folds, disables cv when <= 1. Defaults to 1. |
1
|
cv_test_fold_id
|
int
|
The fold id to use for cross-validation evaluation. Defaults to 0. |
0
|
cv_enable_val_fold
|
bool
|
Whether to enable a validation fold. Defaults to True. |
True
|
cv_fold_id_col
|
Optional[str]
|
The column name containing the fold id from a pre-split dataset. Set to None to enable automatic splitting. Defaults to None. |
None
|
cv_val_offset
|
int
|
the offset applied to cv_test_fold_id to determin val_fold_id |
1
|
Source code in modelgenerator/data/base.py
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 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 |
|
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
264 265 266 267 268 269 270 271 272 |
|
Useful Mixins
modelgenerator.data.HFDatasetLoaderMixin
Provides methods for loading datasets using the Huggingface datasets library.
Source code in modelgenerator/data/base.py
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 |
|
modelgenerator.data.KFoldMixin
Provides methods for splitting datasets into k-folds for cross-validation
Source code in modelgenerator/data/base.py
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 |
|
generate_kfold_split(num_samples, num_folds, shuffle=True)
Randomly split n_samples into n_splits folds and return list of fold_idx
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_samples
|
int
|
Number of samples in the data. |
required |
num_folds
|
Optional[int]
|
Number of folds for cross validation, must be > 1. Defaults to 10. |
required |
shuffle
|
Optional[bool]
|
Whether to shuffle the data before splitting into batches. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
list of list containing indices for each fold |
Source code in modelgenerator/data/base.py
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 |
|
get_split_by_fold_id(train_dataset, val_dataset, test_dataset, fold_id, val_idx_offset=1)
Split the dataset into training, validation, and test sets based on the fold id for test
Source code in modelgenerator/data/base.py
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 |
|
read_kfold_split(dataset)
Read the fold ids from a pre-split dataset and return list of fold_idx
Source code in modelgenerator/data/base.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
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. Inherits from SequenceClassificationDataModule.
Note
Each sample includes a single sequence under key 'sequences' and a single target sequence under key 'target_sequences'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masking_rate
|
float
|
The masking rate. Defaults to 0.15. |
0.15
|
Source code in modelgenerator/data/data.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 |
|
setup(stage=None)
Source code in modelgenerator/data/data.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 |
|
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
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 |
|
get_masked_sample(seq_target, masking_rate)
Mask a sequence with a given masking rate
Source code in modelgenerator/data/data.py
193 194 195 196 197 198 199 200 201 202 203 204 |
|