-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
324 lines (237 loc) · 9.88 KB
/
utils.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
317
318
319
320
321
322
323
324
import os
import numpy as np
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.nn as nn
from sklearn.metrics import f1_score
import dgl
from dgl.data.ppi import PPIDataset
from ka_gat import GAT
from ppi_ka_s import StudentPPIDataset
from ppi_ka_t1 import Teacher1PPIDataset
from ppi_ka_t2 import Teacher2PPIDataset
def evaluate(feats, model, graph, labels, loss_fcn):
"""This is the function that computes the F1 score of the student model
Args:
feats (torch.Tensor): the input node features
model (nn.Module): the student GNN model
graph (DGLGraph): the input graphs containing the topological information
labels (torch.Tensor): the soft labels
loss_fcn (torch.nn): multi-label loss function
Returns:
tuple: a tuple containing the F1 scores for the two tasks of the teachers as well as the loss data
"""
model.eval()
with torch.no_grad():
model.g = graph
for layer in model.gat_layers:
layer.g = graph
output = model(feats.float())
loss_data = loss_fcn(output, labels.float())
predict = np.where(output.data.cpu().numpy() > 0.0, 1, 0)
score_whole = f1_score(labels.data.cpu().numpy(),
predict, average='micro')
# F1 score for the task of teacher #1
score_part1 = f1_score(labels.data.cpu().numpy()[:,:61],
predict[:,:61], average='micro')
# F1 score for the task of teacher #2
score_part2 = f1_score(labels.data.cpu().numpy()[:,61:],
predict[:,61:], average='micro')
model.train()
return score_whole, score_part1, score_part2, loss_data.item()
def test_model(test_dataloader, model, device, loss_fcn):
"""This is the function that returns the testing F1 scores of the student GNN
Args:
test_dataloader (torch.utils.data.DataLoader): testing dataloader for the student GNN
model (nn.Module): the student GNN model
device (torch.device): device to place the pytorch tensor
loss_fcn (torch.nn): multi-label loss function
"""
test_score_list = []
test_score_part1_list = []
test_score_part2_list = []
model.eval()
with torch.no_grad():
for _, graph in enumerate(test_dataloader):
graph = graph.to(device)
feats = graph.ndata['feat'].float()
labels = graph.ndata['label'].float()
test_score_list.append(evaluate(feats, model, graph, labels.float(), loss_fcn)[0])
test_score_part1_list.append(evaluate(feats, model, graph, labels.float(), loss_fcn)[1])
test_score_part2_list.append(evaluate(feats, model, graph, labels.float(), loss_fcn)[2])
mean_score = np.array(test_score_list).mean()
mean_score_part1 = np.array(test_score_part1_list).mean()
mean_score_part2 = np.array(test_score_part2_list).mean()
print(f"F1-Score on testset: whole {mean_score:.4f}, part1 {mean_score_part1:.4f}, part2 {mean_score_part2:.4f}")
model.train()
return
def generate_label(t_model, graph, feats, device):
"""This is the function that generates the logits for the unlabeled data from the pre-trained teacher GNNs
Args:
t_model (nn.Module): the pre-trained teacher GNN models
graph (DGLGraph): the input graphs containing the topological information
feats (torch.Tensor): the input node features
device (torch.device): device to place the pytorch tensor
Returns:
torch.Tensor: logits of the teacher GNNs
"""
t_model.eval()
with torch.no_grad():
t_model.g = graph
for layer in t_model.gat_layers:
layer.g = graph
# generate logits from the pre-trained teacher models
logits_t = t_model(feats.float())
return logits_t.detach()
def val_model(valid_dataloader, model, device, loss_fcn):
"""This is the function that returns the validation F1 scores of the student GNN
Args:
valid_dataloader (torch.utils.data.DataLoader): validation dataloader for the student GNN
model (nn.Module): the student GNN model
device (torch.device): device to place the pytorch tensor
loss_fcn (torch.nn): multi-label loss function
Returns:
torch.Tensor: the overall F1 score on the validation sets
"""
val_score_list = []
val_score_part1_list = []
val_score_part2_list = []
model.eval()
with torch.no_grad():
for _, graph in enumerate(valid_dataloader):
graph = graph.to(device)
feats = graph.ndata['feat'].float()
labels = graph.ndata['label'].float()
val_score_list.append(evaluate(feats, model, graph, labels.float(), loss_fcn)[0])
val_score_part1_list.append(evaluate(feats, model, graph, labels.float(), loss_fcn)[1])
val_score_part2_list.append(evaluate(feats, model, graph, labels.float(), loss_fcn)[2])
mean_score = np.array(val_score_list).mean()
mean_score_part1 = np.array(val_score_part1_list).mean()
mean_score_part2 = np.array(val_score_part2_list).mean()
print(f"F1-Score on valset: whole {mean_score:.4f}, part1 {mean_score_part1:.4f}, part2 {mean_score_part2:.4f}")
model.train()
return mean_score
def get_teacher1(args, data_info):
"""This is the function that returns the model architecture of teacher #1
Args:
args (parse_args): parser arguments
data_info (dict): the dictionary containing the data information of teacher #1
Returns:
model: teacher model #1
"""
heads = ([args.t1_num_heads] * args.t1_num_layers) + [args.t1_num_out_heads]
model = GAT(data_info['g'],
args.t1_num_layers,
data_info['num_feats'],
args.t1_num_hidden,
data_info['n_classes'],
heads,
F.elu,
args.in_drop,
args.attn_drop,
args.alpha,
args.residual)
return model
def get_teacher2(args, data_info):
"""This is the function that returns the model architecture of teacher #2
Args:
args (parse_args): parser arguments
data_info (dict): the dictionary containing the data information of teacher #2
Returns:
model: teacher model #2
"""
heads = ([args.t2_num_heads] * args.t2_num_layers) + [args.t2_num_out_heads]
model = GAT(data_info['g'],
args.t2_num_layers,
data_info['num_feats'],
args.t2_num_hidden,
data_info['n_classes'],
heads,
F.elu,
args.in_drop,
args.attn_drop,
args.alpha,
args.residual)
return model
def get_student(args, data_info):
"""This is the function that returns the model architecture of the student
Args:
args (parse_args): parser arguments
data_info (dict): the dictionary containing the data information of the student
Returns:
model: the student model
"""
heads = ([args.s_num_heads] * args.s_num_layers) + [args.s_num_out_heads]
model = GAT(data_info['g'],
args.s_num_layers,
data_info['num_feats'],
args.s_num_hidden,
data_info['n_classes'],
heads,
F.elu,
args.in_drop,
args.attn_drop,
args.alpha,
args.residual)
return model
def collate(graphs):
"""This is the function that collates lists of samples into batches
Args:
graphs (DGLGraph): the input graphs containing the topological information
Returns:
DGLGraph: batched graphs
"""
graph = dgl.batch(graphs)
return graph
def get_data_loader(args, type, device):
"""This is the function that returns the dataloaders and the data information
Args:
args (parse_args): parser arguments
type (string): ('teacher1', 'teacher2', 'student')
device (torch.device): device to place the dataloader
Returns:
list: the dataloaders and the associated data information
"""
# obtain the dataloaders
train_dataset = PPIDataset(mode='train')
valid_dataset = PPIDataset(mode='valid')
test_dataset = PPIDataset(mode='test')
train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, collate_fn=collate, num_workers=4, shuffle=True)
valid_dataloader = DataLoader(valid_dataset, batch_size=args.batch_size, collate_fn=collate, num_workers=2)
test_dataloader = DataLoader(test_dataset, batch_size=args.batch_size, collate_fn=collate, num_workers=2)
# get the data information
data_info = {}
g = train_dataset[0]
n_classes = train_dataset.num_labels
num_feats = g.ndata['feat'].shape[1]
data_info['num_feats'] = num_feats
data_info['g'] = g.int().to(device)
if type == 'teacher1':
data_info['n_classes'] = 61
elif type == 'teacher2':
data_info['n_classes'] = 60
elif type == 'student':
assert(n_classes == 121)
data_info['n_classes'] = n_classes
return (train_dataloader, valid_dataloader, test_dataloader), data_info
def save_checkpoint(model, path):
"""This is the function that saves the checkpoint
Args:
model (nn.Module): the trained GNN model
path (string): directory to save the model
"""
dirname = os.path.dirname(path)
if not os.path.isdir(dirname):
os.makedirs(dirname)
torch.save(model.state_dict(), path)
print(f"save model to {path}")
def load_checkpoint(model, path, device):
"""This is the function that loads the checkpoint
Args:
model (nn.Module): the GNN model
path (string): directory to load the model
device (torch.device): device to place the model
"""
model.load_state_dict(torch.load(path, map_location=device))
print(f"Load model from {path}")