-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplainer.py
More file actions
56 lines (43 loc) · 1.83 KB
/
explainer.py
File metadata and controls
56 lines (43 loc) · 1.83 KB
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
import torch
import torch.nn as nn
from torch_geometric.nn import InstanceNorm
class BatchSequential(nn.Sequential):
def forward(self, inputs, batch):
for module in self._modules.values():
if isinstance(module, (InstanceNorm)):
inputs = module(inputs, batch)
else:
inputs = module(inputs)
return inputs
class MLP(BatchSequential):
def __init__(self, channels, dropout, bias=True):
m = []
for i in range(1, len(channels)):
m.append(nn.Linear(channels[i - 1], channels[i], bias))
if i < len(channels) - 1:
m.append(InstanceNorm(channels[i]))
m.append(nn.ReLU())
m.append(nn.Dropout(dropout))
super(MLP, self).__init__(*m)
class ExtractorMLP(nn.Module):
def __init__(self, hidden_size, learn_edge_att, dropout_p):
super().__init__()
self.learn_edge_att = learn_edge_att
if self.learn_edge_att:
self.feature_extractor = MLP([hidden_size * 2, hidden_size * 4, hidden_size, 1], dropout=dropout_p)
else:
self.feature_extractor = MLP([hidden_size * 1, hidden_size * 2, hidden_size, 1], dropout=dropout_p)
def forward(self, emb, edge_index, batch):
if self.learn_edge_att:
col, row = edge_index
f1, f2 = emb[col], emb[row]
f12 = torch.cat([f1, f2], dim=-1)
att_log_logits = self.feature_extractor(f12, batch[col])
else:
att_log_logits = self.feature_extractor(emb, batch)
return att_log_logits
def get_explainer(method_name, cfg):
explainer = ExtractorMLP(hidden_size=cfg['hidden_size'],
learn_edge_att=cfg['learn_edge_att'],
dropout_p=cfg['explainer_dropout_p'])
return explainer