TDSS(in_dim, hid_dim, num_classes, mode='node', smooth_mode='RW', num_layers=2, dropout=0.0, act=F.relu, s_pnums=0, t_pnums=30, k=2, rw_len=4, alpha=0.001, beta=0.0001, weight_decay=0.005, adv=False, lr=0.01, epoch=200, device='cuda:0', batch_size=0, num_neigh=-1, verbose=2, **kwargs)
Bases: BaseGDA
Smoothness Really Matters: A Simple Yet Effective Approach for Unsupervised Graph Domain Adaptation (AAAI-25).
| Parameters: |
|
|---|
compute_laplacian_loss(features, edge_index)
Compute the Laplacian regularization loss for graph smoothness.
This method computes the normalized Laplacian loss which encourages smoothness in node features across connected nodes in the graph. The loss is computed as the sum of squared differences between normalized features of connected nodes.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
The computation follows these steps:
-
Compute node degrees from edge_index
-
Calculate degree inverse square root for normalization
-
Normalize features using degree normalization
-
Compute squared differences between connected nodes
-
Weight by edge weights (all ones in this implementation)
-
Sum and divide by 2 to avoid double counting
The loss encourages connected nodes to have similar features, promoting graph smoothness and regularization.
Examples:
>>> features = torch.randn(10, 64)
>>> edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]])
>>> loss = self.compute_laplacian_loss(features, edge_index)
>>> print(loss.item()) # Scalar loss value
fit(source_data, target_data)
Train the A2GNN model on source and target domain data.
| Parameters: |
|
|---|
Notes
Training process includes:
Data Preparation
- Configures loaders for node/graph level tasks
- Handles both full-batch and mini-batch scenarios
- Sets up appropriate batch processing
Training Loop
- Dynamic adaptation parameter scaling
- Asymmetric message propagation
-
Domain adaptation through either:
- Adversarial training
- MMD minimization
-
Comprehensive progress monitoring
Implementation Features
- Flexible task handling (node/graph)
- Efficient batch processing
- Adaptive learning mechanisms
forward_model(source_data, target_data, alpha)
Forward pass of the A2GNN model.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
Implements multiple components:
- Asymmetric propagation (s_pnums vs t_pnums)
- Classification loss on source domain
- Domain adaptation (adversarial or MMD)
- Feature bottleneck processing
init_model(**kwargs)
Initialize the A2GNN base model.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
Configures model with:
- Asymmetric propagation settings
- Domain adaptation components
- Task-specific architecture (node/graph)
- Optional adversarial training module
predict(data, source=False)
Make predictions on input data.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
- Uses different propagation steps for source/target
- Handles batch processing efficiently
- Concatenates results for full predictions
- Maintains evaluation mode consistency
process_graph(data)
Process the input graph data.
| Parameters: |
|
|---|
Notes
Placeholder method as preprocessing is handled through:
- Asymmetric propagation mechanisms
- Domain-specific feature processing
- Batch-wise data handling
smoothness(edge_index, edge_attr, num_nodes)
Compute smoothness-based edge connections using random walk or k-hop neighbors.
This method constructs smoothness-based adjacency matrices using either random walk sampling or k-hop neighbor expansion, depending on the smooth_mode parameter.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
If smooth_mode is 'RW':
-
Performs random walk sampling with length rw_len
-
Creates dense adjacency matrix from walk paths
-
Converts back to sparse format
If smooth_mode is 'K-hop':
-
For k=1: adds self-loops to existing edges
-
For k>1: applies TwoHopNeighbor transformation (k-1) times
-
Adds remaining self-loops to the expanded graph
Examples:
>>> edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]])
>>> edge_attr = torch.randn(4, 10)
>>> num_nodes = 3
>>> smooth_edge_index, smooth_edge_attr = self.smoothness(edge_index, edge_attr, num_nodes)
TwoHopNeighbor
Bases: object
A graph transformation that adds two-hop neighbors to the input graph.
This class computes the two-hop neighborhood of each node by performing sparse matrix multiplication of the adjacency matrix with itself, then concatenating the original edges with the new two-hop edges.
| Returns: |
|
|---|
Notes
The transformation works by:
-
Computing the two-hop adjacency matrix using sparse matrix multiplication
-
Removing self-loops from the two-hop connections
-
Concatenating original edges with two-hop edges
-
Coalescing duplicate edges and their attributes
Examples:
>>> from torch_geometric.data import Data
>>> import torch
>>>
>>> # Create a simple graph with 3 nodes
>>> edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long)
>>> data = Data(edge_index=edge_index, num_nodes=3)
>>>
>>> # Apply two-hop neighbor transformation
>>> transformer = TwoHopNeighbor()
>>> result = transformer(data)
>>> print(result.edge_index)
tensor([[0, 1, 1, 2, 0, 2],
[1, 0, 2, 1, 2, 0]])