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:
  • in_dim (int) –

    Input feature dimension.

  • hid_dim (int) –

    Hidden dimension of model.

  • num_classes (int) –

    Total number of classes.

  • mode (str, default: 'node' ) –

    Mode for node or graph level tasks. Default: node.

  • smooth_mode (str, default: 'RW' ) –

    Mode for smoothness construction (RW or K-hop). Default: RW.

  • num_layers (int, default: 2 ) –

    Total number of layers in model. Default: 3.

  • dropout (float, default: 0.0 ) –

    Dropout rate. Default: 0..

  • weight_decay (float, default: 0.005 ) –

    Weight decay (L2 penalty). Default: 0..

  • act (callable activation function or None, default: relu ) –

    Activation function if not None. Default: torch.nn.functional.relu.

  • s_pnums (int, default: 0 ) –

    Number of propagations for source models. Default: 0.

  • t_pnums (int, default: 30 ) –

    Number of propagations for target models. Default: 30.

  • k (int, default: 2 ) –

    K-hop neighbors. Default: 2.

  • rw_len (int, default: 4 ) –

    Random walk length. Default: 4.

  • alpha (float, default: 0.001 ) –

    MMD loss trade-off parameter. Default: 7.

  • beta (float, default: 0.0001 ) –

    Smoothness loss trade-off parameter. Default: 2e-4.

  • adv (bool, default: False ) –

    Adversarial training or not. Default: False.

  • lr (float, default: 0.01 ) –

    Learning rate. Default: 0.004.

  • epoch (int, default: 200 ) –

    Maximum number of training epoch. Default: 200.

  • device (str, default: 'cuda:0' ) –

    GPU or CPU. Default: cuda:0.

  • batch_size (int, default: 0 ) –

    Minibatch size, 0 for full batch training. Default: 0.

  • num_neigh (int, default: -1 ) –

    Number of neighbors in sampling, -1 for all neighbors. Default: -1.

  • verbose (int, default: 2 ) –

    Verbosity mode. Range in [0, 3]. Larger value for printing out more log information. Default: 2.

  • **kwargs

    Other parameters for the model.

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:
  • features (Tensor) –

    Node feature tensor of shape (num_nodes, feature_dim).

  • edge_index (Tensor) –

    Edge index tensor of shape (2, num_edges) representing the graph structure.

Returns:
  • Tensor

    Scalar tensor representing the Laplacian regularization loss. The loss is normalized by dividing by 2 to account for double counting of edges.

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:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

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:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

  • alpha (float) –

    Gradient reversal scaling parameter.

Returns:
  • tuple

    Contains: - loss : torch.Tensor Combined loss from multiple components. - source_logits : torch.Tensor Source domain predictions. - target_logits : torch.Tensor Target domain predictions.

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:
  • **kwargs

    Additional parameters for model initialization.

Returns:
  • A2GNNBase

    Initialized model with specified architecture parameters.

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:
  • data (Data) –

    Input graph data.

  • source (bool, default: False ) –

    Whether predicting on source domain. Default: False.

Returns:
  • tuple

    Contains: - logits : torch.Tensor Model predictions. - labels : torch.Tensor True labels.

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:
  • data (Data) –

    Input graph data to be processed.

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:
  • edge_index (Tensor) –

    Edge index tensor of shape (2, num_edges) representing the graph structure.

  • edge_attr (Tensor or None) –

    Edge attribute tensor of shape (num_edges, feature_dim) or None.

  • num_nodes (int) –

    Number of nodes in the graph.

Returns:
  • tuple of torch.Tensor

    A tuple containing:

    • edge_index : torch.Tensor Modified edge index tensor with smoothness-based connections.

    • edge_attr : torch.Tensor or None Modified edge attribute tensor or None.

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:
  • Data

    The input data object with modified edge_index and edge_attr. The edge_index will contain both original edges and two-hop edges. If edge_attr was None, the new edges will have no attributes. If edge_attr existed, new edges will have zero-valued attributes.

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]])