-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
126 lines (106 loc) · 3.68 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import torch
from torch_geometric.datasets import TUDataset
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from torch_geometric.utils import add_self_loops
import numpy as np
from sagod.utils import *
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve
from sagod.models import (
DOMINANT,
ANOMALOUS,
AnomalyDAE,
OCGNN,
ONE,
DONE,
AdONE,
ALARM,
Radar,
GAAN,
SADAG,
DeepAE,
ComGA,
ResGCN,
)
torch.manual_seed(42)
np.random.seed(42)
dataset = TUDataset('../data', 'MUTAG')
data: Data = list(DataLoader(dataset, batch_size=188, shuffle=True))[0]
data.edge_index = add_self_loops(data.edge_index)[0]
data.y = torch.zeros(data.num_nodes)
data.x[data.x == 0.] = 1e-5
data = struct_ano_injection(data, 10, 15)
data = attr_ano_injection(data, 150, 25)
verbose = True
plt.suptitle("ROC curve")
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
print("DOMINANT:")
model = DOMINANT(verbose=verbose, epoch=100, lr=0.01).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2],
label='DOMINANT')
print("ANOMALOUS:")
model = ANOMALOUS(verbose=verbose, epoch=500, phi=100.,
lr=0.01).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2],
label='ANOMALOUS')
print("AnomalyDAE:")
model = AnomalyDAE(verbose=verbose, epoch=100).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2],
label='AnomalyDAE')
print("OCGNN:")
model = OCGNN(verbose=verbose, epoch=100).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='OCGNN')
print("ONE:")
model = ONE(5, verbose=verbose, epoch=3).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='ONE')
print("DONE:")
model = DONE(6,
verbose=True,
n_hidden=[64, 16],
epoch=10,
lr=0.005,
n_layers=6).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='DONE')
print("AdONE:")
model = AdONE(4,
verbose=True,
n_hidden=[64, 16],
epoch=10,
lr=0.005,
n_layers=6).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='AdONE')
print("ALARM:")
model = ALARM([2, 2, 3], verbose=True).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='ALARM')
plt.legend()
plt.subplot(1, 2, 2)
print("GAAN:")
model = GAAN(verbose=verbose, n_noise=3, n_enc_layers=3,
n_gen_layers=3).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='GAAN')
print("Radar:")
model = Radar(verbose=verbose, alpha=100., beta=100., gamma=100.,
epoch=5).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='Radar')
print("SADAG:")
model = SADAG(verbose=True, reg=1., lr=0.005, epoch=20).fit(
data,
data.y,
mask=torch.bernoulli(torch.full_like(data.y, 0.8)),
)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='SADAG')
print("DeepAE:")
model = DeepAE(verbose=True, lr=0.005, epoch=50).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2],
label='DeepAE')
print("ComGA:")
model = ComGA(verbose=True, lr=0.01, epoch=100, embed_dim=64).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2], label='ComGA')
print("ResGCN:")
model = ResGCN(verbose=True, lr=0.01, epoch=50).fit(data, data.y)
plt.plot(*roc_curve(data.y.numpy(), model.decision_scores_)[:2],
label='ResGCN')
plt.legend()
plt.savefig("src/eval.png", dpi=1000)