-
由 openaiops 创作于8183bb6e
layers.py 10.59 KiB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import dgl
import dgl.nn.pytorch as dglnn
import torch
import torch.nn as nn
import torch.nn.functional as F
class GCNClassifier(nn.Module):
"""
两层GCN+最大池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes):
super(GCNClassifier, self).__init__()
self.conv1 = dglnn.GraphConv(in_dim, hidden_dim)
self.conv2 = dglnn.GraphConv(hidden_dim, hidden_dim)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
h = F.relu(self.conv1(g, h))
h = F.relu(self.conv2(g, h))
h = self.pool(g, h)
return self.classify(h)
# with g.local_scope():
# g.ndata['h'] = h
# hg = dgl.mean_nodes(g, 'h')
# return self.classify(hg)
class RGCNMSL(nn.Module):
"""
多任务学习:微服务组定位及故障分类"联合训练"
"""
def __init__(self, in_dim, hidden_dim, out_dim1, out_dim2, etype):
super(RGCNMSL, self).__init__()
self.etype = etype
n_rels = len(set([e.item() for e in etype]))
self.conv1 = dglnn.RelGraphConv(in_dim, hidden_dim, n_rels)
self.conv2 = dglnn.RelGraphConv(hidden_dim, hidden_dim, n_rels)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
# self.pool = dglnn.SortPooling(k=2)
self.cls1 = nn.Linear(hidden_dim, out_dim1)
self.cls2 = nn.Linear(hidden_dim, out_dim2)
def forward(self, g, h):
etype = self.etype.repeat((g.num_edges() // len(self.etype)))
h = F.relu(self.conv1(g, h, etype))
h = F.relu(self.conv2(g, h, etype))
h = self.pool(g, h)
return self.cls1(h), self.cls2(h)
def get_embeds(self , g, h):
etype = self.etype.repeat((g.num_edges() // len(self.etype)))
h = F.relu(self.conv1(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
h = F.relu(self.conv2(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
# h = self.pool(g, h)
return h
class RGCNClassifier(nn.Module):
"""
两层RGCN+最大池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes, etype):
super(RGCNClassifier, self).__init__()
self.etype = etype
n_rels = len(set([e.item() for e in etype]))
self.conv1 = dglnn.RelGraphConv(in_dim, hidden_dim, n_rels)
self.conv2 = dglnn.RelGraphConv(hidden_dim, hidden_dim, n_rels)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
# self.pool = dglnn.SortPooling(k=2)
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
etype = self.etype.repeat((g.num_edges() // len(self.etype)))
h = F.relu(self.conv1(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
h = F.relu(self.conv2(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
h = self.pool(g, h)
return self.classify(h)
# with g.local_scope():
# g.ndata['h'] = h
# hg = dgl.mean_nodes(g, 'h')
# return self.classify(hg)
def get_embeds(self , g, h, pool=False):
etype = self.etype.repeat((g.num_edges() // len(self.etype)))
h = F.relu(self.conv1(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
h = F.relu(self.conv2(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
if pool:
h = self.pool(g, h)
return h
class RGCNv2Classifier(nn.Module):
"""
两层RGCN+最大池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes, etype):
super(RGCNv2Classifier, self).__init__()
self.etype = etype
n_rels = len(set([e.item() for e in etype]))
self.conv1 = dglnn.RelGraphConv(in_dim, hidden_dim, n_rels)
self.conv2 = dglnn.RelGraphConv(hidden_dim, hidden_dim, n_rels)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.cls1 = nn.Linear(hidden_dim, n_classes)
# self.cls2 = nn.Linear(hidden_dim//2, n_classes)
def forward(self, g, h):
etype = self.etype.repeat((g.num_edges() // len(self.etype)))
h = F.relu(self.conv1(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
h = F.relu(self.conv2(g, h, etype))
# h = F.dropout(h, p=0.5, training=True)
h = self.pool(g, h)
h = self.cls1(h)
# h = self.cls2(h)
# h = self.cls3(h)
return h
class GATClassifier(nn.Module):
"""
两层GAT(第一层GAT不同注意力头的输出之间是拼接操作;第二层是求平均操作)+平均池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes, num_heads):
super(GATClassifier, self).__init__()
self.conv1 = dglnn.GATConv(in_dim, hidden_dim, num_heads=num_heads, activation=F.relu) # F.relu
self.conv2 = dglnn.GATConv(hidden_dim*num_heads, hidden_dim, num_heads=num_heads, activation=F.relu)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
h = self.conv1(g, h)
hh = torch.zeros(h.shape[0], h.shape[1]*h.shape[2])
# 中间层多头特征拼接
for i in range(h.shape[0]):
hh[i] = torch.cat([t for t in h[i]])
h = self.conv2(g, hh)
# 最后一层多头特征取平均值
h = torch.mean(h, dim=1)
h = self.pool(g, h)
return self.classify(h)
def get_embeds(self , g, h, pool=False):
h = F.relu(self.conv1(g, h))
# h = F.dropout(h, p=0.5, training=True)
h = F.relu(self.conv2(g, h))
# h = F.dropout(h, p=0.5, training=True)
if pool:
h = self.pool(g, h)
return h
class SAGEClassifier(nn.Module):
"""
两层SAGEConv+最大池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes):
super(SAGEClassifier, self).__init__()
self.conv1 = dglnn.SAGEConv(in_dim, hidden_dim, 'lstm', activation=F.relu) # mean, gcn, pool, lstm
self.conv2 = dglnn.SAGEConv(hidden_dim, hidden_dim, 'lstm', activation=F.relu)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
h = self.conv1(g, h)
h = self.conv2(g, h)
h = self.pool(g, h)
return self.classify(h)
class TAGClassifier(nn.Module):
"""
两层TAGConv+最大池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes):
super(TAGClassifier, self).__init__()
self.conv1 = dglnn.TAGConv(in_dim, hidden_dim, activation=F.relu)
self.conv2 = dglnn.TAGConv(hidden_dim, hidden_dim, activation=F.relu)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
h = self.conv1(g, h)
h = self.conv2(g, h)
h = self.pool(g, h)
return self.classify(h)
def get_embeds(self , g, h, pool=False):
h = self.conv1(g, h)
# h = F.dropout(h, p=0.5, training=True)
h = self.conv2(g, h)
# h = F.dropout(h, p=0.5, training=True)
if pool:
h = self.pool(g, h)
return h
class GATv2Classifier(nn.Module):
"""
两层GATv2+平均池化+线性分类器
"""
def __init__(self, in_dim, hidden_dim, n_classes, num_heads):
super(GATv2Classifier, self).__init__()
self.conv1 = dglnn.GATv2Conv(in_dim, hidden_dim, num_heads=num_heads, activation=F.relu) # F.relu
self.conv2 = dglnn.GATv2Conv(hidden_dim*num_heads, hidden_dim, num_heads=num_heads, activation=F.relu)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
h = self.conv1(g, h)
hh = torch.zeros(h.shape[0], h.shape[1]*h.shape[2])
# 中间层多头特征拼接
for i in range(h.shape[0]):
hh[i] = torch.cat([t for t in h[i]])
h = self.conv2(g, hh)
# 最后一层多头特征取平均值
h = torch.mean(h, dim=1)
h = self.pool(g, h)
return self.classify(h)
class LinearClassifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes):
super(LinearClassifier, self).__init__()
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
# self.linear1 = nn.Linear(in_dim, hidden_dim)
# self.linear2 = nn.Linear(hidden_dim, hidden_dim)
# self.classify = nn.Linear(hidden_dim, n_classes)
self.classify = nn.Linear(in_dim, n_classes)
def forward(self, g, h):
h = self.pool(g, h)
# h = F.relu(self.linear1(h))
# h = F.relu(self.linear2(h))
return self.classify(h)
class SGCCClassifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes):
super(SGCCClassifier, self).__init__()
self.conv1 = dglnn.SGConv(in_dim, hidden_dim)
self.conv2 = dglnn.SGConv(hidden_dim, hidden_dim)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
h = F.relu(self.conv1(g, h))
h = F.relu(self.conv2(g, h))
h = self.pool(g, h)
return self.classify(h)
class ChebClassifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes, k=2, compute_lambda_max=False):
super(ChebClassifier, self).__init__()
self.conv1 = dglnn.ChebConv(in_dim, hidden_dim, k, activation=F.relu)
self.conv2 = dglnn.ChebConv(hidden_dim, hidden_dim, k, activation=F.relu)
# self.pool = dglnn.AvgPooling()
self.pool = dglnn.MaxPooling()
self.classify = nn.Linear(hidden_dim, n_classes)
self.lambda_max = None
self.compute_lambda_max = compute_lambda_max
def forward(self, g, h):
if self.compute_lambda_max:
# self.lambda_max = dgl.laplacian_lambda_max(g)
if self.lambda_max is None:
self.lambda_max = dgl.laplacian_lambda_max(g)
else:
self.labda_max = 2
try:
h = self.conv1(g, h, self.lambda_max)
except:
self.lambda_max = dgl.laplacian_lambda_max(g)
h = self.conv1(g, h, self.lambda_max)
h = self.conv2(g, h, self.lambda_max)
h = self.pool(g, h)
return self.classify(h)