-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
185 lines (139 loc) · 5.44 KB
/
model.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import torch
import torch.nn as nn
import torch.nn.functional as F
import dgl
import dgl.function as fn
from dgl.nn.pytorch import GraphConv
import math
from utils import idx_sample, row_normalization
class MLP(nn.Module):
def __init__(self, in_dim, out_dim, activation) -> None:
super().__init__()
# self.encoder = nn.ModuleList([
# nn.Linear(in_dim, hid_dim),
# nn.Dropout(p=dropout),
# activation,
# nn.Linear(hid_dim, out_dim),
# nn.Dropout(p=dropout)
# ])
self.encoder = nn.ModuleList([
nn.Linear(in_dim, out_dim),
activation,
])
def forward(self, features):
h = features
for layer in self.encoder:
h = layer(h)
h = F.normalize(h, p=2, dim=1) # row normalize
return h
class GCN(nn.Module):
def __init__(
self, g, in_dim, hid_dim, activation, dropout
):
super(GCN, self).__init__()
self.g = g
self.gcn = GraphConv(in_dim, hid_dim, activation=activation)
self.dropout = nn.Dropout(p=dropout)
def forward(self, features):
h = self.gcn(self.g, features)
return self.dropout(h)
class MeanAggregator(nn.Module):
def __init__(self):
super(MeanAggregator, self).__init__()
def forward(self, graph, h):
with graph.local_scope():
graph.ndata['h'] = h
graph.update_all(fn.copy_u('h', 'm'), fn.mean('m', 'neigh'))
return graph.ndata['neigh']
class Discriminator(nn.Module):
def __init__(self, hid_dim) -> None:
super().__init__()
def forward(self, features, centers):
# tmp = torch.matmul(features, self.weight)
# res = torch.sum(tmp * centers, dim=1)
# return torch.sigmoid(res)
return torch.sum(features * centers, dim=1)
class Encoder(nn.Module):
def __init__(self, graph, in_dim, out_dim, activation):
super().__init__()
self.encoder = MLP(in_dim, out_dim, activation)
# self.encoder = GCN(graph, in_dim, out_dim, activation, dropout=0.)
self.meanAgg = MeanAggregator()
self.g = graph
def forward(self, h):
h = self.encoder(h)
mean_h = self.meanAgg(self.g ,h)
return h, mean_h
class LocalModel(nn.Module):
# LIM module
def __init__(self, graph, in_dim, out_dim, activation) -> None:
super().__init__()
self.encoder = Encoder(graph, in_dim, out_dim, activation)
self.g = graph
self.discriminator = Discriminator(out_dim)
self.loss = nn.BCEWithLogitsLoss()
self.recon_loss = nn.MSELoss()
def forward(self, h):
h, mean_h = self.encoder(h)
# positive
pos = self.discriminator(h, mean_h)
# negtive
idx = torch.arange(0, h.shape[0])
neg_idx = idx_sample(idx)
neg_neigh_h = mean_h[neg_idx]
neg = self.discriminator(h, neg_neigh_h)
self.g.ndata['pos'] = pos
self.g.ndata['neg'] = neg
l1 = self.loss(pos, torch.ones_like(pos))
l2 = self.loss(neg, torch.zeros_like(neg))
return l1 + l2, l1, l2
class GlobalModel(nn.Module):
def __init__(self, graph, in_dim, out_dim, activation, nor_idx, ano_idx, center):
super().__init__()
self.g = graph
self.discriminator = Discriminator(out_dim)
self.beta = 0.9
self.neigh_weight = 1.
self.loss = nn.BCEWithLogitsLoss()
self.nor_idx = nor_idx
self.ano_idx = ano_idx
self.center = center # high confidence normal center
self.encoder = Encoder(graph, in_dim, out_dim, activation)
self.pre_attn = self.pre_attention()
def pre_attention(self):
# calculate pre-attn
msg_func = lambda edges:{'abs_diff': torch.abs(edges.src['pos'] - edges.dst['pos'])}
red_func = lambda nodes:{'pos_diff': torch.mean(nodes.mailbox['abs_diff'], dim=1)}
self.g.update_all(msg_func, red_func)
pos = self.g.ndata['pos']
pos.requires_grad = False
pos_diff = self.g.ndata['pos_diff'].detach()
diff_mean = pos_diff[self.nor_idx].mean()
diff_std = torch.sqrt(pos_diff[self.nor_idx].var())
normalized_pos = (pos_diff - diff_mean) / diff_std
attn = 1-torch.sigmoid(normalized_pos)
return attn.unsqueeze(1)
def post_attention(self, h, mean_h):
# calculate post-attn
simi = self.discriminator(h, mean_h)
return simi.unsqueeze(1)
def msg_pass(self, h, mean_h, attn):
# h+attn*mean_h
nei = attn * self.neigh_weight
h = nei*mean_h + (1-nei)*h
return h
def forward(self, feats, epoch):
h, mean_h = self.encoder(feats)
post_attn = self.post_attention(h, mean_h)
beta = math.pow(self.beta, epoch)
if beta < 0.1:
beta = 0.
attn = beta*self.pre_attn + (1-beta)*post_attn
h = self.msg_pass(h, mean_h, attn)
scores = self.discriminator(h, self.center)
pos_center_simi = scores[self.nor_idx]
neg_center_simi = scores[self.ano_idx]
pos_center_loss = self.loss(pos_center_simi, torch.ones_like(pos_center_simi, dtype=torch.float32))
neg_center_loss = self.loss(neg_center_simi, torch.zeros_like(neg_center_simi, dtype=torch.float32))
center_loss = pos_center_loss + neg_center_loss
return center_loss, scores