Repository navigation
Best practice for multiple validation datasets #21288
Unanswered
ginfantem
asked this question in
Lightning Trainer API: Trainer, LightningModule, LightningDataModule
Replies: 1 comment
|
You can use Lightning's Approach 1: Use a callback to swap val dataloaders per epochclass RotatingValCallback(pl.Callback):
def on_validation_epoch_start(self, trainer, pl_module):
epoch = trainer.current_epoch
# Rotate through your val datasets
active_idx = epoch % len(trainer.val_dataloaders)
# Store which one is active so validation_step can check
pl_module._active_val_idx = active_idxThen in your def validation_step(self, batch, batch_idx, dataloader_idx=0):
if dataloader_idx != self._active_val_idx:
return None # Skip — Lightning handles None returns gracefully
# ... actual validation logic ...This still iterates all dataloaders but the compute inside Approach 2: Dynamically set val_dataloaders (avoids iterating skipped ones)Override class MyModel(pl.LightningModule):
def __init__(self, val_datasets):
super().__init__()
self.val_datasets = val_datasets # OrderedDict
def val_dataloader(self):
# Return only the dataset for this epoch
idx = self.current_epoch % len(self.val_datasets)
key = list(self.val_datasets.keys())[idx]
return DataLoader(self.val_datasets[key], ...)
def on_validation_epoch_start(self):
idx = self.current_epoch % len(self.val_datasets)
key = list(self.val_datasets.keys())[idx]
self.log("val_dataset", float(idx)) # track which oneThis is more efficient because Lightning only iterates the single active dataloader. Approach 3: Use
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hello everyone, I have multiple validation datasets that I pass to the trainer as an OrderedDict. At each validation epoch I might want to process one of them exclusively, in an interleaving fashion. However, even I can conditionally process each dataset via the dataloader_idx argument, validation_step is run also for all the batches for the rest of dataloaders (even though inside validation_step the flow discards them due to the if statement). Is there a best practice around this, so this does not slow down the training process?

All reactions