pygip.models.attack package¶
Subpackages¶
Submodules¶
pygip.models.attack.AdvMEA module¶
- class pygip.models.attack.AdvMEA.AdvMEA(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
BaseAttack
- _abc_impl = <_abc_data object>¶
- supported_api_types = {'dgl'}¶
pygip.models.attack.CEGA module¶
- class pygip.models.attack.CEGA.CEGA(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
BaseAttack
- _abc_impl = <_abc_data object>¶
- attack(seed=1, cuda=None, LR=0.001, TGT_LR=0.01, EVAL_EPOCH=10, TGT_EPOCH=10, WARMUP_EPOCH=4, dropout=False, model_performance=True, **kwargs)[source]¶
Execute the attack.
- supported_api_types = {'dgl'}¶
- pygip.models.attack.CEGA.attack0(dataset_name, seed, cuda, attack_node_arg=0.25, file_path='', LR=0.001, TGT_LR=0.01, EVAL_EPOCH=1000, TGT_EPOCH=1000, WARMUP_EPOCH=400, dropout=False, model_performance=True, **kwargs)[source]¶
- pygip.models.attack.CEGA.convert_pyg_to_dgl(pyg_data)[source]¶
Converts a PyTorch Geometric Data object into a DGLGraph.
- Parameters:
pyg_data (torch_geometric.data.Data) – PyTorch Geometric Data object.
- Returns:
The converted DGL graph.
- Return type:
dgl.DGLGraph
- pygip.models.attack.CEGA.get_max_info_entropy_node_set(idx_used, high_score_nodes, labels, batch_size, adj_matrix2, num_class, model_prediction)[source]¶
- pygip.models.attack.CEGA.get_max_nnd_node_dense(idx_used, high_score_nodes, min_distance, distance_aax, num_ones, num_node, adj_matrix2, gamma=1)[source]¶
- pygip.models.attack.CEGA.get_receptive_fields_dense(cur_neighbors, selected_node, weighted_score, adj_matrix2)[source]¶
- pygip.models.attack.CEGA.load_data_from_grain(path='./data', dataset='cora')[source]¶
ind.[:dataset].x => the feature vectors of the training instances (scipy.sparse.csr.csr_matrix) ind.[:dataset].y => the one-hot labels of the labeled training instances (numpy.ndarray) ind.[:dataset].allx => the feature vectors of both labeled and unlabeled training instances (csr_matrix) ind.[:dataset].ally => the labels for instances in ind.dataset_str.allx (numpy.ndarray) ind.[:dataset].graph => the dict in the format {index: [index of neighbor nodes]} (collections.defaultdict) ind.[:dataset].tx => the feature vectors of the test instances (scipy.sparse.csr.csr_matrix) ind.[:dataset].ty => the one-hot labels of the test instances (numpy.ndarray) ind.[:dataset].test.index => indices of test instances in graph, for the inductive setting
- pygip.models.attack.CEGA.perturb_avg(net, sub_g, sub_features, num_perturbations, noise_level)[source]¶
- pygip.models.attack.CEGA.quantile_selection(A, B, C, index_1, index_2, index_3, sub_train_mask, sub_train_mask_new, num_each)[source]¶
- pygip.models.attack.CEGA.rank_centrality(sub_g, sub_train_mask, sub_train_mask_new, num_each, return_rank=True)[source]¶
- pygip.models.attack.CEGA.rank_cluster(net, sub_g, sub_features, labels, total_sub_nodes, sub_train_mask, sub_train_mask_new, num_clusters, num_each, return_rank=True)[source]¶
- pygip.models.attack.CEGA.rank_density(net, sub_g, sub_features, sub_train_mask, sub_train_mask_new, num_each, num_clusters, return_rank=True)[source]¶
- pygip.models.attack.CEGA.rank_diversity(net, sub_g, sub_features, sub_train_mask, sub_train_mask_new, num_each, num_clusters, rho, return_rank=True)[source]¶
- pygip.models.attack.CEGA.rank_entropy(net, sub_g, sub_features, sub_train_mask, sub_train_mask_new, num_each, return_rank=True)[source]¶
- pygip.models.attack.CEGA.rank_perturb(net, sub_g, sub_features, num_perturbations, sub_train_mask, sub_train_mask_new, noise_level, num_each, return_rank=True)[source]¶
pygip.models.attack.DataFreeMEA module¶
- class pygip.models.attack.DataFreeMEA.DFEAAttack(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
BaseAttack
- _abc_impl = <_abc_data object>¶
- supported_api_types = {'dgl'}¶
- class pygip.models.attack.DataFreeMEA.DFEATypeI(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
DFEAAttack
Type I: Uses victim outputs + gradients for surrogate training.
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DataFreeMEA.DFEATypeII(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
DFEAAttack
Type II: Uses victim outputs only (hard labels).
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DataFreeMEA.DFEATypeIII(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
DFEAAttack
Type III: Two surrogates with victim supervision + consistency.
- _abc_impl = <_abc_data object>¶
pygip.models.attack.base module¶
- class pygip.models.attack.base.BaseAttack(dataset, attack_node_fraction=None, model_path=None, device=None)[source]¶
Bases:
ABC
Abstract base class for attack models.
This class provides a common interface for various attack strategies on graph-based machine learning models. It handles device management, dataset loading, and compatibility checks to ensure that the attack can be executed on the given dataset and model API type.
- supported_api_types¶
A set of strings representing the supported API types (e.g., ‘pyg’, ‘dgl’).
- Type:
set
- supported_datasets¶
A set of strings representing the names of supported dataset classes.
- Type:
set
- device¶
The computing device (CPU or GPU) to be used for the attack.
- Type:
torch.device
- graph_dataset¶
The raw graph dataset from the underlying library.
- graph_data¶
The primary graph data structure.
- num_nodes¶
The number of nodes in the graph.
- Type:
int
- num_features¶
The number of features per node.
- Type:
int
- num_classes¶
The number of classes for node classification.
- Type:
int
- attack_node_fraction¶
The fraction of nodes to be targeted by the attack.
- Type:
float, optional
- model_path¶
The path to a pre-trained target model.
- Type:
str, optional
- _abc_impl = <_abc_data object>¶
- _check_dataset_compatibility()[source]¶
Checks if the dataset is compatible with the attack.
- Raises:
ValueError – If the dataset’s API type or class name is not in the list of supported types.
- supported_api_types = {}¶
- supported_datasets = {}¶
Module contents¶
- class pygip.models.attack.AdvMEA(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
BaseAttack
- _abc_impl = <_abc_data object>¶
- supported_api_types = {'dgl'}¶
- class pygip.models.attack.CEGA(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
BaseAttack
- _abc_impl = <_abc_data object>¶
- attack(seed=1, cuda=None, LR=0.001, TGT_LR=0.01, EVAL_EPOCH=10, TGT_EPOCH=10, WARMUP_EPOCH=4, dropout=False, model_performance=True, **kwargs)[source]¶
Execute the attack.
- supported_api_types = {'dgl'}¶
- class pygip.models.attack.DFEATypeI(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
DFEAAttack
Type I: Uses victim outputs + gradients for surrogate training.
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DFEATypeII(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
DFEAAttack
Type II: Uses victim outputs only (hard labels).
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DFEATypeIII(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
DFEAAttack
Type III: Two surrogates with victim supervision + consistency.
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.ModelExtractionAttack0(dataset, attack_node_fraction, model_path=None, alpha=0.8)[source]¶
Bases:
ModelExtractionAttack
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.ModelExtractionAttack1(dataset, attack_node_fraction)[source]¶
Bases:
ModelExtractionAttack
- _abc_impl = <_abc_data object>¶
- attack()[source]¶
Main attack procedure.
Reads selected nodes from file for training (attack) nodes.
Reads query labels from another file.
Builds a shadow graph from the given adjacency matrix file.
Trains a shadow model on the selected nodes, then evaluates fidelity & accuracy against the original target graph.
- class pygip.models.attack.ModelExtractionAttack2(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
ModelExtractionAttack
ModelExtractionAttack2.
A strategy that randomly samples a fraction of nodes as attack nodes, synthesizes identity features for all nodes, then trains an extraction model. The leftover nodes become test nodes.
Inherits¶
ModelExtractionAttack
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.ModelExtractionAttack3(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
ModelExtractionAttack
ModelExtractionAttack3.
A more complex extraction strategy that uses a “shadow graph index” file to build partial subgraphs and merges them. It queries selected nodes from a potential set and forms a combined adjacency matrix.
Inherits¶
ModelExtractionAttack
- _abc_impl = <_abc_data object>¶
- attack()[source]¶
Main attack procedure.
Steps: 1. Loads indices for two subgraphs from text files. 2. Selects attack_node_num nodes from the first subgraph index. 3. Merges subgraph adjacency matrices and constructs a new graph
with combined features.
Trains a new GCN and evaluates fidelity & accuracy w.r.t. the original target.
- class pygip.models.attack.ModelExtractionAttack4(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
ModelExtractionAttack
ModelExtractionAttack4.
Another graph-based strategy that reads node indices from files, merges adjacency matrices, and links new edges based on feature similarity.
Inherits¶
ModelExtractionAttack
- _abc_impl = <_abc_data object>¶
- attack()[source]¶
Main attack procedure.
Reads two sets of node indices from text files.
Selects a fixed number of nodes from the target set for attack.
Builds a combined adjacency matrix with zero blocks, then populates edges between shadow and attack nodes based on a distance threshold.
Trains a new GCN on this combined graph and evaluates fidelity & accuracy.
- class pygip.models.attack.ModelExtractionAttack5(dataset, attack_node_fraction, model_path=None)[source]¶
Bases:
ModelExtractionAttack
ModelExtractionAttack5.
Similar to ModelExtractionAttack4, but uses a slightly different strategy to link edges between nodes based on a threshold distance.
Inherits¶
ModelExtractionAttack
- _abc_impl = <_abc_data object>¶
- attack()[source]¶
Main attack procedure.
Reads two sets of node indices (for target and shadow nodes).
Builds a block adjacency matrix with all zero blocks, then links edges between attack nodes and shadow nodes if the feature distance is less than a threshold.
Trains a new GCN on this combined graph and evaluates fidelity & accuracy.
- class pygip.models.attack.RealisticAttack(dataset, attack_node_fraction, model_path=None, hidden_dim=64, threshold_s=0.7, threshold_a=0.5)[source]¶
Bases:
BaseAttack
DGL-based GNN model extraction attack
- _abc_impl = <_abc_data object>¶
- add_potential_edges(candidate_edges, labeled_nodes)[source]¶
Add potential edges based on edge prediction results
- simulate_target_model_queries(query_nodes, error_rate=0.15)[source]¶
Simulate target model queries with a certain proportion of incorrect labels
- supported_api_types = {'dgl'}¶
- supported_datasets = {}¶