---
title: combined_loader
description: Combines different iterables under specific sampling modes.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.utilities.combined_loader
---
# combined_loader {#combined-loader}

Classes

- [`CombinedLoader`](#lightning.pytorch.utilities.combined_loader.CombinedLoader) — Combines different iterables under specific sampling modes.

### `class lightning.pytorch.utilities.combined_loader.CombinedLoader(iterables, mode='min_size')` {#lightning.pytorch.utilities.combined_loader.CombinedLoader}

Bases: [`Iterable`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterable "(in Python v3.15)")

Combines different iterables under specific sampling modes.

Parameters:

*   **iterables** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – the iterable or collection of iterables to sample from.
    
*   **mode** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'min_size'`, `'max_size_cycle'`, `'max_size'`, `'sequential'`\]) –
    
    the mode to use. The following modes are supported:
    
    *   `min_size`: stops after the shortest iterable (the one with the lowest number of items) is done.
        
    *   `max_size_cycle`: stops after the longest iterable (the one with most items) is done, while cycling through the rest of the iterables.
        
    *   `max_size`: stops after the longest iterable (the one with most items) is done, while returning None for the exhausted iterables.
        
    *   `sequential`: completely consumes each iterable sequentially, and returns a triplet `(data, idx, iterable_idx)`
        
    

Examples

```python
>>> from torch.utils.data import DataLoader
>>> iterables = {'a': DataLoader(range(6), batch_size=4),
...              'b': DataLoader(range(15), batch_size=5)}
>>> combined_loader = CombinedLoader(iterables, 'max_size_cycle')
>>> _ = iter(combined_loader)
>>> len(combined_loader)
3
>>> for batch, batch_idx, dataloader_idx in combined_loader:
...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
{'a': tensor([0, 1, 2, 3]), 'b': tensor([0, 1, 2, 3, 4])}, batch_idx=0, dataloader_idx=0
{'a': tensor([4, 5]), 'b': tensor([5, 6, 7, 8, 9])}, batch_idx=1, dataloader_idx=0
{'a': tensor([0, 1, 2, 3]), 'b': tensor([10, 11, 12, 13, 14])}, batch_idx=2, dataloader_idx=0
```

```python
>>> combined_loader = CombinedLoader(iterables, 'max_size')
>>> _ = iter(combined_loader)
>>> len(combined_loader)
3
>>> for batch, batch_idx, dataloader_idx in combined_loader:
...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
{'a': tensor([0, 1, 2, 3]), 'b': tensor([0, 1, 2, 3, 4])}, batch_idx=0, dataloader_idx=0
{'a': tensor([4, 5]), 'b': tensor([5, 6, 7, 8, 9])}, batch_idx=1, dataloader_idx=0
{'a': None, 'b': tensor([10, 11, 12, 13, 14])}, batch_idx=2, dataloader_idx=0
```

```python
>>> combined_loader = CombinedLoader(iterables, 'min_size')
>>> _ = iter(combined_loader)
>>> len(combined_loader)
2
>>> for batch, batch_idx, dataloader_idx in combined_loader:
...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
{'a': tensor([0, 1, 2, 3]), 'b': tensor([0, 1, 2, 3, 4])}, batch_idx=0, dataloader_idx=0
{'a': tensor([4, 5]), 'b': tensor([5, 6, 7, 8, 9])}, batch_idx=1, dataloader_idx=0
```

```python
>>> combined_loader = CombinedLoader(iterables, 'sequential')
>>> _ = iter(combined_loader)
>>> len(combined_loader)
5
>>> for batch, batch_idx, dataloader_idx in combined_loader:
...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
tensor([0, 1, 2, 3]), batch_idx=0, dataloader_idx=0
tensor([4, 5]), batch_idx=1, dataloader_idx=0
tensor([0, 1, 2, 3, 4]), batch_idx=0, dataloader_idx=1
tensor([5, 6, 7, 8, 9]), batch_idx=1, dataloader_idx=1
tensor([10, 11, 12, 13, 14]), batch_idx=2, dataloader_idx=1
```

#### `reset()` {#lightning.pytorch.utilities.combined_loader.CombinedLoader.reset}

Reset the state and shutdown any workers.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `property batch_sampler: Any` {#lightning.pytorch.utilities.combined_loader.CombinedLoader.batch_sampler}

Return a collections of batch samplers extracted from iterables.

#### `property flattened: list[Any]` {#lightning.pytorch.utilities.combined_loader.CombinedLoader.flattened}

Return the flat list of iterables.

#### `property iterables: Any` {#lightning.pytorch.utilities.combined_loader.CombinedLoader.iterables}

Return the original collection of iterables.

#### `property limits: Optional[list[Union[int, float]]]` {#lightning.pytorch.utilities.combined_loader.CombinedLoader.limits}

Optional limits per iterator.

#### `property sampler: Any` {#lightning.pytorch.utilities.combined_loader.CombinedLoader.sampler}

Return a collections of samplers extracted from iterables.

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.6.6/optional-api/api.md)
