[doc] Update Dict Train Loader doc. (#6579)

* update doc

* update example
This commit is contained in:
thomas chaton 2021-03-18 17:14:38 +00:00 committed by GitHub
parent 9e35f979ea
commit 8853a36d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 8 deletions

View File

@ -89,7 +89,24 @@ For more details please have a look at :paramref:`~pytorch_lightning.trainer.tra
return loaders return loaders
Furthermore, Lightning also supports that nested lists and dicts (or a combination) can Furthermore, Lightning also supports that nested lists and dicts (or a combination) can
be returned be returned.
.. testcode::
class LitModel(LightningModule):
def train_dataloader(self):
loader_a = torch.utils.data.DataLoader(range(8), batch_size=4)
loader_b = torch.utils.data.DataLoader(range(16), batch_size=2)
return {'a': loader_a, 'b': loader_b}
def training_step(self, batch, batch_idx):
# access a dictionnary with a batch from each dataloader
batch_a = batch["a"]
batch_b = batch["b"]
.. testcode:: .. testcode::
@ -103,12 +120,29 @@ be returned
loader_c = torch.utils.data.DataLoader(range(64), batch_size=4) loader_c = torch.utils.data.DataLoader(range(64), batch_size=4)
# pass loaders as a nested dict. This will create batches like this: # pass loaders as a nested dict. This will create batches like this:
# {'loader_a_b': {'a': batch from loader a, 'b': batch from loader b}, loaders = {
# 'loader_c_d': {'c': batch from loader c, 'd': batch from loader d}} 'loaders_a_b': {
loaders = {'loaders_a_b': {'a': loader_a, 'b': loader_b}, 'a': loader_a,
'loaders_c_d': {'c': loader_c, 'd': loader_d}} 'b': loader_b
},
'loaders_c_d': {
'c': loader_c,
'd': loader_d
}
}
return loaders return loaders
def training_step(self, batch, batch_idx):
# access the data
batch_a_b = batch["loaders_a_b"]
batch_c_d = batch["loaders_c_d"]
batch_a = batch_a_b["a"]
batch_b = batch_a_b["a"]
batch_c = batch_c_d["c"]
batch_d = batch_c_d["d"]
---------- ----------
Test/Val dataloaders Test/Val dataloaders