pygip.models.attack package¶
Subpackages¶
- pygip.models.attack.mea package
- Submodules
- pygip.models.attack.mea.MEA module
ModelExtractionAttack0ModelExtractionAttack1ModelExtractionAttack2ModelExtractionAttack3ModelExtractionAttack4ModelExtractionAttack5_MEABase_MEABase._abc_impl_MEABase._attack_impl()_MEABase._compute_metrics()_MEABase._load_model()_MEABase._query_target()_MEABase._train_surrogate()_MEABase._train_target_model()_MEABase.attack()_MEABase.features_MEABase.graph_MEABase.labels_MEABase.supported_api_types_MEABase.test_mask_MEABase.train_mask
_as_tensor()_safe_dir()add_self_loops()erdos_renyi_graph()load_attack2_generated_graph()load_attack3_shadow_indices()random_shadow_indices()subgraph_from_nodes()
- Module contents
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, attack_x_ratio=1.0, attack_a_ratio=1.0)[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]¶
- Returns:
perf_json (dict) – Performance metrics (JSON-serialisable): accuracy/fidelity/F1 of the surrogate, and optionally target accuracy/F1 for reference.
comp_json (dict) – Computation metrics (JSON-serialisable): attack_time, query_target_time, train_surrogate_time, etc.
- 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_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
BaseAttack- _abc_impl = <_abc_data object>¶
- _evaluate_on_real_test(surrogate, metric, metric_comp)[source]¶
Evaluate the surrogate on the real test set and update metrics
- supported_api_types = {'dgl'}¶
- class pygip.models.attack.DataFreeMEA.DFEATypeI(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
DFEAAttackType I: Uses victim outputs + gradients for surrogate training.
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DataFreeMEA.DFEATypeII(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
DFEAAttackType II: Uses victim outputs only (hard labels).
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DataFreeMEA.DFEATypeIII(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
DFEAAttackType 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:
ABCAbstract 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, attack_x_ratio=1.0, attack_a_ratio=1.0)[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]¶
- Returns:
perf_json (dict) – Performance metrics (JSON-serialisable): accuracy/fidelity/F1 of the surrogate, and optionally target accuracy/F1 for reference.
comp_json (dict) – Computation metrics (JSON-serialisable): attack_time, query_target_time, train_surrogate_time, etc.
- supported_api_types = {'dgl'}¶
- class pygip.models.attack.DFEATypeI(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
DFEAAttackType I: Uses victim outputs + gradients for surrogate training.
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DFEATypeII(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
DFEAAttackType II: Uses victim outputs only (hard labels).
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.DFEATypeIII(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
DFEAAttackType III: Two surrogates with victim supervision + consistency.
- _abc_impl = <_abc_data object>¶
- class pygip.models.attack.ModelExtractionAttack0(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
_MEABaseAttack-0: Random-node label-only extraction on the original graph.
- _abc_impl = <_abc_data object>¶
- _attack_impl()[source]¶
Subclasses must implement this method to 1) build a graph g_att and features x_att for training, 2) pick a list of training indices idx_train of length attack_node_num, 3) query the target for labels on idx_train and train a surrogate, and then return filled metrics objects.
- Return type:
Tuple[AttackMetric,AttackCompMetric]
-
features:
Tensor¶
-
graph:
DGLGraph¶
-
labels:
Tensor¶
-
test_mask:
Tensor¶
-
train_mask:
Tensor¶
- class pygip.models.attack.ModelExtractionAttack1(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
_MEABaseAttack-1: Degree-based sampling of query nodes on the original graph.
- _abc_impl = <_abc_data object>¶
- _attack_impl()[source]¶
Subclasses must implement this method to 1) build a graph g_att and features x_att for training, 2) pick a list of training indices idx_train of length attack_node_num, 3) query the target for labels on idx_train and train a surrogate, and then return filled metrics objects.
- Return type:
Tuple[AttackMetric,AttackCompMetric]
-
features:
Tensor¶
-
graph:
DGLGraph¶
-
labels:
Tensor¶
-
test_mask:
Tensor¶
-
train_mask:
Tensor¶
- class pygip.models.attack.ModelExtractionAttack2(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
_MEABaseAttack-2: Data-free extraction on a synthetic graph with random features.
- _abc_impl = <_abc_data object>¶
- _attack_impl()[source]¶
Subclasses must implement this method to 1) build a graph g_att and features x_att for training, 2) pick a list of training indices idx_train of length attack_node_num, 3) query the target for labels on idx_train and train a surrogate, and then return filled metrics objects.
- Return type:
Tuple[AttackMetric,AttackCompMetric]
-
features:
Tensor¶
-
graph:
DGLGraph¶
-
labels:
Tensor¶
-
test_mask:
Tensor¶
-
train_mask:
Tensor¶
- class pygip.models.attack.ModelExtractionAttack3(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
_MEABaseAttack-3: Shadow-graph extraction. Train on a subgraph induced by a set of target nodes and their neighbors (potential nodes).
- _abc_impl = <_abc_data object>¶
- _attack_impl()[source]¶
Subclasses must implement this method to 1) build a graph g_att and features x_att for training, 2) pick a list of training indices idx_train of length attack_node_num, 3) query the target for labels on idx_train and train a surrogate, and then return filled metrics objects.
- Return type:
Tuple[AttackMetric,AttackCompMetric]
-
features:
Tensor¶
-
graph:
DGLGraph¶
-
labels:
Tensor¶
-
test_mask:
Tensor¶
-
train_mask:
Tensor¶
- class pygip.models.attack.ModelExtractionAttack4(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
_MEABaseAttack-4: Cosine-similarity neighbor expansion. Start from random seeds and expand candidates by feature similarity to form the training subgraph.
- _abc_impl = <_abc_data object>¶
- _attack_impl()[source]¶
Subclasses must implement this method to 1) build a graph g_att and features x_att for training, 2) pick a list of training indices idx_train of length attack_node_num, 3) query the target for labels on idx_train and train a surrogate, and then return filled metrics objects.
- Return type:
Tuple[AttackMetric,AttackCompMetric]
-
features:
Tensor¶
-
graph:
DGLGraph¶
-
labels:
Tensor¶
-
test_mask:
Tensor¶
-
train_mask:
Tensor¶
- class pygip.models.attack.ModelExtractionAttack5(dataset, attack_x_ratio, attack_a_ratio, model_path=None)[source]¶
Bases:
_MEABaseAttack-5: Variant of the shadow-graph attack that samples two candidate lists and trains on their union. If attack_6 index files are present (historical name), they will be used; otherwise we fall back to generated indices.
- _abc_impl = <_abc_data object>¶
- _attack_impl()[source]¶
Subclasses must implement this method to 1) build a graph g_att and features x_att for training, 2) pick a list of training indices idx_train of length attack_node_num, 3) query the target for labels on idx_train and train a surrogate, and then return filled metrics objects.
- Return type:
Tuple[AttackMetric,AttackCompMetric]
-
features:
Tensor¶
-
graph:
DGLGraph¶
-
labels:
Tensor¶
-
test_mask:
Tensor¶
-
train_mask:
Tensor¶
- class pygip.models.attack.RealisticAttack(dataset, attack_x_ratio, attack_a_ratio, model_path=None, hidden_dim=64, threshold_s=0.7, threshold_a=0.5)[source]¶
Bases:
BaseAttackDGL-based GNN model extraction attack with updated metrics API.
- _abc_impl = <_abc_data object>¶
- _evaluate_and_update_metrics(enhanced_graph, metric, metric_comp)[source]¶
Evaluate surrogate against target on the real test set and update metric containers.
- add_potential_edges(candidate_edges, labeled_nodes)[source]¶
Add potential edges whose predicted probability exceeds the threshold.
- attack()[source]¶
Execute the attack and return two JSON-like dicts: performance and computation metrics.
- generate_candidate_edges(labeled_nodes, unlabeled_nodes)[source]¶
Generate candidate edges based on feature cosine similarity threshold.
- simulate_target_model_queries(query_nodes, error_rate=0.15)[source]¶
Query the target model for labels on query_nodes and introduce a small error rate.
- supported_api_types = {'dgl'}¶
- supported_datasets = {}¶