Yaning1001 commited on
Commit
b576cf5
·
verified ·
1 Parent(s): 4113c4d

Add files using upload-large-folder tool

Browse files
docs/make.bat ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @ECHO OFF
2
+
3
+ pushd %~dp0
4
+
5
+ REM Command file for Sphinx documentation
6
+
7
+ if "%SPHINXBUILD%" == "" (
8
+ set SPHINXBUILD=sphinx-build
9
+ )
10
+ set SOURCEDIR=.
11
+ set BUILDDIR=_build
12
+
13
+ if "%1" == "" goto help
14
+
15
+ %SPHINXBUILD% >NUL 2>NUL
16
+ if errorlevel 9009 (
17
+ echo.
18
+ echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19
+ echo.installed, then set the SPHINXBUILD environment variable to point
20
+ echo.to the full path of the 'sphinx-build' executable. Alternatively you
21
+ echo.may add the Sphinx directory to PATH.
22
+ echo.
23
+ echo.If you don't have Sphinx installed, grab it from
24
+ echo.http://sphinx-doc.org/
25
+ exit /b 1
26
+ )
27
+
28
+ %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29
+ goto end
30
+
31
+ :help
32
+ %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33
+
34
+ :end
35
+ popd
docs/source/deeprobust.graph.data.rst ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.graph.data package
2
+ =============================
3
+
4
+ Submodules
5
+ ----------
6
+
7
+ deeprobust.graph.data.attacked\_data module
8
+ -------------------------------------------
9
+
10
+ .. automodule:: deeprobust.graph.data.attacked_data
11
+ :members:
12
+
13
+ deeprobust.graph.data.dataset module
14
+ ------------------------------------
15
+
16
+ .. automodule:: deeprobust.graph.data.dataset
17
+ :members:
18
+
19
+
20
+ Module contents
21
+ ---------------
22
+
23
+ .. automodule:: deeprobust.graph.data
24
+ :members:
docs/source/deeprobust.graph.rl.rst ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.graph.rl package
2
+ ===========================
3
+
4
+ Submodules
5
+ ----------
6
+
7
+ deeprobust.graph.rl.env module
8
+ ------------------------------
9
+
10
+ .. automodule:: deeprobust.graph.rl.env
11
+ :members:
12
+
13
+ deeprobust.graph.rl.nipa\_config module
14
+ ---------------------------------------
15
+
16
+ .. automodule:: deeprobust.graph.rl.nipa_config
17
+ :members:
18
+
19
+ deeprobust.graph.rl.nipa\_env module
20
+ ------------------------------------
21
+
22
+ .. automodule:: deeprobust.graph.rl.nipa_env
23
+ :members:
24
+
25
+ deeprobust.graph.rl.nipa\_nstep\_replay\_mem module
26
+ ---------------------------------------------------
27
+
28
+ .. automodule:: deeprobust.graph.rl.nipa_nstep_replay_mem
29
+ :members:
30
+
31
+ deeprobust.graph.rl.nipa\_q\_net\_node module
32
+ ---------------------------------------------
33
+
34
+ .. automodule:: deeprobust.graph.rl.nipa_q_net_node
35
+ :members:
36
+
37
+ deeprobust.graph.rl.nstep\_replay\_mem module
38
+ ---------------------------------------------
39
+
40
+ .. automodule:: deeprobust.graph.rl.nstep_replay_mem
41
+ :members:
42
+
43
+ deeprobust.graph.rl.q\_net\_node module
44
+ ---------------------------------------
45
+
46
+ .. automodule:: deeprobust.graph.rl.q_net_node
47
+ :members:
48
+
49
+ deeprobust.graph.rl.rl\_s2v\_config module
50
+ ------------------------------------------
51
+
52
+ .. automodule:: deeprobust.graph.rl.rl_s2v_config
53
+ :members:
54
+
55
+ deeprobust.graph.rl.rl\_s2v\_env module
56
+ ---------------------------------------
57
+
58
+ .. automodule:: deeprobust.graph.rl.rl_s2v_env
59
+ :members:
60
+
61
+
62
+ Module contents
63
+ ---------------
64
+
65
+ .. automodule:: deeprobust.graph.rl
66
+ :members:
docs/source/deeprobust.graph.targeted_attack.rst ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.graph.targeted\_attack package
2
+ =========================================
3
+
4
+ Submodules
5
+ ----------
6
+
7
+ deeprobust.graph.targeted\_attack.base\_attack module
8
+ -----------------------------------------------------
9
+
10
+ .. automodule:: deeprobust.graph.targeted_attack.base_attack
11
+ :members:
12
+
13
+ deeprobust.graph.targeted\_attack.fga module
14
+ --------------------------------------------
15
+
16
+ .. automodule:: deeprobust.graph.targeted_attack.fga
17
+ :members:
18
+
19
+ deeprobust.graph.targeted\_attack.ig\_attack module
20
+ ---------------------------------------------------
21
+
22
+ .. automodule:: deeprobust.graph.targeted_attack.ig_attack
23
+ :members:
24
+
25
+ deeprobust.graph.targeted\_attack.nettack module
26
+ ------------------------------------------------
27
+
28
+ .. automodule:: deeprobust.graph.targeted_attack.nettack
29
+ :members:
30
+
31
+ deeprobust.graph.targeted\_attack.rl\_s2v module
32
+ ------------------------------------------------
33
+
34
+ .. automodule:: deeprobust.graph.targeted_attack.rl_s2v
35
+ :members:
36
+
37
+ deeprobust.graph.targeted\_attack.rnd module
38
+ --------------------------------------------
39
+
40
+ .. automodule:: deeprobust.graph.targeted_attack.rnd
41
+ :members:
42
+
43
+
44
+ deeprobust.graph.targeted\_attack.sga module
45
+ --------------------------------------------
46
+
47
+ .. automodule:: deeprobust.graph.targeted_attack.sga
48
+ :members:
49
+
50
+ Module contents
51
+ ---------------
52
+
53
+ .. automodule:: deeprobust.graph.targeted_attack
54
+ :members:
docs/source/deeprobust.image.attack.rst ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.image.attack package
2
+ ===============================
3
+
4
+ Submodules
5
+ ----------
6
+
7
+ deeprobust.image.attack.BPDA module
8
+ -----------------------------------
9
+
10
+ .. automodule:: deeprobust.image.attack.BPDA
11
+ :members:
12
+
13
+ deeprobust.image.attack.Nattack module
14
+ --------------------------------------
15
+
16
+ .. automodule:: deeprobust.image.attack.Nattack
17
+ :members:
18
+
19
+ deeprobust.image.attack.Universal module
20
+ ----------------------------------------
21
+
22
+ .. automodule:: deeprobust.image.attack.Universal
23
+ :members:
24
+
25
+ deeprobust.image.attack.YOPOpgd module
26
+ --------------------------------------
27
+
28
+ .. automodule:: deeprobust.image.attack.YOPOpgd
29
+ :members:
30
+
31
+ deeprobust.image.attack.base\_attack module
32
+ -------------------------------------------
33
+
34
+ .. automodule:: deeprobust.image.attack.base_attack
35
+ :members:
36
+
37
+ deeprobust.image.attack.cw module
38
+ ---------------------------------
39
+
40
+ .. automodule:: deeprobust.image.attack.cw
41
+ :members:
42
+
43
+ deeprobust.image.attack.deepfool module
44
+ ---------------------------------------
45
+
46
+ .. automodule:: deeprobust.image.attack.deepfool
47
+ :members:
48
+
49
+ deeprobust.image.attack.fgsm module
50
+ -----------------------------------
51
+
52
+ .. automodule:: deeprobust.image.attack.fgsm
53
+ :members:
54
+
55
+ deeprobust.image.attack.l2\_attack module
56
+ -----------------------------------------
57
+
58
+ .. automodule:: deeprobust.image.attack.l2_attack
59
+ :members:
60
+
61
+ deeprobust.image.attack.lbfgs module
62
+ ------------------------------------
63
+
64
+ .. automodule:: deeprobust.image.attack.lbfgs
65
+ :members:
66
+
67
+ deeprobust.image.attack.onepixel module
68
+ ---------------------------------------
69
+
70
+ .. automodule:: deeprobust.image.attack.onepixel
71
+ :members:
72
+
73
+ deeprobust.image.attack.pgd module
74
+ ----------------------------------
75
+
76
+ .. automodule:: deeprobust.image.attack.pgd
77
+ :members:
78
+
79
+
80
+ Module contents
81
+ ---------------
82
+
83
+ .. automodule:: deeprobust.image.attack
84
+ :members:
docs/source/deeprobust.image.defense.rst ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.image.defense package
2
+ ================================
3
+
4
+ Submodules
5
+ ----------
6
+
7
+ deeprobust.image.defense.LIDclassifier module
8
+ ---------------------------------------------
9
+
10
+ .. automodule:: deeprobust.image.defense.LIDclassifier
11
+ :members:
12
+
13
+ deeprobust.image.defense.TherEncoding module
14
+ --------------------------------------------
15
+
16
+ .. automodule:: deeprobust.image.defense.TherEncoding
17
+ :members:
18
+
19
+ deeprobust.image.defense.YOPO module
20
+ ------------------------------------
21
+
22
+ .. automodule:: deeprobust.image.defense.YOPO
23
+ :members:
24
+
25
+ deeprobust.image.defense.base\_defense module
26
+ ---------------------------------------------
27
+
28
+ .. automodule:: deeprobust.image.defense.base_defense
29
+ :members:
30
+
31
+ deeprobust.image.defense.fast module
32
+ ------------------------------------
33
+
34
+ .. automodule:: deeprobust.image.defense.fast
35
+ :members:
36
+
37
+ deeprobust.image.defense.fgsmtraining module
38
+ --------------------------------------------
39
+
40
+ .. automodule:: deeprobust.image.defense.fgsmtraining
41
+ :members:
42
+
43
+ deeprobust.image.defense.pgdtraining module
44
+ -------------------------------------------
45
+
46
+ .. automodule:: deeprobust.image.defense.pgdtraining
47
+ :members:
48
+
49
+ deeprobust.image.defense.trades module
50
+ --------------------------------------
51
+
52
+ .. automodule:: deeprobust.image.defense.trades
53
+ :members:
54
+
55
+
56
+ Module contents
57
+ ---------------
58
+
59
+ .. automodule:: deeprobust.image.defense
60
+ :members:
docs/source/deeprobust.image.netmodels.rst ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.image.netmodels package
2
+ ==================================
3
+
4
+ Submodules
5
+ ----------
6
+
7
+ deeprobust.image.netmodels.CNN module
8
+ -------------------------------------
9
+
10
+ .. automodule:: deeprobust.image.netmodels.CNN
11
+ :members:
12
+
13
+ deeprobust.image.netmodels.CNN\_multilayer module
14
+ -------------------------------------------------
15
+
16
+ .. automodule:: deeprobust.image.netmodels.CNN_multilayer
17
+ :members:
18
+
19
+ deeprobust.image.netmodels.YOPOCNN module
20
+ -----------------------------------------
21
+
22
+ .. automodule:: deeprobust.image.netmodels.YOPOCNN
23
+ :members:
24
+
25
+ deeprobust.image.netmodels.densenet module
26
+ ------------------------------------------
27
+
28
+ .. automodule:: deeprobust.image.netmodels.densenet
29
+ :members:
30
+
31
+ deeprobust.image.netmodels.preact\_resnet module
32
+ ------------------------------------------------
33
+
34
+ .. automodule:: deeprobust.image.netmodels.preact_resnet
35
+ :members:
36
+
37
+ deeprobust.image.netmodels.resnet module
38
+ ----------------------------------------
39
+
40
+ .. automodule:: deeprobust.image.netmodels.resnet
41
+ :members:
42
+
43
+ deeprobust.image.netmodels.train\_model module
44
+ ----------------------------------------------
45
+
46
+ .. automodule:: deeprobust.image.netmodels.train_model
47
+ :members:
48
+
49
+ deeprobust.image.netmodels.train\_resnet module
50
+ -----------------------------------------------
51
+
52
+ .. automodule:: deeprobust.image.netmodels.train_resnet
53
+ :members:
54
+
55
+ deeprobust.image.netmodels.vgg module
56
+ -------------------------------------
57
+
58
+ .. automodule:: deeprobust.image.netmodels.vgg
59
+ :members:
60
+
61
+
62
+ Module contents
63
+ ---------------
64
+
65
+ .. automodule:: deeprobust.image.netmodels
66
+ :members:
docs/source/deeprobust.image.rst ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ deeprobust.image package
2
+ ========================
3
+
4
+ Subpackages
5
+ -----------
6
+
7
+ .. toctree::
8
+
9
+ deeprobust.image.attack
10
+ deeprobust.image.defense
11
+ deeprobust.image.netmodels
12
+
13
+ Submodules
14
+ ----------
15
+
16
+ deeprobust.image.config module
17
+ ------------------------------
18
+
19
+ .. automodule:: deeprobust.image.config
20
+ :members:
21
+
22
+ deeprobust.image.evaluation\_attack module
23
+ ------------------------------------------
24
+
25
+ .. automodule:: deeprobust.image.evaluation_attack
26
+ :members:
27
+
28
+ deeprobust.image.optimizer module
29
+ ---------------------------------
30
+
31
+ .. automodule:: deeprobust.image.optimizer
32
+ :members:
33
+
34
+ deeprobust.image.utils module
35
+ -----------------------------
36
+
37
+ .. automodule:: deeprobust.image.utils
38
+ :members:
39
+
40
+
41
+ Module contents
42
+ ---------------
43
+
44
+ .. automodule:: deeprobust.image
45
+ :members:
docs/source/modules.rst ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ deeprobust
2
+ ==========
3
+
4
+ .. toctree::
5
+ :maxdepth: 4
6
+
7
+ deeprobust
examples/graph/cgscore_experiments/attack_method/attack_dice.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import torch.nn.functional as F
4
+ import torch.optim as optim
5
+ from deeprobust.graph.defense import GCN
6
+ from deeprobust.graph.global_attack import DICE
7
+ from deeprobust.graph.utils import *
8
+ from deeprobust.graph.data import Dataset
9
+ import os
10
+
11
+ import argparse
12
+
13
+ parser = argparse.ArgumentParser()
14
+ parser.add_argument('--seed', type=int, default=15, help='Random seed.')
15
+ parser.add_argument('--dataset', type=str, default='Flickr', choices=['cora', 'cora_ml', 'citeseer', 'polblogs', 'pubmed', 'Flickr'], help='dataset')
16
+ parser.add_argument('--ptb_rate', type=float, default=0.25, help='pertubation rate')
17
+ parser.add_argument('--ptb_type', type=str, default='dice', choices=['clean', 'meta', 'dice', 'minmax', 'pgd', 'random'], help='attack type')
18
+
19
+
20
+ args = parser.parse_args()
21
+ args.cuda = torch.cuda.is_available()
22
+ print('cuda: %s' % args.cuda)
23
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
24
+
25
+ np.random.seed(args.seed)
26
+ torch.manual_seed(args.seed)
27
+ if args.cuda:
28
+ torch.cuda.manual_seed(args.seed)
29
+
30
+ # data = Dataset(root='/tmp/', name=args.dataset, setting='prognn')
31
+ data = Dataset(root='/tmp/', name=args.dataset)
32
+ adj, features, labels = data.adj, data.features, data.labels
33
+ idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
34
+ idx_unlabeled = np.union1d(idx_val, idx_test)
35
+
36
+ # Setup Attack Model
37
+ model = DICE()
38
+
39
+ n_perturbations = int(args.ptb_rate * (adj.sum()//2))
40
+
41
+ model.attack(adj, labels, n_perturbations)
42
+ modified_adj = model.modified_adj
43
+
44
+ adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False, sparse=True, device=device)
45
+
46
+ modified_adj = normalize_adj(modified_adj)
47
+ modified_adj = sparse_mx_to_torch_sparse_tensor(modified_adj)
48
+ modified_adj = modified_adj.to(device)
49
+
50
+ def test(adj):
51
+ ''' test on GCN '''
52
+ # adj = normalize_adj_tensor(adj)
53
+ gcn = GCN(nfeat=features.shape[1],
54
+ nhid=16,
55
+ nclass=labels.max().item() + 1,
56
+ dropout=0.5, device=device)
57
+
58
+ gcn = gcn.to(device)
59
+
60
+ optimizer = optim.Adam(gcn.parameters(),
61
+ lr=0.01, weight_decay=5e-4)
62
+
63
+ gcn.fit(features, adj, labels, idx_train) # train without model picking
64
+ # gcn.fit(features, adj, labels, idx_train, idx_val) # train with validation model picking
65
+ output = gcn.output
66
+ loss_test = F.nll_loss(output[idx_test], labels[idx_test])
67
+ acc_test = accuracy(output[idx_test], labels[idx_test])
68
+ print("Test set results:",
69
+ "loss= {:.4f}".format(loss_test.item()),
70
+ "accuracy= {:.4f}".format(acc_test.item()))
71
+
72
+ return acc_test.item()
73
+
74
+ def main():
75
+ print('=== testing GCN on original(clean) graph ===')
76
+ test(adj)
77
+
78
+ save_dir = f"../attacked_adj/{args.dataset}"
79
+ os.makedirs(save_dir, exist_ok=True)
80
+
81
+ modified_adj_save = modified_adj.to_dense()
82
+ save_path = os.path.join(save_dir, f"{args.ptb_type}_{args.dataset}_{args.ptb_rate}.pt")
83
+ torch.save(modified_adj_save, save_path)
84
+
85
+ print('=== testing GCN on perturbed graph ===')
86
+ test(modified_adj)
87
+
88
+ if __name__ == '__main__':
89
+ main()
90
+
examples/graph/cgscore_experiments/attack_method/attack_meta.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import torch.nn.functional as F
4
+ import torch.optim as optim
5
+ from deeprobust.graph.defense import GCN
6
+ from deeprobust.graph.global_attack import MetaApprox, Metattack
7
+ from deeprobust.graph.utils import *
8
+ from deeprobust.graph.data import Dataset
9
+ import argparse
10
+ import os
11
+
12
+ parser = argparse.ArgumentParser()
13
+ parser.add_argument('--no-cuda', action='store_true', default=False,
14
+ help='Disables CUDA training.')
15
+ parser.add_argument('--seed', type=int, default=15, help='Random seed.')
16
+ parser.add_argument('--epochs', type=int, default=200,
17
+ help='Number of epochs to train.')
18
+ parser.add_argument('--lr', type=float, default=0.01,
19
+ help='Initial learning rate.')
20
+ parser.add_argument('--weight_decay', type=float, default=5e-4,
21
+ help='Weight decay (L2 loss on parameters).')
22
+ parser.add_argument('--hidden', type=int, default=16,
23
+ help='Number of hidden units.')
24
+ parser.add_argument('--dropout', type=float, default=0.5,
25
+ help='Dropout rate (1 - keep probability).')
26
+ parser.add_argument('--dataset', type=str, default='Flickr', choices=['cora', 'cora_ml', 'citeseer', 'polblogs', 'pubmed', 'Flickr'], help='dataset')
27
+
28
+ parser.add_argument('--ptb_type', type=str, default='meta', choices=['clean', 'meta', 'dice', 'minmax', 'pgd', 'random'], help='attack type')
29
+ parser.add_argument('--ptb_rate', type=float, default=0.25, help='pertubation rate')
30
+ parser.add_argument('--model', type=str, default='Meta-Self',
31
+ choices=['Meta-Self', 'A-Meta-Self', 'Meta-Train', 'A-Meta-Train'], help='model variant')
32
+
33
+ args = parser.parse_args()
34
+
35
+ device = torch.device("cuda:5" if torch.cuda.is_available() else "cpu")
36
+ np.random.seed(args.seed)
37
+ torch.manual_seed(args.seed)
38
+ if device != 'cpu':
39
+ torch.cuda.manual_seed(args.seed)
40
+
41
+
42
+ # cora, citeseer, polglogs, pubmed, flickr
43
+ # data = Dataset(root='/tmp/', name=args.dataset, setting='prognn')
44
+ data = Dataset(root='/tmp/', name=args.dataset)
45
+ adj, features, labels = data.adj, data.features, data.labels
46
+ idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
47
+ idx_unlabeled = np.union1d(idx_val, idx_test)
48
+
49
+ perturbations = int(args.ptb_rate * (adj.sum()//2))
50
+ adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False)
51
+
52
+ # Setup Surrogate Model
53
+ surrogate = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1, nhid=16,
54
+ dropout=0.5, with_relu=False, with_bias=True, weight_decay=5e-4, device=device)
55
+
56
+ surrogate = surrogate.to(device)
57
+ surrogate.fit(features, adj, labels, idx_train)
58
+
59
+ # Setup Attack Model
60
+ if 'Self' in args.model:
61
+ lambda_ = 0
62
+ if 'Train' in args.model:
63
+ lambda_ = 1
64
+ if 'Both' in args.model:
65
+ lambda_ = 0.5
66
+
67
+ if 'A' in args.model:
68
+ model = MetaApprox(model=surrogate, nnodes=adj.shape[0], feature_shape=features.shape, attack_structure=True, attack_features=False, device=device, lambda_=lambda_)
69
+
70
+ else:
71
+ model = Metattack(model=surrogate, nnodes=adj.shape[0], feature_shape=features.shape, attack_structure=True, attack_features=False, device=device, lambda_=lambda_)
72
+
73
+ model = model.to(device)
74
+
75
+ def test(adj):
76
+ ''' test on GCN '''
77
+
78
+ # adj = normalize_adj_tensor(adj)
79
+ gcn = GCN(nfeat=features.shape[1],
80
+ nhid=args.hidden,
81
+ nclass=labels.max().item() + 1,
82
+ dropout=args.dropout, device=device)
83
+ gcn = gcn.to(device)
84
+ gcn.fit(features, adj, labels, idx_train) # train without model picking
85
+ # gcn.fit(features, adj, labels, idx_train, idx_val) # train with validation model picking
86
+ output = gcn.output.cpu()
87
+ loss_test = F.nll_loss(output[idx_test], labels[idx_test])
88
+ acc_test = accuracy(output[idx_test], labels[idx_test])
89
+ print("Test set results:",
90
+ "loss= {:.4f}".format(loss_test.item()),
91
+ "accuracy= {:.4f}".format(acc_test.item()))
92
+
93
+ return acc_test.item()
94
+
95
+
96
+ def main():
97
+
98
+ model.attack(features, adj, labels, idx_train, idx_unlabeled, perturbations, ll_constraint=False)
99
+ print('=== testing GCN on original(clean) graph ===')
100
+ test(adj)
101
+ modified_adj = model.modified_adj
102
+
103
+ save_dir = f"../attacked_adj/{args.dataset}"
104
+ os.makedirs(save_dir, exist_ok=True)
105
+
106
+ save_path = os.path.join(save_dir, f"{args.ptb_type}_{args.dataset}_{args.ptb_rate}.pt")
107
+ torch.save(modified_adj, save_path)
108
+
109
+ # modified_features = model.modified_features
110
+ test(modified_adj)
111
+
112
+ # # if you want to save the modified adj/features, uncomment the code below
113
+ # model.save_adj(root='./', name=f'mod_adj')
114
+ # model.save_features(root='./', name='mod_features')
115
+
116
+ if __name__ == '__main__':
117
+ main()
118
+
examples/graph/cgscore_experiments/attack_method/attack_pgd.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import torch.nn.functional as F
4
+ import torch.optim as optim
5
+ from deeprobust.graph.defense import GCN, GAT
6
+ from deeprobust.graph.global_attack import MinMax, PGDAttack
7
+ from deeprobust.graph.utils import *
8
+ from deeprobust.graph.data import Dataset, Dpr2Pyg
9
+ import argparse
10
+ import os
11
+
12
+ parser = argparse.ArgumentParser()
13
+ parser.add_argument('--seed', type=int, default=15, help='Random seed.')
14
+ parser.add_argument('--epochs', type=int, default=25,
15
+ help='Number of epochs to train.')
16
+ parser.add_argument('--lr', type=float, default=0.01,
17
+ help='Initial learning rate.')
18
+ parser.add_argument('--weight_decay', type=float, default=5e-4,
19
+ help='Weight decay (L2 loss on parameters).')
20
+ parser.add_argument('--hidden', type=int, default=16,
21
+ help='Number of hidden units.')
22
+ parser.add_argument('--dropout', type=float, default=0.5,
23
+ help='Dropout rate (1 - keep probability).')
24
+
25
+ parser.add_argument('--dataset', type=str, default='Flickr', choices=['cora', 'cora_ml', 'citeseer', 'polblogs', 'pubmed', 'Flickr'], help='dataset')
26
+ parser.add_argument('--ptb_rate', type=float, default=0.25, help='pertubation rate')
27
+ parser.add_argument('--ptb_type', type=str, default='pgd', choices=['clean', 'meta', 'dice', 'minmax', 'pgd', 'random'], help='attack type')
28
+ parser.add_argument('--model', type=str, default='PGD', choices=['PGD', 'min-max'], help='model variant')
29
+
30
+ args = parser.parse_args()
31
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
32
+
33
+ np.random.seed(args.seed)
34
+ torch.manual_seed(args.seed)
35
+ if device != 'cpu':
36
+ torch.cuda.manual_seed(args.seed)
37
+
38
+ # data = Dataset(root='/tmp/', name=args.dataset, setting='prognn')
39
+ data = Dataset(root='/tmp/', name=args.dataset)
40
+ adj, features, labels = data.adj, data.features, data.labels
41
+
42
+ features = normalize_feature(features.toarray())
43
+
44
+ idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
45
+ adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False)
46
+
47
+ perturbations = int(args.ptb_rate * (adj.sum()//2))
48
+ # Setup Victim Model
49
+ victim_model = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1, nhid=16,
50
+ dropout=0.5, weight_decay=5e-4, device=device)
51
+
52
+
53
+ victim_model = victim_model.to(device)
54
+ victim_model.fit(features, adj, labels, idx_train)
55
+
56
+ # Setup Attack Model
57
+ model = PGDAttack(model=victim_model, nnodes=adj.shape[0], loss_type='CE', device=device)
58
+
59
+ model = model.to(device)
60
+
61
+ def test(adj):
62
+ ''' test on GCN '''
63
+
64
+
65
+ # adj = normalize_adj_tensor(adj)
66
+ gcn = GCN(nfeat=features.shape[1],
67
+ nhid=args.hidden,
68
+ nclass=labels.max().item() + 1,
69
+ dropout=args.dropout, device=device)
70
+ gcn = gcn.to(device)
71
+ gcn.fit(features, adj, labels, idx_train) # train without model picking
72
+ # gcn.fit(features, adj, labels, idx_train, idx_val) # train with validation model picking
73
+ output = gcn.output.cpu()
74
+ loss_test = F.nll_loss(output[idx_test], labels[idx_test])
75
+ acc_test = accuracy(output[idx_test], labels[idx_test])
76
+ print("Test set results:",
77
+ "loss= {:.4f}".format(loss_test.item()),
78
+ "accuracy= {:.4f}".format(acc_test.item()))
79
+
80
+ return acc_test.item()
81
+
82
+ def test_gat(adj):
83
+
84
+ perturbed_adj = sp.csr_matrix(adj.cpu().numpy())
85
+ gat = GAT(nfeat=features.shape[1],
86
+ nhid=8, heads=8,
87
+ nclass=labels.max().item() + 1,
88
+ dropout=0.5, device=device)
89
+ gat = gat.to(device)
90
+
91
+ # test on clean graph
92
+ print('==================')
93
+ print('=== train on clean graph ===')
94
+
95
+ pyg_data = Dpr2Pyg(data)
96
+ pyg_data.update_edge_index(perturbed_adj) # inplace operation
97
+ gat.fit(pyg_data, train_iters=200, verbose=True) # train with earlystopping
98
+ gat.test()
99
+
100
+
101
+ def main():
102
+ model.attack(features, adj, labels, idx_train, perturbations, epochs=args.epochs)
103
+ print('=== testing GCN on original(clean) graph ===')
104
+ test(adj)
105
+ modified_adj = model.modified_adj
106
+ # modified_features = model.modified_features
107
+ print(modified_adj)
108
+ print(type(modified_adj))
109
+ save_dir = f"../attacked_adj/{args.dataset}"
110
+ os.makedirs(save_dir, exist_ok=True)
111
+
112
+ save_path = os.path.join(save_dir, f"{args.ptb_type}_{args.dataset}_{args.ptb_rate}.pt")
113
+ torch.save(modified_adj, save_path)
114
+
115
+ test(modified_adj)
116
+ test_gat(modified_adj)
117
+
118
+ # # if you want to save the modified adj/features, uncomment the code below
119
+ # model.save_adj(root='./', name=f'mod_adj')
120
+ # model.save_features(root='./', name='mod_features')
121
+
122
+ if __name__ == '__main__':
123
+ main()
124
+
examples/graph/cgscore_experiments/attack_method/attack_random.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import torch.nn.functional as F
4
+ import torch.optim as optim
5
+ from deeprobust.graph.defense import GCN
6
+ from deeprobust.graph.global_attack import Random
7
+ from deeprobust.graph.utils import *
8
+ from deeprobust.graph.data import Dataset
9
+ import argparse
10
+ import os
11
+
12
+ parser = argparse.ArgumentParser()
13
+ parser.add_argument('--seed', type=int, default=15, help='Random seed.')
14
+ parser.add_argument('--dataset', type=str, default='cora', choices=['cora', 'cora_ml', 'citeseer', 'polblogs', 'pubmed', 'Flickr'], help='dataset')
15
+ parser.add_argument('--ptb_rate', type=float, default=0.25, help='pertubation rate')
16
+ parser.add_argument('--ptb_type', type=str, default='random', choices=['clean', 'meta', 'dice', 'minmax', 'pgd', 'random'], help='attack type')
17
+
18
+ args = parser.parse_args()
19
+ args.cuda = torch.cuda.is_available()
20
+ print('cuda: %s' % args.cuda)
21
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
22
+
23
+ np.random.seed(args.seed)
24
+ torch.manual_seed(args.seed)
25
+ if args.cuda:
26
+ torch.cuda.manual_seed(args.seed)
27
+
28
+ # data = Dataset(root='/tmp/', name=args.dataset, setting='prognn')
29
+ data = Dataset(root='/tmp/', name=args.dataset)
30
+ adj, features, labels = data.adj, data.features, data.labels
31
+ idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
32
+ idx_unlabeled = np.union1d(idx_val, idx_test)
33
+
34
+ # Setup Attack Model
35
+ model = Random()
36
+
37
+ n_perturbations = int(args.ptb_rate * (adj.sum()//2))
38
+
39
+ model.attack(adj, n_perturbations, type='flip') # type ['add', 'remove', 'flip']
40
+ modified_adj = model.modified_adj
41
+
42
+ adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False, sparse=True)
43
+ adj = adj.to(device)
44
+ features = features.to(device)
45
+ labels = labels.to(device)
46
+
47
+ modified_adj = normalize_adj(modified_adj)
48
+ modified_adj = sparse_mx_to_torch_sparse_tensor(modified_adj)
49
+ modified_adj = modified_adj.to(device)
50
+
51
+
52
+ def test(adj):
53
+ ''' test on GCN '''
54
+ # adj = normalize_adj_tensor(adj)
55
+ gcn = GCN(nfeat=features.shape[1],
56
+ nhid=16,
57
+ nclass=labels.max().item() + 1,
58
+ dropout=0.5, device=device)
59
+
60
+ gcn = gcn.to(device)
61
+
62
+ optimizer = optim.Adam(gcn.parameters(),
63
+ lr=0.01, weight_decay=5e-4)
64
+
65
+ gcn.fit(features, adj, labels, idx_train, train_iters=200) # train without model picking
66
+ # gcn.fit(features, adj, labels, idx_train, idx_val) # train with validation model picking
67
+ output = gcn.output
68
+ loss_test = F.nll_loss(output[idx_test], labels[idx_test])
69
+ acc_test = accuracy(output[idx_test], labels[idx_test])
70
+ print("Test set results:",
71
+ "loss= {:.4f}".format(loss_test.item()),
72
+ "accuracy= {:.4f}".format(acc_test.item()))
73
+
74
+ return acc_test.item()
75
+
76
+ def main():
77
+ print('=== testing GCN on original(clean) graph ===')
78
+ test(adj)
79
+ save_dir = f"../attacked_adj/{args.dataset}"
80
+ os.makedirs(save_dir, exist_ok=True)
81
+
82
+ modified_adj_save = modified_adj.to_dense()
83
+ save_path = os.path.join(save_dir, f"{args.ptb_type}_{args.dataset}_{args.ptb_rate}.pt")
84
+ torch.save(modified_adj_save, save_path)
85
+
86
+ print('=== testing GCN on perturbed graph ===')
87
+
88
+ test(modified_adj)
89
+
90
+ if __name__ == '__main__':
91
+ main()
92
+
examples/graph/cgscore_experiments/attack_method/no_attack.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import torch.nn.functional as F
4
+ import torch.optim as optim
5
+ from deeprobust.graph.defense import GCN
6
+ from deeprobust.graph.global_attack import Random
7
+ from deeprobust.graph.utils import *
8
+ from deeprobust.graph.data import Dataset
9
+ import argparse
10
+ import os
11
+
12
+ parser = argparse.ArgumentParser()
13
+ parser.add_argument('--seed', type=int, default=15, help='Random seed.')
14
+ parser.add_argument('--dataset', type=str, default='cora', choices=['cora', 'cora_ml', 'citeseer', 'polblogs', 'pubmed', 'Flickr'], help='dataset')
15
+ parser.add_argument('--ptb_rate', type=float, default=0.00, help='pertubation rate')
16
+ parser.add_argument('--ptb_type', type=str, default='clean', choices=['clean', 'meta', 'dice', 'minmax', 'pgd', 'random'], help='attack type')
17
+
18
+ args = parser.parse_args()
19
+ args.cuda = torch.cuda.is_available()
20
+ print('cuda: %s' % args.cuda)
21
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
22
+
23
+ np.random.seed(args.seed)
24
+ torch.manual_seed(args.seed)
25
+ if args.cuda:
26
+ torch.cuda.manual_seed(args.seed)
27
+
28
+ # data = Dataset(root='/tmp/', name=args.dataset, setting='prognn')
29
+ data = Dataset(root='/tmp/', name=args.dataset)
30
+ adj, features, labels = data.adj, data.features, data.labels
31
+ idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
32
+ idx_unlabeled = np.union1d(idx_val, idx_test)
33
+
34
+
35
+ adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False, sparse=True)
36
+ adj, features, labels = adj.to(device), features.to(device), labels.to(device)
37
+ adj = adj.to_dense()
38
+
39
+ def test(adj):
40
+ ''' test on GCN '''
41
+ # adj = normalize_adj_tensor(adj)
42
+ gcn = GCN(nfeat=features.shape[1],
43
+ nhid=16,
44
+ nclass=labels.max().item() + 1,
45
+ dropout=0.5, device=device)
46
+
47
+ gcn = gcn.to(device)
48
+
49
+ optimizer = optim.Adam(gcn.parameters(),
50
+ lr=0.01, weight_decay=5e-4)
51
+
52
+ gcn.fit(features, adj, labels, idx_train, train_iters=200) # train without model picking
53
+ # gcn.fit(features, adj, labels, idx_train, idx_val) # train with validation model picking
54
+ output = gcn.output
55
+ loss_test = F.nll_loss(output[idx_test], labels[idx_test])
56
+ acc_test = accuracy(output[idx_test], labels[idx_test])
57
+ print("Test set results:",
58
+ "loss= {:.4f}".format(loss_test.item()),
59
+ "accuracy= {:.4f}".format(acc_test.item()))
60
+
61
+ return acc_test.item()
62
+
63
+ def main():
64
+
65
+
66
+ save_dir = f"../attacked_adj/{args.dataset}"
67
+ os.makedirs(save_dir, exist_ok=True)
68
+
69
+ save_path = os.path.join(save_dir, f"{args.ptb_type}_{args.dataset}_{args.ptb_rate}.pt")
70
+ torch.save(adj, save_path)
71
+
72
+ print('=== testing GCN on original(clean) graph ===')
73
+ test(adj)
74
+
75
+
76
+ if __name__ == '__main__':
77
+ main()
78
+