PageRank(k=5, alpha=0.9, **kwargs)

Bases: MessagePassing

Implementation of PageRank algorithm as a message passing layer.

This class implements a personalized PageRank algorithm that can be used as a message passing layer in graph neural networks. It performs k iterations of random walk with restart to compute node importance scores.

Parameters:
  • k (int, default: 5 ) –

    Number of iterations for random walk. Default: 5.

  • alpha (float, default: 0.9 ) –

    Restart probability (teleportation factor). Default: 0.9.

  • **kwargs

    Additional arguments for MessagePassing.

Attributes:
  • k (int) –

    Number of iterations for random walk.

  • alpha (float) –

    Restart probability.

Notes

The PageRank computation follows these steps:

  • Normalize edge weights using GCN normalization

  • For k iterations:

    • Propagate messages along edges

    • Combine with restart probability

  • Return final node importance scores

The message passing follows the formula: x = (1 - alpha) * propagate(x) + alpha * hidden, where hidden is the initial node features.

forward(x, edge_index, edge_weight=None)

Forward pass of the PageRank layer.

Parameters:
  • x (Tensor) –

    Node feature matrix, shape (num_nodes, feature_dim).

  • edge_index (Tensor) –

    Edge indices, shape (2, num_edges).

  • edge_weight (Tensor, default: None ) –

    Edge weights. Default: None.

Returns:
  • Tensor

    Updated node features after k iterations of PageRank.

message(x_j, norm)

Compute messages for each edge.

Parameters:
  • x_j (Tensor) –

    Node features of the target nodes.

  • norm (Tensor) –

    Normalized edge weights.

Returns:
  • Tensor

    Messages to be aggregated.

SEPA(in_dim, hid_dim, num_classes, num_layers=2, dropout=0.0, gnn='gcn', lamda_im=0.2, lamda_pp=0.1, lamda_sep=1.0, tau=1.5, act=F.relu, weight_decay=0.01, lr=0.001, epoch=200, device='cuda:0', batch_size=0, num_neigh=-1, verbose=2, **kwargs)

Bases: BaseGDA

Structure Enhanced Prototypical Alignment for Unsupervised Cross-Domain Node Classification (NN-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: 2 ) –

    Total number of layers in model. Default: 2.

  • dropout (float, default: 0.0 ) –

    Dropout rate. Default: 0..

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

    GNN backbone. Default: gcn.

  • lamda_im (float, default: 0.2 ) –

    Parameter of loss_im. Default: 0.2.

  • lamda_pp (float, default: 0.1 ) –

    Parameter of loss_pp. Default: 0.1.

  • lamda_sep (float, default: 1.0 ) –

    Parameter of loss_sep. Default: 1.0.

  • tau (float, default: 1.5 ) –

    Temperature in info-nce loss. Default: 1.5.

  • weight_decay (float, default: 0.01 ) –

    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.001 ) –

    Learning rate. Default: 0.001.

  • epoch (int, default: 200 ) –

    Maximum number of training epoch. Default: 100.

  • 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: 0.

  • **kwargs

    Other parameters for the model.

cls_im(prob)

Compute information maximization loss for domain adaptation.

This function calculates the information maximization loss which consists of two components:

  • Diversity loss: Encourages the model to make diverse predictions across classes
  • Entropy loss: Minimizes the uncertainty of predictions
Parameters:
  • prob (Tensor) –

    Probability distribution over classes, shape (num_samples, num_classes)

Returns:
  • tuple

    Contains (loss_im, div_loss, ent_loss, ent_loss_temp):

    • loss_im : torch.Tensor Total information maximization loss (div_loss + ent_loss)
    • div_loss : torch.Tensor Diversity loss term
    • ent_loss : torch.Tensor Mean entropy loss across samples
    • ent_loss_temp : torch.Tensor Per-sample entropy values
Notes

The loss is computed as:

  • Diversity loss: -sum(mean_prob * log(mean_prob))
  • Entropy loss: mean(-sum(prob * log(prob)))

where mean_prob is the average probability distribution across samples

fit(source_data, target_data)

Train the SEPA model.

Parameters:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

Notes

The training process includes:

  • Creating data loaders for both domains
  • Initializing the GNN model and optimizer
  • Training for specified number of epochs
  • Logging training progress (loss and micro-F1 score)
