GraphCTA(in_dim, hid_dim, num_classes, num_layers=3, dropout=0.0, act=F.relu, loop_model=3, loop_adj=1, loop_feat=4, ratio=0.1, K=5, tau=0.2, lamb=0.2, momentum=0.9, make_undirected=True, weight_decay=0.0, lr=0.0001, epoch=500, gnn='gcn', device='cuda:0', batch_size=0, num_neigh=-1, verbose=2, **kwargs)

Bases: BaseGDA

Collaborate to Adapt: Source-Free Graph Domain Adaptation via Bi-directional Adaptation (WWW-24).

Parameters:
  • in_dim (int) –

    Input feature dimension.

  • hid_dim (int) –

    Hidden dimension of model.

  • num_classes (int) –

    Total number of classes.

  • num_layers (int, default: 3 ) –

    Total number of layers in model. Default: 3.

  • dropout (float, default: 0.0 ) –

    Dropout rate. Default: 0..

  • gnn (string, default: 'gcn' ) –

    GNN backbone. Default: gcn.

  • loop_model (int, default: 3 ) –

    Loops for optimizing model. Default: 3.

  • loop_adj (int, default: 1 ) –

    Loops for optimizing structure. Default: 1.

  • loop_feat (int, default: 4 ) –

    Loops for optimizing features. Default: 4.

  • K (int, default: 5 ) –

    Number of k-nearest neighbors. Default: 5.

  • ratio (float, default: 0.1 ) –

    Budget B for changing graph structure. Default: 0.1.

  • tau (float, default: 0.2 ) –

    Contrastive loss hyperparameter. Default: 0.2.

  • lamb (float, default: 0.2 ) –

    Loss trade off hyperparameter. Default: 0.2.

  • momentum (float, default: 0.9 ) –

    Momentum update hyperparameter. Default: 0.9.

  • make_undirected (bool, default: True ) –

    Transform into undirected graph. Default: True.

  • weight_decay (float, default: 0.0 ) –

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

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

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

  • lr (float, default: 0.0001 ) –

    Learning rate. Default: 0.004.

  • epoch (int, default: 500 ) –

    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.

entropy(input_)

Calculate entropy of probability distribution.

Parameters:
  • input_ (Tensor) –

    Input probability distribution.

Returns:
  • Tensor

    Computed entropy values per sample.

Notes
  • Handles numerical stability with epsilon
  • Used for confidence-based pseudo-labeling
fit(source_data, target_data)

Train the GraphCTA model with collaborative adaptation.

Parameters:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

Notes

Implementation consists of three main phases:

Initialization

  • Sets up data loaders
  • Initializes model and optimizers
  • Creates memory banks for features and classes
  • Prepares feature and structure perturbation variables

Source Pretraining

  • Trains model on source domain
  • Uses standard cross-entropy loss
  • Prepares model for adaptation

Target Adaptation (Iterative)

  • Model Update Loop:

    • Updates model parameters
    • Computes prototype-based alignment
    • Updates memory banks with momentum
    • Combines local and contrastive losses
  • Feature Optimization Loop:

    • Optimizes feature perturbations
    • Uses test-time adaptation loss
    • Maintains feature consistency
  • Structure Optimization Loop:

    • Modifies edge weights
    • Ensures budget constraints
    • Preserves graph properties

Implementation Features:

  • Memory-based prototype learning
  • Gradient checkpointing for efficiency
  • Momentum updates for stability
  • Budget-constrained modifications
  • Multiple optimization objectives
  • Collaborative feature-structure adaptation
forward_model(data, **kwargs)

Forward pass placeholder for GraphCTA model.

Parameters:
  • data (Data) –

    Input graph data.

  • **kwargs

    Additional arguments.

Notes

Placeholder method as GraphCTA implements custom forward logic through:

  • Source domain training in train_source()
  • Collaborative adaptation in fit()
  • Feature and structure optimization
  • Memory-based prototype learning
init_model(**kwargs)

Initialize the GraphCTA base model.

Parameters:
  • **kwargs

    Additional parameters for model initialization.

Returns:
  • GNNBase

    Initialized model with specified architecture.

Notes

