Answers.Body: <p>For efficiency reasons, it is the standard for graph learning libraries to use the format <code>[n_batches x num_nodes, node_feature_dim]</code>, instead of having batches in an extra batch dimension. Specifically, the different graphs of a batch are transformed into a single large graph with <strong>disjoint subgraphs</strong>. However, because there is no connection between any two graphs of the batch, neighborhood operations like <em>message-passing</em> will not leak information into different graphs of the batch.
  This is not true for global graph operations, such as pooling. Such operations require a <code>batch-index</code> as an additional input. The batch-index maps each node onto its batch index. In the following, <span class="math-container">$f$</span> is a global graph operation (set operation), <span class="math-container">$N$</span> is note number of graphs <span class="math-container">$\mathcal{G}$</span> in your batch.</p>
  <p><span class="math-container">$$
  X = \{ f\left(\mathcal{G}_i\right) : \forall i \in [0,..,N] \}
  $$</span></p>
  <p>Each result will only contain information from within the respective batch. In the <code>Batch</code> class that you are using, according to the documentation, there is a <code>batch</code> field that tells you which nodes belong to which graph:</p>
  <blockquote>
  <p><a href="https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.data.Batch.html#torch_geometric.data.Batch" rel="nofollow noreferrer">In addition, single graphs can be identified via the assignment vector batch, which maps each node to its respective graph identifier.</a></p>
  </blockquote>
  <p>For more on graph batching in general <a href="https://colab.research.google.com/drive/1I8a0DfQ3fI7Njc62__mVXUlcAleUclnb?usp=sharing#scrollTo=0gZ-l0npPIca" rel="nofollow noreferrer">here is a colab notebook</a> that contains some nice demo code.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-10T09:03:44.020Z
Answers.LastActivityDate: 2024-03-10T09:03:44.020Z
Answers.UserDisplayName: Chillston
Body: <p>I have a batched input to GATv2Conv with node matrix of shape [batch_sz , num_nodes , node_feature_dim] , but the GATv2Conv accepts input of dim 2  ,searching through the internet , I found some solution ... (not the one I want)</p>
  <pre><code>data_list = [Data(x= torch.squeeze(torch.index_select(x, dim= 0, index = torch.tensor([idx]))) , 
                                edge_index= self.edge_indices ,
                                edge_attr= torch.squeeze(torch.index_select(adj_mats[i], dim= 0, index = torch.tensor([idx])))) 
                           for idx in range(self.batch_sz)] 
              batch  = Batch.from_data_list(data_list)
  </code></pre>
  <p>But using above solution , the distinction between graphs got lost , becuase :
  <code>batch.x.shape</code> gave <code>[batch_sz * num_nodes , node_feature_dim]</code>...</p>
  <p>It simply put all nodes of all graphs in one single graphs.. Now there is shared calculations between different graphs , which is strictly undesirable... As when applying some graph pooling layer, I don't know which nodes belonged to which graph....</p>
  <p>Pls suggest some fix for this issue ...</p>
  <p>Thanks in advance</p>
  
Tags: <deep-learning><graph-neural-networks><geometric-deep-learning><gnn>
Title: How to pass batch of Graphs in GATv2Conv?
UserDisplayName: Sarvagya Porwal
ThreadText: # How to pass batch of Graphs in GATv2Conv?
  
  (Asked by: username_0 on 2024-03-09)
  
  I have a batched input to GATv2Conv with node matrix of shape [batch_sz , num_nodes , node_feature_dim] , but the GATv2Conv accepts input of dim 2  ,searching through the internet , I found some solution ... (not the one I want)
  data_list = [Data(x= torch.squeeze(torch.index_select(x, dim= 0, index = torch.tensor([idx]))) , 
                                edge_index= self.edge_indices ,
                                edge_attr= torch.squeeze(torch.index_select(adj_mats[i], dim= 0, index = torch.tensor([idx])))) 
                           for idx in range(self.batch_sz)] 
              batch  = Batch.from_data_list(data_list)
  
  But using above solution , the distinction between graphs got lost , becuase :
  batch.x.shape gave [batch_sz * num_nodes , node_feature_dim]...
  It simply put all nodes of all graphs in one single graphs.. Now there is shared calculations between different graphs , which is strictly undesirable... As when applying some graph pooling layer, I don't know which nodes belonged to which graph....
  Pls suggest some fix for this issue ...
  Thanks in advance
  
  **Tags:** deep-learning, graph-neural-networks, geometric-deep-learning, gnn
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-03-10. Score: 2
  
  For efficiency reasons, it is the standard for graph learning libraries to use the format `[n_batches x num_nodes, node_feature_dim]`, instead of having batches in an extra batch dimension. Specifically, the different graphs of a batch are transformed into a single large graph with **disjoint subgraphs**. However, because there is no connection between any two graphs of the batch, neighborhood operations like *message-passing* will not leak information into different graphs of the batch.
  This is not true for global graph operations, such as pooling. Such operations require a `batch-index` as an additional input. The batch-index maps each node onto its batch index. In the following, $f$ is a global graph operation (set operation), $N$ is note number of graphs $\mathcal{G}$ in your batch.
  
  $$
  X = \{ f\left(\mathcal{G}\_i\right) : \forall i \in [0,..,N] \}
  $$
  
  Each result will only contain information from within the respective batch. In the `Batch` class that you are using, according to the documentation, there is a `batch` field that tells you which nodes belong to which graph:
  
  > [In addition, single graphs can be identified via the assignment vector batch, which maps each node to its respective graph identifier.](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.data.Batch.html#torch_geometric.data.Batch)
  
  For more on graph batching in general [here is a colab notebook](https://colab.research.google.com/drive/1I8a0DfQ3fI7Njc62__mVXUlcAleUclnb?usp=sharing#scrollTo=0gZ-l0npPIca) that contains some nice demo code.
  
  
  ---
  
  