You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

t_robust.rst 7.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. .. _fe:
  2. Graph Robustness
  3. ==========================
  4. Graph robustness is an important research direction in the field of graph representation learning in recent years,
  5. and we have integrated graph robustness-related algorithms in AutoGL, which can be easily used in conjunction with other modules.
  6. Preliminaries
  7. -----------
  8. In AutoGL, we divide the algorithms for graph robustness into three categories, which are placed in different modules for implementation.
  9. Robust graph feature engineering aims to generate robust graph features in the data pre-processing phase to enhance the robustness of downstream tasks.
  10. Robust graph neural networks, on the other hand, are designed at the model level to ensure the robustness of the model during the training process.
  11. Robust graph neural network architecture search aims to search for a robust graph neural network architecture.
  12. Each of these three types of graph robustness algorithms will be described in the following sections.
  13. Robust Graph Feature Engineering
  14. -----------
  15. Robust Graph Neural Networks
  16. -----------
  17. Robust Graph Neural Architecture Search
  18. ---------------------------------------
  19. Robust Graph Neural Architecture Search aims to search for adversarial robust Graph Neural Networks under attack.
  20. In AutoGL, this module is the code realization of G-RNA.
  21. Specifically, we design a robust search space for the message-passing mechanism by adding the adjacency mask operations into the search space,
  22. which is inspired by various defensive operators and allows us to search for defensive GNNs.
  23. Furthermore, we define a robustness metric to guide the search procedure, which helps to filter robust architectures.
  24. G-RNA allows us to effectively search for optimal robust GNNs and understand GNN robustness from an architectural perspective.
  25. Adjacency Mask Operations
  26. >>>>>>>>>>>>>>>>>>>>>>>>>
  27. Inspired by the success of current defensive approaches, we conclude the properties of operations on graph structure for robustness and
  28. design representative defensive operators in our search space accordingly.
  29. This way, we can choose the most appropriate defensive strategies when confronting perturbed graphs.
  30. To our knowledge, this is the first time the search space to be designed with a specific purpose to enhance the robustness of GNNs.
  31. Specifically, we include five mask operations in the search space.
  32. - Identity keeps the same adjacency matrix as previous layer
  33. - Low Rank Approximation (LRA) reconstructs the adjacency matrix from the top-k components of singular value decomposition.
  34. - Node Feature Similarity (NFS) deletes edges that have small jaccard similarities among node features.
  35. - Neighbor Importance Estimation (NIE) updates mask values with a pruning strategy base on quantifying the relevance among nodes.
  36. - Variable Power Operator (VPO) forms a variable power graph from the original adjacency matrix weighted by the parameters of influence strengths.
  37. Measuring Robustness
  38. >>>>>>>>>>>>>>>>>>>>
  39. Intuitively, the performance of a robust GNN should not deteriorate too much when confronting various perturbed
  40. graph data.
  41. we use KL distance to measure the prediction difference between clean and perturbed data.
  42. A larger robustness score indicates a smaller distance between the prediction of clean data and the perturbed data, and consequently, more robust GNN architectures.
  43. Robust Neural Architecture search framework for GNNs: G-RNA
  44. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  45. G-RNA is able to search for robust Graph Neural Networks based on clean graph data and gain high robustness on perturbed data for searched architectures.
  46. Specifically, G-RNA designs a robust search space for the message-passing mechanism by adding the adjacency matrix mask operations into the search space,
  47. which comprises various defensive operation candidates and allows us to search for defensive GNNs.
  48. Furthermore, it defines a robustness metric to guide the search procedure, which helps to filter robust architectures.
  49. In this way, G-RNA helps understand GNN robustness from an architectural perspective and effectively searches for optimal adversarial robust GNNs.
  50. Here is an example of G-RNA's implementation.
  51. First, set autogl backend and load the dataset.
  52. .. code-block:: python
  53. # set autogl-backend
  54. import os
  55. os.environ["AUTOGL_BACKEND"] = "pyg"
  56. # load dataset
  57. from autogl.datasets import build_dataset_from_name
  58. dataset = build_dataset_from_name('Cora', path='./')
  59. Then, you could define your own GRNA space and GRNA estimator.
  60. .. code-block:: python
  61. from autogl.module.nas.space import GRNASpace
  62. from autogl.module.nas.estimator import GRNAEstimator
  63. from autogl.module.nas.algorithm import GRNA
  64. space = GRNASpace(
  65. dropout=0.6,
  66. input_dim = dataset[0].x.size(1),
  67. output_dim = dataset[0].y.max().item()+1,
  68. ops = ['gcn', "gat_2"],
  69. rob_ops = ["identity","svd","jaccard","gnnguard"], # graph structure mask operation
  70. act_ops = ['relu','elu','leaky_relu','tanh']
  71. )
  72. estimator = GRNAEstimator(
  73. lambda_=0.05,
  74. perturb_type='random',
  75. adv_sample_num=10,
  76. dis_type='ce',
  77. ptbr=0.05
  78. )
  79. algorithm = GRNA(
  80. n_warmup=1000,
  81. population_size=100,
  82. sample_size=50,
  83. cycles=5000,
  84. mutation_prob=0.05,
  85. )
  86. Or, you could simply use GRNA's default parameters.
  87. .. code-block:: python
  88. from autogl.solver import AutoNodeClassifier
  89. solver = AutoNodeClassifier(
  90. graph_models = (),
  91. ensemble_module = None,
  92. hpo_module = None,
  93. nas_spaces=['grnaspace'],
  94. nas_algorithms=['grna'],
  95. nas_estimators=['grna']
  96. )
  97. Next, search for best robust architecture.
  98. .. code-block:: python
  99. device = 'cuda'
  100. solver.fit(dataset)
  101. solver.get_leaderboard().show()
  102. orig_acc = solver.evaluate(metric="acc")
  103. trainer = solver.graph_model_list[0]
  104. trainer.device = device
  105. After getting the best architecture, we could evaluate on clean/perturbed graph data.
  106. .. code-block:: python
  107. def metattack(data):
  108. print('Meta-attack...')
  109. adj, features, labels = to_scipy_sparse_matrix(data.edge_index, num_nodes=data.num_nodes), data.x.numpy(), data.y.numpy()
  110. idx = np.arange(data.num_nodes)
  111. idx_train, idx_val, idx_test = idx[data.train_mask], idx[data.val_mask], idx[data.test_mask]
  112. idx_unlabeled = np.union1d(idx_val, idx_test)
  113. # Setup Surrogate model
  114. surrogate = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1,
  115. nhid=16, dropout=0, with_relu=False, with_bias=False, device=device).to(device)
  116. surrogate.fit(features, adj, labels, idx_train, idx_val, patience=30)
  117. # Setup Attack Model
  118. model = Metattack(surrogate, nnodes=adj.shape[0], feature_shape=features.shape,
  119. attack_structure=True, attack_features=False, device=device, lambda_=0).to(device)
  120. # Attack
  121. n_perturbations = int(data.edge_index.size(1)/2 * 0.05)
  122. n_perturbations = 1
  123. model.attack(features, adj, labels, idx_train, idx_unlabeled, n_perturbations=n_perturbations, ll_constraint=False)
  124. perturbed_adj = model.modified_adj
  125. perturbed_data = data.clone()
  126. perturbed_data.edge_index = torch.LongTensor(perturbed_adj.nonzero().T)
  127. return perturbed_data
  128. from autogl.solver.utils import set_seed
  129. def test_from_data(trainer, dataset):
  130. set_seed(0)
  131. trainer.train(dataset)
  132. acc = trainer.evaluate(dataset, mask='test')
  133. return acc
  134. ## test searched model on clean data
  135. acc = test_from_data(trainer, dataset)
  136. ## test searched model on perturbed data
  137. data = dataset[0].cpu()
  138. dataset[0] = metattack(data).to(device)
  139. ptb_acc = test_from_data(trainer, dataset)