Configures base GNN model with:

  • Input and hidden dimensions
  • Number of classes and layers
  • Dropout rate
  • Specified GNN backbone type
  • Device placement
instance_proto_alignment(feat, center, pred)

Compute instance-prototype alignment loss.

Parameters:
  • feat (Tensor) –

    Node features.

  • center (Tensor) –

    Prototype centers.

  • pred (Tensor) –

    Predicted labels.

Returns:
  • tuple

    Contains: - loss : torch.Tensor Contrastive alignment loss. - weight : torch.Tensor Instance-prototype similarity weights.

Notes
  • Implements temperature-scaled contrastive loss
  • Handles both instance-prototype and instance-instance relations
  • Uses cosine similarity for feature comparison
predict(data)

Make predictions using the adapted model.

Parameters:
  • data (Data) –

    Input graph data.

Returns:
  • tuple

    Contains: - logits : torch.Tensor Model predictions using optimized features and structure. - labels : torch.Tensor True labels.

Notes
  • Uses transformed features (self.new_feat)
  • Uses optimized edge structure (self.edge_index, self.edge_weight)
  • Evaluates model in inference mode
process_graph(data)

Process input graph data.

Parameters:
  • data (Data) –

    Input graph to be processed.

Notes

Placeholder method as graph processing is handled through:

  • Feature perturbation optimization
  • Structure modification
  • Memory bank updates
  • Prototype-based alignment
sample_final_edges(n_perturbations, perturbed_edge_weight, data, modified_edge_index, n, mem_fea, mem_cls)

Sample final edge structure based on learned weights.

Parameters:
  • n_perturbations (int) –

    Maximum number of allowed edge modifications.

  • perturbed_edge_weight (Tensor) –

    Learned edge weights.

  • data (Data) –

    Target graph data.

  • modified_edge_index (Tensor) –

    Modified edge indices.

  • n (int) –

    Number of nodes.

  • mem_fea (Tensor) –

    Memory bank features.

  • mem_cls (Tensor) –

    Memory bank class predictions.

Returns:
  • tuple

    Contains: - edge_index : torch.Tensor Final edge structure. - edge_weight : torch.Tensor Final edge weights.

Notes
  • Uses iterative sampling strategy
  • Maintains best performing structure
  • Ensures perturbation budget constraints
  • Handles undirected graph requirements
test_time_loss(feat, edge_index, edge_weight, mem_fea, mem_cls)

Compute test-time adaptation loss.

Parameters:
  • feat (Tensor) –

    Node features.

  • edge_index (Tensor) –

    Edge indices.

  • edge_weight (Tensor) –

    Edge weights.

  • mem_fea (Tensor) –

    Memory bank features.

  • mem_cls (Tensor) –

    Memory bank class predictions.

Returns:
  • Tensor

    Combined loss value.

Notes

Loss components include:

  1. Pseudo-label based classification
  2. Feature similarity with memory bank
  3. Class-wise prototype alignment
  4. Confidence-based sample selection
train_source(optimizer)

Train the model on source domain data.

Parameters:
  • optimizer (Optimizer) –

    Optimizer for model parameters.

Notes

Training process includes:

Per-epoch Operations:

  • Tracks cumulative loss
  • Maintains logits and labels
  • Computes performance metrics

Batch Processing:

  • Moves data to device
  • Computes model predictions
  • Applies negative log-likelihood loss
  • Updates model parameters

Monitoring:

  • Computes micro-F1 score
  • Logs training progress
  • Tracks timing information

Implementation Features:

  • Supports batch processing
  • Uses softmax with log probabilities
  • Accumulates predictions for full evaluation
  • Comprehensive logging
update_edge_weights(gradient, optimizer_adj, perturbed_edge_weight)

Update edge weights during structure optimization.

Parameters:
  • gradient (Tensor) –

    Computed gradients for edge weights.

  • optimizer_adj (Optimizer) –

    Optimizer for edge weights.

  • perturbed_edge_weight (Tensor) –

    Current edge weights to be updated.

Notes
  • Applies gradient updates to edge weights
  • Maintains minimum weight threshold
  • Uses Adam optimizer for updates