forward_model(source_data, target_data)

Forward pass of the SEPA model for domain adaptation.

Parameters:
  • source_data (Data) –

    Source domain graph data containing:

  • target_data (Data) –

    Target domain graph data with the same structure as source_data

Returns:
  • tuple

    Contains (loss, source_logits, target_logits): - loss : torch.Tensor Combined loss value - source_logits : torch.Tensor Model predictions for source domain nodes - target_logits : torch.Tensor Model predictions for target domain nodes

Notes

This function requires batch training (batch_size > 0) as specified in the model initialization. The implementation should handle batched data from both source and target domains.

init_model(**kwargs)

Initialize the SEPA model.

Parameters:
  • **kwargs

    Other parameters for the SEPA model.

Returns:
  • SEPA

    Initialized SEPA model on the specified device.

obtain_source_prototype(feat, label)

Compute class prototypes for source domain features.

This function calculates the mean feature vector (prototype) for each class in the source domain by averaging the features of samples belonging to the same class.

Parameters:
  • feat (Tensor) –

    Node features from source domain, shape (num_nodes, feature_dim)

  • label (Tensor) –

    Class labels for source domain nodes, shape (num_nodes,)

Returns:
  • Tensor

    Class prototypes, shape (num_classes, feature_dim) Each row represents the mean feature vector for one class

Notes

The prototype for each class is computed as: center = sum(features * onehot) / sum(onehot), where onehot is the one-hot encoding of class labels

predict(data)

Make predictions using the trained model.

Parameters:
  • data (Data) –

    Input graph data.

Returns:
  • tuple

    Contains (logits, labels):

    • logits : torch.Tensor Model predictions
    • labels : torch.Tensor True labels from the data
process_graph(data)

Process the input graph data.

Parameters:
  • data (Data) –

    Input graph data to be processed.

Notes

This is a placeholder method that should be implemented by subclasses if graph preprocessing is needed.

proto_alignment(target_proto, source_proto)

Compute prototype alignment loss between source and target domains.

This function calculates a loss that aligns class prototypes between source and target domains using InfoNCE loss. It encourages similar classes to have similar prototypes across domains while maintaining discriminability between different classes.

Parameters:
  • target_proto (Tensor) –

    Class prototypes from target domain, shape (num_classes, feature_dim)

  • source_proto (Tensor) –

    Class prototypes from source domain, shape (num_classes, feature_dim)

Returns:
  • Tensor

    Prototype alignment loss value that measures the alignment between domains

Notes

The alignment loss is computed using InfoNCE loss with three components:

  • Target-Source similarity: Measures alignment between corresponding classes
  • Target-Target similarity: Ensures target prototypes are discriminable
  • Source-Source similarity: Ensures source prototypes are discriminable

The loss is computed as:

  • Normalize prototypes to unit length

  • Compute cosine similarity matrices between:

    • Target and source prototypes (ts)
    • Target and target prototypes (tt)
    • Source and source prototypes (ss)
  • Apply temperature scaling (tau)

  • Calculate InfoNCE loss using positive pairs (same class) and negative pairs (different classes)

The loss encourages:

  • High similarity between corresponding class prototypes across domains
  • Low similarity between different class prototypes within each domain
seperate_center(center, feat, label)

Compute separation loss between class prototypes to enhance class discriminability.

This function calculates a loss that encourages class prototypes to be well-separated in the feature space by maximizing the distance between different class prototypes while minimizing the distance between samples of the same class.

Parameters:
  • center (Tensor) –

    Class prototypes, shape (num_classes, feature_dim)

  • feat (Tensor) –

    Node features, shape (num_nodes, feature_dim)

  • label (Tensor) –

    Class labels for nodes, shape (num_nodes,)

Returns:
  • Tensor

    Separation loss value that measures the discriminability between class prototypes

Notes

The separation loss is computed using InfoNCE loss:

  • Normalize prototypes to unit length
  • Compute cosine similarity matrix between prototypes
  • Apply temperature scaling (tau) to the similarity scores
  • Calculate InfoNCE loss by comparing positive pairs (same class) against negative pairs (different classes)

The loss encourages:

  • High similarity between samples of the same class
  • Low similarity between samples of different classes