-
Notifications
You must be signed in to change notification settings - Fork 177
/
explainer_main.py
317 lines (291 loc) · 9.9 KB
/
explainer_main.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
""" explainer_main.py
Main user interface for the explainer module.
"""
import argparse
import os
import sklearn.metrics as metrics
from tensorboardX import SummaryWriter
import pickle
import shutil
import torch
import models
import utils.io_utils as io_utils
import utils.parser_utils as parser_utils
from explainer import explain
def arg_parse():
parser = argparse.ArgumentParser(description="GNN Explainer arguments.")
io_parser = parser.add_mutually_exclusive_group(required=False)
io_parser.add_argument("--dataset", dest="dataset", help="Input dataset.")
benchmark_parser = io_parser.add_argument_group()
benchmark_parser.add_argument(
"--bmname", dest="bmname", help="Name of the benchmark dataset"
)
io_parser.add_argument("--pkl", dest="pkl_fname", help="Name of the pkl data file")
parser_utils.parse_optimizer(parser)
parser.add_argument("--clean-log", action="store_true", help="If true, cleans the specified log directory before running.")
parser.add_argument("--logdir", dest="logdir", help="Tensorboard log directory")
parser.add_argument("--ckptdir", dest="ckptdir", help="Model checkpoint directory")
parser.add_argument("--cuda", dest="cuda", help="CUDA.")
parser.add_argument(
"--gpu",
dest="gpu",
action="store_const",
const=True,
default=False,
help="whether to use GPU.",
)
parser.add_argument(
"--epochs", dest="num_epochs", type=int, help="Number of epochs to train."
)
parser.add_argument(
"--hidden-dim", dest="hidden_dim", type=int, help="Hidden dimension"
)
parser.add_argument(
"--output-dim", dest="output_dim", type=int, help="Output dimension"
)
parser.add_argument(
"--num-gc-layers",
dest="num_gc_layers",
type=int,
help="Number of graph convolution layers before each pooling",
)
parser.add_argument(
"--bn",
dest="bn",
action="store_const",
const=True,
default=False,
help="Whether batch normalization is used",
)
parser.add_argument("--dropout", dest="dropout", type=float, help="Dropout rate.")
parser.add_argument(
"--nobias",
dest="bias",
action="store_const",
const=False,
default=True,
help="Whether to add bias. Default to True.",
)
parser.add_argument(
"--no-writer",
dest="writer",
action="store_const",
const=False,
default=True,
help="Whether to add bias. Default to True.",
)
# Explainer
parser.add_argument("--mask-act", dest="mask_act", type=str, help="sigmoid, ReLU.")
parser.add_argument(
"--mask-bias",
dest="mask_bias",
action="store_const",
const=True,
default=False,
help="Whether to add bias. Default to True.",
)
parser.add_argument(
"--explain-node", dest="explain_node", type=int, help="Node to explain."
)
parser.add_argument(
"--graph-idx", dest="graph_idx", type=int, help="Graph to explain."
)
parser.add_argument(
"--graph-mode",
dest="graph_mode",
action="store_const",
const=True,
default=False,
help="whether to run Explainer on Graph Classification task.",
)
parser.add_argument(
"--multigraph-class",
dest="multigraph_class",
type=int,
help="whether to run Explainer on multiple Graphs from the Classification task for examples in the same class.",
)
parser.add_argument(
"--multinode-class",
dest="multinode_class",
type=int,
help="whether to run Explainer on multiple nodes from the Classification task for examples in the same class.",
)
parser.add_argument(
"--align-steps",
dest="align_steps",
type=int,
help="Number of iterations to find P, the alignment matrix.",
)
parser.add_argument(
"--method", dest="method", type=str, help="Method. Possible values: base, att."
)
parser.add_argument(
"--name-suffix", dest="name_suffix", help="suffix added to the output filename"
)
parser.add_argument(
"--explainer-suffix",
dest="explainer_suffix",
help="suffix added to the explainer log",
)
# TODO: Check argument usage
parser.set_defaults(
logdir="log",
ckptdir="ckpt",
dataset="syn1",
opt="adam",
opt_scheduler="none",
cuda="0",
lr=0.1,
clip=2.0,
batch_size=20,
num_epochs=100,
hidden_dim=20,
output_dim=20,
num_gc_layers=3,
dropout=0.0,
method="base",
name_suffix="",
explainer_suffix="",
align_steps=1000,
explain_node=None,
graph_idx=-1,
mask_act="sigmoid",
multigraph_class=-1,
multinode_class=-1,
)
return parser.parse_args()
def main():
# Load a configuration
prog_args = arg_parse()
if prog_args.gpu:
os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda
print("CUDA", prog_args.cuda)
else:
print("Using CPU")
# Configure the logging directory
if prog_args.writer:
path = os.path.join(prog_args.logdir, io_utils.gen_explainer_prefix(prog_args))
if os.path.isdir(path) and prog_args.clean_log:
print('Removing existing log dir: ', path)
if not input("Are you sure you want to remove this directory? (y/n): ").lower().strip()[:1] == "y": sys.exit(1)
shutil.rmtree(path)
writer = SummaryWriter(path)
else:
writer = None
# Load a model checkpoint
ckpt = io_utils.load_ckpt(prog_args)
cg_dict = ckpt["cg"] # get computation graph
input_dim = cg_dict["feat"].shape[2]
num_classes = cg_dict["pred"].shape[2]
print("Loaded model from {}".format(prog_args.ckptdir))
print("input dim: ", input_dim, "; num classes: ", num_classes)
# Determine explainer mode
graph_mode = (
prog_args.graph_mode
or prog_args.multigraph_class >= 0
or prog_args.graph_idx >= 0
)
# build model
print("Method: ", prog_args.method)
if graph_mode:
# Explain Graph prediction
model = models.GcnEncoderGraph(
input_dim=input_dim,
hidden_dim=prog_args.hidden_dim,
embedding_dim=prog_args.output_dim,
label_dim=num_classes,
num_layers=prog_args.num_gc_layers,
bn=prog_args.bn,
args=prog_args,
)
else:
if prog_args.dataset == "ppi_essential":
# class weight in CE loss for handling imbalanced label classes
prog_args.loss_weight = torch.tensor([1.0, 5.0], dtype=torch.float).cuda()
# Explain Node prediction
model = models.GcnEncoderNode(
input_dim=input_dim,
hidden_dim=prog_args.hidden_dim,
embedding_dim=prog_args.output_dim,
label_dim=num_classes,
num_layers=prog_args.num_gc_layers,
bn=prog_args.bn,
args=prog_args,
)
if prog_args.gpu:
model = model.cuda()
# load state_dict (obtained by model.state_dict() when saving checkpoint)
model.load_state_dict(ckpt["model_state"])
# Create explainer
explainer = explain.Explainer(
model=model,
adj=cg_dict["adj"],
feat=cg_dict["feat"],
label=cg_dict["label"],
pred=cg_dict["pred"],
train_idx=cg_dict["train_idx"],
args=prog_args,
writer=writer,
print_training=True,
graph_mode=graph_mode,
graph_idx=prog_args.graph_idx,
)
# TODO: API should definitely be cleaner
# Let's define exactly which modes we support
# We could even move each mode to a different method (even file)
if prog_args.explain_node is not None:
explainer.explain(prog_args.explain_node, unconstrained=False)
elif graph_mode:
if prog_args.multigraph_class >= 0:
print(cg_dict["label"])
# only run for graphs with label specified by multigraph_class
labels = cg_dict["label"].numpy()
graph_indices = []
for i, l in enumerate(labels):
if l == prog_args.multigraph_class:
graph_indices.append(i)
if len(graph_indices) > 30:
break
print(
"Graph indices for label ",
prog_args.multigraph_class,
" : ",
graph_indices,
)
explainer.explain_graphs(graph_indices=graph_indices)
elif prog_args.graph_idx == -1:
# just run for a customized set of indices
explainer.explain_graphs(graph_indices=[1, 2, 3, 4])
else:
explainer.explain(
node_idx=0,
graph_idx=prog_args.graph_idx,
graph_mode=True,
unconstrained=False,
)
io_utils.plot_cmap_tb(writer, "tab20", 20, "tab20_cmap")
else:
if prog_args.multinode_class >= 0:
print(cg_dict["label"])
# only run for nodes with label specified by multinode_class
labels = cg_dict["label"][0] # already numpy matrix
node_indices = []
for i, l in enumerate(labels):
if len(node_indices) > 4:
break
if l == prog_args.multinode_class:
node_indices.append(i)
print(
"Node indices for label ",
prog_args.multinode_class,
" : ",
node_indices,
)
explainer.explain_nodes(node_indices, prog_args)
else:
# explain a set of nodes
masked_adj = explainer.explain_nodes_gnn_stats(
range(400, 700, 5), prog_args
)
if __name__ == "__main__":
main()