ToSparseTensor converts a dense adjacency matrix or edge index into a sparse tensor in PyTorch Geometric. It is useful when large graphs contain many absent edges, because sparse storage can reduce memory use. section 7.5
Use the transformation as a short data-processing pipeline:
- Create or load a graph dataset containing dense adjacency data or an edge index. section 7.5
- Attach ToSparseTensor() as the dataset transform to construct a COO-format sparse adjacency matrix. section 7.5
- Prefer dense storage when few adjacency entries are zero or sparse indexing overhead outweighs memory savings. section 7.5
Campus social network
Represent students as nodes and associations as edges in a multi-campus social graph, then convert its adjacency data to a sparse tensor when most possible associations are absent. section 7.5
torch_geometric.transforms import ToSparseTensor dataset = YourDataset(transform=ToSparseTensor())
View the minimal PyG dataset configuration that applies ToSparseTensor during loading. section 7.5
