-
Notifications
You must be signed in to change notification settings - Fork 1
/
traditional_gnn.py
143 lines (124 loc) · 5.03 KB
/
traditional_gnn.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
import torch
from torch_geometric.loader import DataLoader
import torch_geometric
from tqdm import tqdm
import numpy as np
import yaml
from yaml import SafeLoader
from utils.args import Arguments
from utils.sampling import subsampling
from data.load import load_data
from models import load_model
def train_subgraph(model, optimizer, criterion, config, train_loader, val_loader, test_loader, device):
if config.earlystop:
cnt = 0
patience = config.patience
best_val = 0
best_test_fromval = 0
for epoch in tqdm(range(config.epochs)):
model.train()
for batch in train_loader:
batch = batch.to(device)
optimizer.zero_grad()
out = model.forward_subgraph(batch.x, batch.edge_index, batch.batch, batch.root_n_index)
loss = criterion(out, batch.y)
loss.backward()
optimizer.step()
if config.earlystop:
val_acc = eval_subgraph(model, val_loader, device)
if val_acc > best_val:
cnt = 0
best_test_fromval = eval_subgraph(model, test_loader, device)
best_val = val_acc
else:
cnt += 1
if cnt >= patience:
print(f'early stop at epoch {epoch}')
break
if not config.earlystop:
best_test_fromval = eval_subgraph(model, test_loader, device)
return best_test_fromval
def eval_subgraph(model, data_loader, device):
model.eval()
correct = 0
total_num = 0
for batch in data_loader:
batch = batch.to(device)
preds = model.forward_subgraph(batch.x, batch.edge_index, batch.batch, batch.root_n_index).argmax(dim=1)
correct += (preds == batch.y).sum().item()
total_num += batch.y.shape[0]
acc = correct / total_num
return acc
def train_fullgraph(model, optimizer, criterion, config, data, device):
if config.earlystop:
cnt = 0
patience = config.patience
best_val = 0
best_test_fromval = 0
model.train()
data = data.to(device)
for epoch in tqdm(range(config.epochs)):
optimizer.zero_grad()
out = model(data.x, data.edge_index)
loss = criterion(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
if config.earlystop:
val_acc = eval_fullgraph(model, data, device, config)
if val_acc > best_val:
cnt = 0
best_test_fromval = eval_fullgraph(model, data, device, config, eval="test")
best_val = val_acc
else:
cnt += 1
if cnt >= patience:
print(f'early stop at epoch {epoch}')
break
if not config.earlystop:
best_test_fromval = eval_fullgraph(model, data, device, config)
return best_test_fromval
def eval_fullgraph(model, data, device, config, eval="valid"):
assert eval in ["valid", "test"]
model.eval()
data = data.to(device)
pred = model(data.x, data.edge_index).argmax(dim=1)
if eval == "test":
correct = (pred[data.test_mask] == data.y[data.test_mask]).sum()
total = int(data.test_mask.sum())
else:
correct = (pred[data.val_mask] == data.y[data.val_mask]).sum()
total = int(data.val_mask.sum())
acc = int(correct) / total
return acc
def train_eval(model, optimizer, criterion, config, data, train_loader, val_loader, test_loader, device):
if config.subsampling:
test_acc = train_subgraph(model, optimizer, criterion, config, train_loader, val_loader, test_loader, device)
else:
test_acc = train_fullgraph(model, optimizer, criterion, config, data, device)
return test_acc
def main(config):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
acc_list = []
for i in range(5):
# load data
data, text, num_classes = load_data(config.dataset, use_text=True, seed=i)
data.y = data.y.squeeze()
model = load_model(data.x.shape[1], num_classes, config).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=config.lr, weight_decay=config.weight_decay)
criterion = torch.nn.CrossEntropyLoss()
train_loader, val_loader, test_loader = None, None, None
if config.subsampling:
train_loader, val_loader, test_loader = subsampling(data, config, sampler=config.sampler)
test_acc = train_eval(model, optimizer, criterion, config, data, train_loader, val_loader, test_loader, device)
print(i, test_acc)
acc_list.append(test_acc)
final_acc, final_acc_std = np.mean(acc_list), np.std(acc_list)
print(f"# final_acc: {final_acc*100:.2f}±{final_acc_std*100:.2f}")
if __name__ == '__main__':
args = Arguments().parse_args()
config = yaml.load(open(args.config), Loader=SafeLoader)
# combine args and config
for k, v in config.items():
args.__setattr__(k, v)
print(args)
main(args)