from torch_geometric.data import Batch, Data # List of 3 graphs with varying shapes graph_list = [Data(x=torch.randn(5, 10), edge_index=torch.randint(0, 5, (2, 20)))), Data(x=torch.randn(3, 10), edge_index=torch.randint(0, 3, (2, 10)))), Data(x=torch.randn(7, 10), edge_index=torch.randint(0, 7, (2, 40))))] # Batch graphs batch = Batch.from_data_list(graph_list) # Output batch print(batch)
from torch_geometric.data import Batch, Data # List of 3 graphs with varying sizes graph_list = [Data(x=torch.randn(torch.randint(5, 10), 10), edge_index=torch.randint(0, 10, (2, 20)))), Data(x=torch.randn(torch.randint(3, 6), 10), edge_index=torch.randint(0, 6, (2, 10)))), Data(x=torch.randn(torch.randint(7, 12), 10), edge_index=torch.randint(0, 12, (2, 40))))] # Batch graphs batch = Batch.from_data_list(graph_list) # Output batch print(batch)In both examples, we start by creating a list of PyTorch Geometric Data objects representing multiple graphs. We then pass this list to the Batch.from_data_list() method to create the batched graph. Finally, we print the batched graph using the print() statement. Package library: torch_geometric.