-
Notifications
You must be signed in to change notification settings - Fork 28
/
main-batch.py
172 lines (144 loc) · 6.22 KB
/
main-batch.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
import argparse
import sys
import os, random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.utils import to_undirected, remove_self_loops, add_self_loops, subgraph, k_hop_subgraph
from torch_scatter import scatter
from logger import Logger
from dataset import load_dataset
from data_utils import load_fixed_splits, adj_mul, to_sparse_tensor
from eval import evaluate_cpu, eval_acc, eval_rocauc, eval_f1
from parse import parse_method, parser_add_main_args
import time
import warnings
warnings.filterwarnings('ignore')
# NOTE: for consistent data splits, see data_utils.rand_train_test_idx
def fix_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
### Parse args ###
parser = argparse.ArgumentParser(description='General Training Pipeline')
parser_add_main_args(parser)
args = parser.parse_args()
print(args)
fix_seed(args.seed)
if args.cpu:
device = torch.device("cpu")
else:
device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available() else torch.device("cpu")
### Load and preprocess data ###
dataset = load_dataset(args.data_dir, args.dataset, args.sub_dataset)
if len(dataset.label.shape) == 1:
dataset.label = dataset.label.unsqueeze(1)
# get the splits for all runs
if args.rand_split:
split_idx_lst = [dataset.get_idx_split(train_prop=args.train_prop, valid_prop=args.valid_prop)
for _ in range(args.runs)]
elif args.rand_split_class:
split_idx_lst = [dataset.get_idx_split(split_type='class', label_num_per_class=args.label_num_per_class)
for _ in range(args.runs)]
elif args.dataset in ['ogbn-proteins', 'ogbn-arxiv', 'ogbn-products', 'amazon2m']:
split_idx_lst = [dataset.load_fixed_splits()
for _ in range(args.runs)]
else:
split_idx_lst = load_fixed_splits(args.data_dir, dataset, dataset=args.dataset, protocol=args.protocol)
n = dataset.graph['num_nodes']
# infer the number of classes for non one-hot and one-hot labels
c = max(dataset.label.max().item() + 1, dataset.label.shape[1])
d = dataset.graph['node_feat'].shape[1]
# whether or not to symmetrize
if not args.directed and args.dataset != 'ogbn-proteins':
dataset.graph['edge_index'] = to_undirected(dataset.graph['edge_index'])
edge_index, x = dataset.graph['edge_index'], dataset.graph['node_feat']
print(f"num nodes {n} | num edges {edge_index.size(1)} | num classes {c} | num node feats {d}")
### Load method ###
model = parse_method(args, dataset, n, c, d, device)
### Loss function (Single-class, Multi-class) ###
if args.dataset in ('yelp-chi', 'deezer-europe', 'twitch-e', 'fb100', 'ogbn-proteins'):
criterion = nn.BCEWithLogitsLoss()
else:
criterion = nn.NLLLoss()
### Performance metric (Acc, AUC, F1) ###
if args.metric == 'rocauc':
eval_func = eval_rocauc
elif args.metric == 'f1':
eval_func = eval_f1
else:
eval_func = eval_acc
logger = Logger(args.runs, args)
model.train()
print('MODEL:', model)
adjs = []
adj, _ = remove_self_loops(edge_index)
adj, _ = add_self_loops(adj, num_nodes=n)
adjs.append(adj)
for i in range(args.rb_order - 1): # edge_index of high order adjacency
adj = adj_mul(adj, adj, n)
adjs.append(adj)
dataset.graph['adjs'] = adjs
if args.dataset in ('yelp-chi', 'deezer-europe', 'twitch-e', 'fb100', 'ogbn-proteins'):
if dataset.label.shape[1] == 1:
true_label = F.one_hot(dataset.label, dataset.label.max() + 1).squeeze(1)
else:
true_label = dataset.label
### Training loop ###
for run in range(args.runs):
if args.dataset in ['cora', 'citeseer', 'pubmed'] and args.protocol == 'semi':
split_idx = split_idx_lst[0]
else:
split_idx = split_idx_lst[run]
train_idx = split_idx['train'].to(device)
model.reset_parameters()
optimizer = torch.optim.Adam(model.parameters(),weight_decay=args.weight_decay, lr=args.lr)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[100, 200], gamma=0.5)
best_val = float('-inf')
num_batch = train_idx.size(0) // args.batch_size + 1
for epoch in range(args.epochs):
model.to(device)
model.train()
if args.dataset in ('yelp-chi', 'deezer-europe', 'twitch-e', 'fb100', 'ogbn-proteins'):
true_label = true_label.to(device)
else:
dataset.label = dataset.label.to(device)
idx = torch.randperm(train_idx.size(0))
for i in range(num_batch):
idx_i = train_idx[idx[i*args.batch_size:(i+1)*args.batch_size]]
x_i = x[idx_i].to(device)
adjs_i = []
edge_index_i, _ = subgraph(idx_i, adjs[0], num_nodes=n, relabel_nodes=True)
adjs_i.append(edge_index_i.to(device))
for k in range(args.rb_order - 1):
edge_index_i, _ = subgraph(idx_i, adjs[k+1], num_nodes=n, relabel_nodes=True)
adjs_i.append(edge_index_i.to(device))
optimizer.zero_grad()
out_i, link_loss_ = model(x_i, adjs_i, args.tau)
if args.dataset in ('yelp-chi', 'deezer-europe', 'twitch-e', 'fb100', 'ogbn-proteins'):
loss = criterion(out_i, true_label.squeeze(1)[idx_i].to(torch.float))
else:
out_i = F.log_softmax(out_i, dim=1)
loss = criterion(out_i, dataset.label.squeeze(1)[idx_i])
loss -= args.lamda * sum(link_loss_) / len(link_loss_)
loss.backward()
optimizer.step()
if args.dataset == 'ogbn-proteins':
scheduler.step()
if epoch % 9 == 0:
result = evaluate_cpu(model, dataset, split_idx, eval_func, criterion, args)
logger.add_result(run, result[:-1])
if result[1] > best_val:
best_val = result[1]
if args.save_model:
torch.save(model.state_dict(), args.model_dir + f'{args.dataset}-{args.method}.pkl')
print(f'Epoch: {epoch:02d}, '
f'Loss: {loss:.4f}, '
f'Train: {100 * result[0]:.2f}%, '
f'Valid: {100 * result[1]:.2f}%, '
f'Test: {100 * result[2]:.2f}%')
logger.print_statistics(run)
results = logger.print_statistics()