peal.data.dataloaders.DataloaderMixer¶
- class peal.data.dataloaders.DataloaderMixer[source]¶
Bases:
DataLoaderDataloader that samples batches from several dataloaders.
Starts with one dataloader;
appendadds more (e.g. counterfactual datasets during CFKD) with priorities proportional to dataset size. In the default mode everysample()picks one dataloader by multinomial draw overpriorities; withtrain_config.concatenate_batchesone batch from each dataloader is concatenated instead (used for class balancing). Exhausted iterators are restarted transparently, and one epoch istrain_config.steps_per_epochbatches. Only the attribute interface ofDataLoaderis reused;DataLoader.__init__is not called.- Parameters:
train_config (
TrainingConfig) – Providessteps_per_epochand optionallyconcatenate_batches.initial_dataloader (
DataLoader) – First member; itsbatch_sizeanddatasetare adopted.return_src (
bool, optional) – IfTrue(and batches are not concatenated)samplereturns(batch, dataloader_index).
- Variables:
dataloaders (
listofDataLoader)iterators (
list) – One live iterator per dataloader.priorities (
numpy.ndarrayorNone) – Sampling probabilities,Nonewhile only one dataloader exists.class_balancing_enabled (hints_enabled,)
Methods
Add another dataloader and recompute the sampling priorities.
Undo
enable_class_balancing.Disable hint outputs on all member datasets and reset the iterators.
Stop member datasets returning sample indices and reset the iterators.
Replace every plain member dataloader by a per-class concatenating mixer.
Enable hint outputs on all member datasets and reset the iterators.
Make member datasets return sample indices and reset the iterators.
Recursively remove dataloaders with zero-length datasets.
Rebuild every member dataloader and its iterator.
Draw one batch.
Re-point
self.datasetat the first member's dataset (arg ignored).Attributes
Whether
samplealso returns the source index (off when concatenating).- __init__(train_config, initial_dataloader, return_src=False)[source]¶
Register the first dataloader (
DataLoader.__init__is skipped).
- property return_src¶
Whether
samplealso returns the source index (off when concatenating).
- append(dataloader, priority=1, weight_added_dataloader=None)[source]¶
Add another dataloader and recompute the sampling priorities.
- Parameters:
dataloader (
DataLoader) – Dataloader to add.priority (
float, optional) – Multiplier on the new dataloader’s dataset size before the priorities are normalized. Defaults to 1.weight_added_dataloader (
float, optional) – If given, priorities become[1 - w, w]regardless of sizes (only meaningful with exactly two dataloaders).
Notes
With
train_config.concatenate_batchesall members are reset to half the batch size so concatenated batches keep the original size.
- sample()[source]¶
Draw one batch.
In priority mode one dataloader is chosen by multinomial draw and its next batch returned; in concatenation mode the next batch of every dataloader is fetched and the tensors (also inside nested lists/tuples) are concatenated along dim 0. Exhausted iterators are restarted once; a dataloader that is still empty afterwards raises
StopIteration.- Returns:
The batch as produced by the member dataloader(s), or
(batch, source_index)whenreturn_srcis set.- Return type:
- reset(batch_size=None)[source]¶
Rebuild every member dataloader and its iterator.
- Parameters:
batch_size (
int, optional) – New batch size for the rebuiltDataLoaderobjects; nested mixers are reset recursively with the same value.
- remove_empty_dataloaders()[source]¶
Recursively remove dataloaders with zero-length datasets.
This method iterates through all nested dataloaders and removes any that have no data (len(dataset) == 0), including nested DataloaderMixers that become empty after recursive cleaning.
- enable_class_balancing()[source]¶
Replace every plain member dataloader by a per-class concatenating mixer.
For each class
iindataset.output_sizea deep copy of the dataloader restricted to that class is created; the copies are wrapped in a nestedDataloaderMixerwithconcatenate_batchesandsteps_per_epoch = 200so each batch holds an equal share of every class. No-op if already enabled.