Skip to content

Dataloader

Dataloader(
    dataset: Union[Dataset, Sequence[GraphData]],
    batch_size: int = 1,
    shuffle: bool = False,
    drop_last: bool = False,
    follow_batch: Optional[Sequence[str]] = None,
    sampler: Optional[Iterable[int]] = None,
    batch_sampler: Optional[Iterable[Sequence[int]]] = None,
    transform: Optional[Callable[[GraphData], GraphData]] = None,
    collate_fn: Optional[Callable[[list[GraphData]], Any]] = None,
)

Default data loader to batch and iterate over multiple graphs.

Parameters:

Name Type Description Default
dataset Union[Dataset, Sequence[GraphData]]

Dataset or list of GraphData to batch and iterate over

required
batch_size int

Number of graphs to load per batch. Defaults to 1

1
shuffle bool

Whether to reshuffle the order of graphs at the start of each epoch. Defaults to False

False
drop_last bool

Whether to drop the last batch if it holds fewer than batch_size graphs. Defaults to False

False
follow_batch Optional[Sequence[str]]

Names of attributes for which to create a batch vector <name>_batch assigning each of their elements (along the concatenation dimension) to its graph, like PyG's x_batch. E.g. follow_batch=["node_features"] adds batch.node_features_batch. Defaults to None

None
sampler Optional[Iterable[int]]

Iterable yielding the dataset indices to draw for an epoch, e.g. an ImbalancedSampler. Mutually exclusive with shuffle and batch_sampler. Defaults to None

None
batch_sampler Optional[Iterable[Sequence[int]]]

Iterable yielding lists of dataset indices, one list per batch, e.g. a DynamicBatchSampler. Mutually exclusive with batch_size, shuffle, sampler and drop_last. Defaults to None

None
transform Optional[Callable[[GraphData], GraphData]]

Function applied to every graph before collating. Defaults to None

None
collate_fn Optional[Callable[[list[GraphData]], Any]]

Function turning a list of graphs into a batch. Defaults to batch(), which builds a GraphDataBatch. follow_batch requires the default.

None

Example:

from mlx_graphs.datasets import TUDataset
from mlx_graphs.loaders import Dataloader

dataset = TUDataset("MUTAG")
loader = Dataloader(
    dataset,
    batch_size=32,
    shuffle=True,
    drop_last=True,
    follow_batch=["node_features"],
)
for b in loader:
    b.node_features_batch  # graph index of every node
Source code in mlx_graphs/loaders/dataloaders.py
def __init__(
    self,
    dataset: Union[Dataset, Sequence[GraphData]],
    batch_size: int = 1,
    shuffle: bool = False,
    drop_last: bool = False,
    follow_batch: Optional[Sequence[str]] = None,
    sampler: Optional[Iterable[int]] = None,
    batch_sampler: Optional[Iterable[Sequence[int]]] = None,
    transform: Optional[Callable[[GraphData], GraphData]] = None,
    collate_fn: Optional[Callable[[list[GraphData]], Any]] = None,
):
    if batch_size < 1:
        raise ValueError(f"`batch_size` must be positive (got {batch_size}).")
    if sampler is not None and shuffle:
        raise ValueError("`sampler` and `shuffle` are mutually exclusive.")
    if batch_sampler is not None and (
        batch_size != 1 or shuffle or sampler is not None or drop_last
    ):
        raise ValueError(
            "`batch_sampler` is mutually exclusive with `batch_size`, "
            "`shuffle`, `sampler` and `drop_last`."
        )
    if follow_batch and collate_fn is not None:
        raise ValueError("`follow_batch` requires the default `collate_fn`.")

    self.dataset = dataset
    self.batch_size = batch_size
    self.shuffle = shuffle
    self.drop_last = drop_last
    self.follow_batch = list(follow_batch or [])
    self.sampler = sampler
    self.batch_sampler = batch_sampler
    self.transform = transform
    self.collate_fn = collate_fn

    self._indices = list(range(len(dataset)))
    self._batches: Optional[list[list[int]]] = None
    self._current_index = 0

from mlx_graphs.loaders import Dataloader · mlx_graphs/loaders/dataloaders.py