Graph Readout Functions
Relevant source files
The following files were used as context for generating this wiki page:
Graph Readout Functions are essential components in graph neural network architectures that aggregate node-level representations into a single fixed-size vector representing the entire graph. In DGL-LifeSci, these functions enable graph-level tasks such as molecular property prediction and generative modeling by summarizing information from all atoms (nodes) within a molecular structure.
These functions handle varied graph sizes by employing aggregation techniques like summation, averaging, and maximum pooling, often combined with learnable layers to capture complex structural distributions.
Core Readout Architectures
Weighted Sum and Max
The WeightedSumAndMax module performs a dual-aggregation strategy. It calculates both a weighted sum and a maximum pooling of node features, then concatenates them. The weighted sum is often used to calculate attention-like importance for nodes during the aggregation process.
The diagram shows the parallel aggregation paths that result in a doubled feature dimension for the final graph representation.
Sources: python/dgllife/model/readout/weighted_sum_and_max.py:12-25, tests/model/test_readout.py:86-94
MLP Node Readout
MLPNodeReadout applies a Multi-Layer Perceptron (MLP) to node features before performing a global pooling operation. It supports multiple aggregation modes:
- Sum: Total sum of transformed node features.
- Mean: Average of transformed node features.
- Max: Element-wise maximum across transformed node features.
Sources: python/dgllife/model/readout/mlp_readout.py:11-30, tests/model/test_readout.py:46-72
Weave Readout (WeaveGather)
The Weave architecture uses a specialized WeaveGather module. It can optionally expand node features using a Gaussian histogram before aggregation. This expansion uses multiple normal distributions with predefined means and standard deviations to capture the distribution of node features more effectively.
This flow illustrates the conditional feature expansion logic within the Weave readout mechanism.
Sources: python/dgllife/model/readout/weave_readout.py:23-55, python/dgllife/model/readout/weave_readout.py:104-123
Readout for Generative Models
In the Deep Generative Models of Graphs (DGMG), the readout is represented by the GraphEmbed class. This module uses a gating mechanism to determine the contribution of each node to the global graph representation.
Gated Graph Embedding
The GraphEmbed module computes:
- Node Gating: A sigmoid-activated linear layer that produces a score between 0 and 1 for each node.
- Node Transformation: A linear transformation of the node hidden state.
- Aggregation: The element-wise product of the gating score and the transformed features, summed across all nodes.
Sources: python/dgllife/model/model_zoo/dgmg.py:180-210
Comparison of Readout Components
| Readout Module | Primary Mechanism | Feature Expansion | Key Parameters |
|---|---|---|---|
WeightedSumAndMax | Concatenated Sum/Max | None | in_feats |
MLPNodeReadout | MLP + Sum/Mean/Max | MLP Transformation | node_feats, graph_feats, mode |
WeaveGather | Gaussian Hist + Sum | Gaussian Membership | gaussian_expand, gaussian_memberships |
GraphEmbed (DGMG) | Sigmoid Gating + Sum | None | node_hidden_size |
Sources: python/dgllife/model/readout/mlp_readout.py:15-35, python/dgllife/model/readout/weave_readout.py:35-50, python/dgllife/model/model_zoo/dgmg.py:188-198
Implementation Details
Gaussian Histogram Construction
In WeaveGather, the Gaussian histogram is constructed by calculating the log probability of node features against a set of normal distributions. These probabilities are normalized across distributions using a softmax-like approach (log-subtraction and exponentiation) to ensure they sum to 1.
# Simplified Gaussian Histogram Logic
log_probs = [gaussian_dists[i].log_prob(node_feats) - max_log_probs[i]
for i in range(len(self.means))]
probs = torch.stack(log_probs, dim=2).exp()
probs = probs / probs.sum(2, keepdim=True)
Sources: python/dgllife/model/readout/weave_readout.py:75-93
Handling Empty Graphs
Readout functions must handle edge cases where a graph might have zero nodes, particularly in generative processes. For instance, GraphEmbed in DGMG explicitly checks g.num_nodes() and returns a zero tensor of the appropriate graph_hidden_size if the graph is empty.
Sources: python/dgllife/model/model_zoo/dgmg.py:204-207
Graph readout functions provide the bridge between localized atomic information and global molecular properties. By offering a variety of methods—from simple pooling in MLPNodeReadout to complex distributional capturing in WeaveGather and learnable gating in GraphEmbed—the library supports a wide range of molecular graph analysis requirements.