Skip to content

DynamicBatchSampler

DynamicBatchSampler(
    dataset: Union[Dataset, Sequence[GraphData]],
    max_num: int,
    mode: str = "node",
    shuffle: bool = False,
    skip_too_big: bool = False,
    num_steps: Optional[int] = None,
)

Batch sampler grouping graphs into batches with at most max_num nodes (or edges) in total, so that batches use a bounded amount of memory.

Pass it as batch_sampler to Dataloader. A graph larger than max_num on its own is either yielded alone in its batch, or skipped if skip_too_big is set.

Parameters:

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

A Dataset or list of GraphData.

required
max_num int

Maximum number of nodes (or edges) per batch.

required
mode str

"node" or "edge": what max_num counts. Defaults to "node"

'node'
shuffle bool

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

False
skip_too_big bool

Whether to skip graphs larger than max_num. Defaults to False

False
num_steps Optional[int]

Maximum number of batches per epoch. Defaults to None (all graphs are used). The length of the sampler is only defined when it is set.

None

Example:

from mlx_graphs.loaders import Dataloader, DynamicBatchSampler

sampler = DynamicBatchSampler(dataset, max_num=1000, mode="node")
loader = Dataloader(dataset, batch_sampler=sampler)
Source code in mlx_graphs/loaders/samplers.py
def __init__(
    self,
    dataset: Union[Dataset, Sequence[GraphData]],
    max_num: int,
    mode: str = "node",
    shuffle: bool = False,
    skip_too_big: bool = False,
    num_steps: Optional[int] = None,
):
    if max_num <= 0:
        raise ValueError(
            f"`max_num` should be a positive integer value (got {max_num})"
        )
    if mode not in ("node", "edge"):
        raise ValueError(
            f"`mode` choice should be either 'node' or 'edge' (got '{mode}')"
        )
    self.dataset = dataset
    self.max_num = max_num
    self.mode = mode
    self.shuffle = shuffle
    self.skip_too_big = skip_too_big
    self.num_steps = num_steps
    self._sizes: Optional[np.ndarray] = None

from mlx_graphs.loaders import DynamicBatchSampler · mlx_graphs/loaders/samplers.py