PyTorch Geometric Temporal

repository·master·Indexed 25 days ago

https://github.com/benedekrozemberczki/pytorch_geometric_temporal

A temporal extension of PyTorch Geometric for dynamic and temporal graph neural networks. It provides spatio-temporal regression methods, recurrent and attention-based graph convolutional layers (such as GConvGRU and STGCN), and tools for handling temporal graph datasets. Key features include index-batching for memory efficiency, Dask-DDP support for distributed training, and specialized temporal signal iterators for both homogeneous and heterogeneous graphs.

Tokens
9.7K
Snippets
24
Records
45
Agent score
84%

What's inside PyTorch Geometric Temporal

  1. Overview of PyTorch Geometric Temporal

    master

    PyTorch Geometric Temporal is a temporal (dynamic) extension library for PyTorch Geometric. It provides various dynamic and temporal geometric deep learning, embedding, and spatio-temporal regression methods.

    Key features include:

    • Index-batching: A technique to improve spatiotemporal memory efficiency without impacting accuracy.
    • Dask-DDP support: Enables memory-efficient distributed data parallel training when combined with index-batching.
    • PyTorch Lightning integration: Supports training on CPUs and single/multiple GPUs out-of-the-box.
    • Dataset Utilities: Includes dataset loaders, train-test splitters, and temporal snapshot iterators for dynamic and temporal graphs.
    • Benchmark Datasets: Includes datasets from epidemiological forecasting, sharing economy, energy production, and web traffic management domains.
  2. Understand Temporal Signal Iterators

    master

    PyTorch Geometric Temporal provides data iterators for spatio-temporal datasets. These iterators yield temporal snapshots (PyTorch Data objects) or temporal batch snapshots (PyTorch Batch objects) depending on whether they are designed for single graphs or batched graphs.

    Non-Batched Iterators

    Use these when working with individual temporal snapshots:

    • StaticGraphTemporalSignal: Temporal signals on a static graph.
    • DynamicGraphTemporalSignal: Temporal signals on a dynamic graph.
    • DynamicGraphStaticSignal: Static signals on a dynamic graph.

    Batched Iterators

    Use these when working with batches of temporal snapshots (using the block diagonal batching trick):

    • StaticGraphTemporalSignalBatch: Temporal signals on a batch of static graphs.
    • DynamicGraphTemporalSignalBatch: Temporal signals on a batch of dynamic graphs.
    • DynamicGraphStaticSignalBatch: Static signals on a batch of dynamic graphs.
  3. Available PyTorch Geometric Temporal Datasets

    master

    The torch_geometric_temporal.dataset module provides several built-in temporal graph datasets. These datasets are designed for temporal graph learning tasks and can be instantiated directly from the library.

    Available datasets include:

    • Epidemiological/Social Data: chickenpox, encovid, twitter_tennis
    • Traffic/Sensor Data: metr_la, pems_bay, pemsAllLA, pems, montevideo_bus
    • Energy/Windmill Data: windmilllarge, windmillmedium, windsmalls
    • Other: pedalme, wikimath, mtm
  4. Use Temporal Signal Iterators for Homogeneous Graphs

    master

    The torch_geometric_temporal.signal module provides several iterator types for homogeneous graphs, categorized by whether the graph structure is static or dynamic and whether the signal is temporal or static:

    • Static Graph Temporal Signal: Use static_graph_temporal_signal when the graph structure (edges) remains constant over time, but node/edge features change.
    • Dynamic Graph Temporal Signal: Use dynamic_graph_temporal_signal when both the graph structure and features change over time.
    • Dynamic Graph Static Signal: Use dynamic_graph_static_signal for scenarios where the graph structure is dynamic but the signal itself is static.
  5. Implement Index-Batching for ST-GNN training

    master

    Index-batching reduces memory costs for training Spatio-Temporal Graph Neural Networks (ST-GNNs) without impacting accuracy. It enables training on large datasets like the full PeMS dataset without graph partitioning.

    To use index-batching, use the loader.get_index_dataset(batch_size=batch_size) method to retrieve the dataset components. The training loop requires passing edges and edge_weights directly to the model alongside the temporal signal batch.

    train_dataloader, _, _, edges, edge_weights, means, stds = loader.get_index_dataset(batch_size=batch_size)
    
    for batch in train_dataloader:
        X_batch, y_batch = batch
    
        # Forward pass
        outputs = model(X_batch, edges, edge_weights) 
    
        # Calculate loss 
        loss = masked_mae_loss((outputs * std) + mean, (y_batch * std) + mean)
    
        # Backward pass
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
  6. Install PyTorch Geometric Temporal

    master

    Install the core library using pip. Note that you must have pytorch and pytorch-geometric installed beforehand.

    To install with index-batching support (improves spatiotemporal memory efficiency):

    pip install torch-geometric-temporal[index]

    To install with both index-batching and Dask-DDP support (for memory-efficient distributed data parallel training):

    pip install torch-geometric-temporal[ddp]
    pip install torch-geometric-temporal