StruRW(in_dim, hid_dim, num_classes, num_layers=2, cls_dim=128, cls_layers=2, dropout=0.0, gnn='GS', pooling='mean', reweight=True, pseudo=True, ew_start=100, ew_freq=20, lamb=0.8, mode='erm', act=F.relu, bn=False, weight_decay=0.0001, lr=0.05, epoch=100, device='cuda:0', batch_size=0, num_neigh=-1, verbose=2, **kwargs)

Bases: BaseGDA

Structural Re-weighting Improves Graph Domain Adaptation (ICML-23).

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.

  • cls_dim (int, default: 128 ) –

    Hidden dimension for classification layer. Default: 128.

  • cls_layers (int, default: 2 ) –

    Total number of cls layers in model. Default: 2.

  • dropout (float, default: 0.0 ) –

    Dropout rate. Default: 0..

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

    GNN backbone. Default: GS.

  • pooling (string, default: 'mean' ) –

    Aggregation in gnn. Default: mean.

  • bn (bool, default: False ) –

    Batch normalization or not. Default: False.

  • reweight (bool, default: True ) –

    Reweight the edge in source graph or not. Default: True.

  • pseudo (bool, default: True ) –

    Use pseudo labels in target graph or not. Default: True.

  • ew_start (int, default: 100 ) –

    Starting epoch for edge reweighting. Default: 100.

  • ew_freq (int, default: 20 ) –

    Frequency for edge reweighting. Default: 20.

  • lamb (float, default: 0.8 ) –

    Trade-off parameter for edge reweight. Default: 0.8.

  • mode (string, default: 'erm' ) –

    The training mode of StruRW. Default: erm.

  • weight_decay (float, default: 0.0001 ) –

    Weight decay (L2 penalty). Default: 0.0001.

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

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

  • lr (float, default: 0.05 ) –

    Learning rate. Default: 0.05.

  • epoch (int, default: 100 ) –

    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: 2.

  • **kwargs

    Other parameters for the model.

cal_edge_prob_sep(src_graph, tgt_graph, tgt_pred)

Calculate edge probabilities for source and target graphs.

Parameters:
  • src_graph (Data) –

    Source domain graph.

  • tgt_graph (Data) –

    Target domain graph.

  • tgt_pred (Tensor) –

    Predicted labels for target domain.

Returns:
  • tuple

    Contains: - src_edge_prob : torch.Tensor Edge probabilities in source graph. - tgt_edge_prob : torch.Tensor Edge probabilities in target graph (predicted). - tgt_true_edge_prob : torch.Tensor Edge probabilities in target graph (true).

cal_reweight(source_data, target_data, target_pred)

Calculate edge weights for source graph based on structural alignment.

Parameters:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

  • target_pred (Tensor) –

    Predicted labels for target domain.

Notes

Updates source_data.edge_weight based on the ratio of edge probabilities between source and target domains.

cal_str_dif_rel(pred_mtx, true_mtx)

Calculate absolute and relative structural differences.

Parameters:
  • pred_mtx (Tensor) –

    Predicted structural matrix.

  • true_mtx (Tensor) –

    True structural matrix.

Returns:
  • tuple

    Contains: - abs_diff : torch.Tensor Average absolute difference. - rel_diff : torch.Tensor Average relative difference.

cal_str_diff_ratio(pred_mtx, true_mtx)

Calculate structural difference ratio.

Parameters:
  • pred_mtx (Tensor) –

    Predicted structural matrix.

  • true_mtx (Tensor) –

    True structural matrix.

Returns:
  • Tensor

    Ratio of structural differences excluding diagonal elements.

calculate_str_diff(src_edge_prob, tgt_edge_prob, tgt_true_edge_prob)

Calculate multiple structural difference metrics between source and target graphs.

Parameters:
  • src_edge_prob (Tensor) –

    Edge probability matrix for source graph.

  • tgt_edge_prob (Tensor) –

    Edge probability matrix for target graph (predicted).

  • tgt_true_edge_prob (Tensor) –

    Edge probability matrix for target graph (true labels).

Returns:
  • tuple

    Contains: - list : List of structural differences [src_tgt_diff_abs, src_tgt_diff_rel, ratio_diff] - tgt_diff_abs : torch.Tensor Absolute difference between predicted and true target structure.

Notes

Computes four types of structural differences:

  1. Target prediction vs. true target (absolute and relative)
  2. Target prediction vs. source (absolute and relative)
  3. Ratio-based structural difference
  4. Log-based structural difference
fit(source_data, target_data)

Train the StruRW 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:

Edge weight initialization:

  • Sets default edge weights to 1 if not provided
  • Handles both source and target graphs

Batch processing setup:

  • Full batch (batch_size=0): Uses entire graph
  • Mini-batch: Creates neighbor loaders with specified size

Model initialization:

  • Sets up main model (GNN)
  • For 'adv' mode: Initializes domain discriminator
  • Configures optimizer based on training mode

Training loop:

  • Updates edge weights periodically if reweighting is enabled
  • Computes losses based on selected mode (erm/mixup/mmd/adv)
  • Tracks and logs training metrics

The method adapts its behavior based on the selected training mode and handles both full-batch and mini-batch training scenarios.

forward_model(source_data, target_data, alpha, epoch)

Forward pass of the model for non-mixup modes.

Parameters:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

  • alpha (float) –

    Gradient reversal scaling parameter.

  • epoch (int) –

    Current training epoch.

Returns:
  • tuple

    Contains: - loss : torch.Tensor Combined loss based on training mode. - source_logits : torch.Tensor Model predictions for source domain. - target_logits : torch.Tensor Model predictions for target domain.

Notes

Implements different training modes:

  • 'erm': Standard cross-entropy loss
  • 'adv': Adversarial training with domain discriminator
  • 'mmd': Maximum Mean Discrepancy loss
forward_model_mixup(source_data, target_data, epoch)

Forward pass of the model for mixup mode.

Parameters:
  • source_data (Data) –

    Source domain graph data.

  • target_data (Data) –

    Target domain graph data.

  • epoch (int) –

    Current training epoch.

Returns:
  • tuple

    Contains: - loss : torch.Tensor Cross-entropy loss on mixed data. - source_logits : torch.Tensor Model predictions for source domain. - target_logits : torch.Tensor Model predictions for target domain.

Notes

Implements mixup training with beta distribution sampling and edge reweighting if enabled.

id_node(data, id_new_value_old)

Update graph structure based on new node indices.

Parameters:
  • data (Data) –

    Input graph data.

  • id_new_value_old (ndarray) –

    Mapping from new to old indices.

Returns:
  • Data

    Graph with updated node indices and edge connections.

init_model(**kwargs)

Initialize the StruRW model.

Parameters:
  • **kwargs

    Additional parameters for the base models.

Returns:
  • Module

    Either MixupBase or ReweightGNN model based on the mode.

Notes

Initializes different model architectures based on mode:

  • 'mixup': Returns MixupBase model
  • others: Returns ReweightGNN model with specified backbone
predict(data)

Make predictions on given data.

Parameters:
  • data (Data) –

    Input graph data.

Returns:
  • tuple

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

Notes

Handles both mixup and standard prediction modes.

process_graph(data)

Process the input graph data.

Parameters:
  • data (Data) –

    Input graph data to be processed.

Notes

Placeholder method for graph preprocessing.

shuffle_data(data)

Shuffle node indices in the graph.

Parameters:
  • data (Data) –

    Input graph data.

Returns:
  • tuple

    Contains: - data : torch_geometric.data.Data Graph with shuffled node indices. - id_new_value_old : numpy.ndarray Mapping from new to old indices.