-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_graph.py
163 lines (137 loc) · 6.47 KB
/
task_graph.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
import sys
import torch
from transformers.optimization import get_cosine_schedule_with_warmup
import torch.nn.functional as F
import torch_geometric.transforms as T
from datasets.asap.utils.data_asap import num_graphs
def get_trainer(params):
evaluator = None
# get datasets/dataloaders
if params['splits']=="GMT":
sys.path.append('datasets/gmt/')
from datasets.gmt.dataloader import get_dataloaders
if params['task'] in ['DD', 'PTC_MR', 'NCI1', 'PROTEINS', 'IMDB-BINARY', 'IMDB-MULTI', 'MUTAG', 'COLLAB', 'ENZYMES']:
train_loader, val_loader, test_loader, num_features, num_classes = get_dataloaders(task=params['task'], fold_number=params['index_split'], batch_size=params['batch_size'])
params['in_channel'] = num_features
params['out_channel'] = num_classes
elif params['splits']=="ASAP":
sys.path.append('datasets/asap/')
from datasets.asap.dataloader_asap import get_dataloaders
if params['task'] in ["FRANKENSTEIN", "NCI109", "NCI1", "PROTEINS", "DD"]:
train_loader, val_loader, test_loader, num_features, num_classes = get_dataloaders(task=params['task'], seed=params['seed'], fold_number=params['index_split'], batch_size=params['batch_size'])
params['in_channel'] = num_features
params['out_channel'] = num_classes
elif params['splits']=="OGB":
from ogb.graphproppred import PygGraphPropPredDataset, Evaluator
from torch_geometric.loader import DataLoader
dataset = PygGraphPropPredDataset(root='datasets/pyg/', name=params['task'], transform=T.ToSparseTensor(attr='edge_attr'))
split_idx = dataset.get_idx_split()
evaluator = Evaluator(params['task'])
train_loader = DataLoader(dataset[split_idx["train"]], batch_size=params['batch_size'], shuffle=True, num_workers=0)
val_loader = DataLoader(dataset[split_idx["valid"]], batch_size=params['batch_size'], shuffle=False, num_workers=0)
test_loader = DataLoader(dataset[split_idx["test"]], batch_size=params['batch_size'], shuffle=False, num_workers=0)
params['eval_metric'] = dataset.eval_metric
params['in_channel'] = params['hidden_channel']
params['out_channel'] = dataset.num_tasks
# get device
device = torch.device('cuda:%s'%(params['gpu_index']) if torch.cuda.is_available() else 'cpu')
print("GPU device: [%s]"%(device))
# get model
if params['model'] in ['GPNN']:
from model import GPNN as Encoder
elif params['model'] in ['EdgePool']:
from baseline import EdgePool as Encoder
elif params['model'] in ['GMT']:
from baseline import GMT as Encoder
model = Encoder(params)
# get criterion and optimizer
if params['task'] in ["ogbg-molhiv", "ogbg-molpcba"]:
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=params['learning_rate'])
else:
criterion = torch.nn.NLLLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=params['learning_rate'], weight_decay=params['weight_decay'])
# get scheduler
if params['lr_scheduler']==True:
scheduler = get_cosine_schedule_with_warmup(optimizer, params['patience']*len(train_loader), params['epochs']*len(train_loader))
else:
scheduler = None
# get trainer
trainer = dict(zip(['train_loader', 'val_loader', 'test_loader', 'device', 'model', 'criterion', 'optimizer', 'scheduler', 'evaluator', 'params'], [train_loader, val_loader, test_loader, device, model, criterion, optimizer, scheduler, evaluator, params]))
return trainer
def get_metric(trainer, stage):
# load variables
train_loader, val_loader, test_loader, device, model, criterion, optimizer, scheduler, evaluator, params = trainer.values()
if stage=='train':
data_loader = train_loader
elif stage=='valid':
data_loader = val_loader
elif stage=='test':
data_loader = test_loader
# set train/evaluate mode and device for model
model = model.to(device)
if stage=='train':
torch.set_grad_enabled(True)
model.train()
else:
torch.set_grad_enabled(False)
model.eval()
if params['task'] in ["ogbg-molhiv", "ogbg-molpcba"]:
y_true = []
y_pred = []
total_loss = 0.
num_g_total = 0.
for batch in data_loader:
if stage=='train':
if batch.x.shape[0] == 1 or batch.batch[-1] == 0:
continue
else:
if batch.x.shape[0] == 1:
continue
num_g = num_graphs(batch)
num_g_total += num_g
batch = batch.to(device)
encode_values = model(batch)
pred = encode_values['x']
is_labeled = batch.y == batch.y
loss = criterion(pred.to(torch.float32)[is_labeled], batch.y.to(torch.float32)[is_labeled])
total_loss += loss.item()*num_g
if stage=='train':
loss.backward()
optimizer.step()
optimizer.zero_grad()
if params['lr_scheduler']==True:
scheduler.step()
y_true.append(batch.y.view(pred.shape).detach().cpu())
y_pred.append(pred.detach().cpu())
total_loss = total_loss/num_g_total
y_true = torch.cat(y_true, dim = 0).numpy()
y_pred = torch.cat(y_pred, dim = 0).numpy()
input_dict = {"y_true": y_true, "y_pred": y_pred}
metric = evaluator.eval(input_dict)[params['eval_metric']]
else:
total_loss = 0.
correct = 0.
num_g_total = 0
for data in data_loader:
num_g = num_graphs(data)
num_g_total += num_g
data = data.to(device)
encode_values = model(data)
h = encode_values['x']
out = F.log_softmax(h, dim=-1)
loss = criterion(out, data.y)
total_loss += loss.item()*num_g
if stage=='train':
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), params['grad_norm'])
optimizer.step()
optimizer.zero_grad()
if params['lr_scheduler']==True:
scheduler.step()
pred = out.max(dim=1)[1]
correct += pred.eq(data.y).sum().item()
total_loss = total_loss/num_g_total
metric = correct/num_g_total
metrics = dict(zip(['metric', 'loss'], [metric, total_loss]))
return metrics