Conditionally have a single or multiple data Loaders. #13241
-
Hi, I have a use case where I need to have a single validation dataloader or multiple validation dataloader based on a flag. My data module looks something like class CodeXGlueDataModule(pl.LightningDataModule):
def __init__(self, args):
...
def train_dataloader(self):
...
def val_dataloader(self):
loader1 = ...
if flag:
loader2 = ...
loaders = [dataloader_ppl, dataloader_bleu]
return loaders
loaders = [dataloader_ppl, ]
return loaders and my lightening model looks like def validation_step(self, batch, batch_idx, dataloader_idx):
if dataloader_idx == 0:
...
return {"x":x}
elif dataloader_idx == 1:
...
return {"y":y} When I run the code with flag = True, it works fines but when I run it with flag = False, I get an error saying
I understand that a single dataloader is not treated as a list and hence there is no dataloader_idx argument for the validation_step function but is there something I can do to make this work. Thanks in Advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@prateeky2806 Making it an optional keyword argument will avoid the error? -def validation_step(self, batch, batch_idx, dataloader_idx):
- if dataloader_idx == 0:
+def validation_step(self, batch, batch_idx, dataloader_idx=None):
+ if dataloader_idx == 0 or dataloader_idx is None:
...
elif dataloader_idx == 1:
... |
Beta Was this translation helpful? Give feedback.
@prateeky2806 Making it an optional keyword argument will avoid the error?