|
- .. _robust:
-
- 鲁棒模型
- ==========================
-
- 我们提供了一系列的防御方法,旨在增强图神经网络的鲁棒性。
-
- 生成并训练 GNNGuard 模型
- ------------------------------
-
- 首先,加载预先攻击的图数据:
-
- .. code-block:: python
-
- perturbed_data = PrePtbDataset(root='/tmp/', name=dataset,attack_method='meta', ptb_rate=0.2)
- modified_adj = perturbed_data.adj
-
- 然后,在原图 / 扰动图上训练图神经网络模型:
-
- .. code-block:: python
-
- flag = False
- print('=== testing GNN on original(clean) graph (AutoGL) ===')
- print("acc_test:",test_autogl(adj, features, device, attention=flag))
- print('=== testing GCN on perturbed graph (AutoGL) ===')
- print("acc_test:",test_autogl(modified_adj, features, device, attention=flag))
-
- 训练图神经网络的细节如下:
-
- .. code-block:: python
-
- def test_autogl(adj, features, device, attention):
- ''
- """test on GCN """
- """model_name could be 'GCN', 'GAT', 'GIN','JK' """
- accs = []
- for seed in tqdm(range(5)):
- # defense model
- gcn = AutoGNNGuard(
- num_features=pyg_data.num_node_features,
- num_classes=pyg_data.num_classes,
- device=args.device,
- init=False
- ).from_hyper_parameter(model_hp).model
- gcn = gcn.to(device)
- gcn.fit(features, adj, labels, idx_train, idx_val=idx_val,
- idx_test=idx_test,
- attention=attention, verbose=True, train_iters=81)
- gcn.eval()
- acc_test, output = gcn.test(idx_test=idx_test)
- accs.append(acc_test.item())
- mean = np.mean(accs)
- std = np.std(accs)
- return {"mean": mean, "std": std}
-
- 最后,在扰动图上训练防御模型GNNGuard
-
- .. code-block:: python
-
- flag = True
- print('=== testing GNN on original(clean) graph (AutoGL) + GNNGuard ===')
- print("acc_test:",test_autogl(adj, features, device, attention=flag))
- print('=== testing GCN on perturbed graph (AutoGL) + GNNGuard ===')
- print("acc_test:",test_autogl(modified_adj, features, device, attention=flag))
|