Skip to content

TemporalDataLoader

TemporalDataLoader(
    data: TemporalData,
    batch_size: int = 1,
    neg_sampling_ratio: float = 0.0,
    drop_last: bool = False,
)

Iterates over the events of a TemporalData in their stored (time) order, merging batch_size successive events into a mini-batch, as PyG's torch_geometric.loader.TemporalDataLoader.

Every mini-batch is a TemporalData with an extra n_id attribute: the sorted unique nodes involved in the batch (sources, destinations and, if any, negative destinations).

With neg_sampling_ratio > 0, every mini-batch also gets round(neg_sampling_ratio * batch_size) negative destinations neg_dst, drawn uniformly (with mx.random, so reproducible with mx.random.seed) between the smallest and the largest destination node of data.

Parameters:

Name Type Description Default
data TemporalData

The events to iterate over

required
batch_size int

Number of events per mini-batch. Default: 1

1
neg_sampling_ratio float

Ratio of sampled negative destinations to positive destinations. Default: 0.0

0.0
drop_last bool

Whether to drop the last mini-batch if it has fewer than batch_size events. Default: False

False

Example:

from mlx_graphs.datasets import JODIEDataset
from mlx_graphs.loaders import TemporalDataLoader

data = JODIEDataset("wikipedia")[0]
train_data, val_data, test_data = data.train_val_test_split()
loader = TemporalDataLoader(train_data, batch_size=200, neg_sampling_ratio=1.0)
for batch in loader:
    batch.src, batch.dst, batch.neg_dst, batch.n_id
Source code in mlx_graphs/loaders/temporal_dataloader.py
def __init__(
    self,
    data: TemporalData,
    batch_size: int = 1,
    neg_sampling_ratio: float = 0.0,
    drop_last: bool = False,
):
    if batch_size < 1:
        raise ValueError(f"`batch_size` must be positive (got {batch_size}).")
    self.data = data
    self.batch_size = batch_size
    self.events_per_batch = batch_size
    self.neg_sampling_ratio = neg_sampling_ratio
    self.drop_last = drop_last

    if neg_sampling_ratio > 0:
        self.min_dst = int(data.dst.min().item())
        self.max_dst = int(data.dst.max().item())

    num_events = len(data)
    if drop_last and num_events % batch_size != 0:
        self._starts = range(0, num_events - batch_size, batch_size)
    else:
        self._starts = range(0, num_events, batch_size)

from mlx_graphs.loaders import TemporalDataLoader · mlx_graphs/loaders/temporal_dataloader.py