-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
146 lines (119 loc) · 4.75 KB
/
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
import argparse
import 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
from logger import *
from dataset import load_dataset
from data_utils import eval_acc, eval_rocauc, load_fixed_splits
from eval import *
from parse import parse_method, parser_add_main_args
def fix_seed(seed=42):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
### Parse args ###
parser = argparse.ArgumentParser(description='Training Pipeline for Node Classification')
parser_add_main_args(parser)
args = parser.parse_args()
if not args.global_dropout:
args.global_dropout = args.dropout
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)
if len(dataset.label.shape) == 1:
dataset.label = dataset.label.unsqueeze(1)
dataset.label = dataset.label.to(device)
split_idx_lst = load_fixed_splits(args.data_dir, dataset, name=args.dataset)
### Basic information of datasets ###
n = dataset.graph['num_nodes']
e = dataset.graph['edge_index'].shape[1]
c = max(dataset.label.max().item() + 1, dataset.label.shape[1])
d = dataset.graph['node_feat'].shape[1]
print(f"dataset {args.dataset} | num nodes {n} | num edge {e} | num node feats {d} | num classes {c}")
dataset.graph['edge_index'] = to_undirected(dataset.graph['edge_index'])
dataset.graph['edge_index'], _ = remove_self_loops(dataset.graph['edge_index'])
dataset.graph['edge_index'], _ = add_self_loops(dataset.graph['edge_index'], num_nodes=n)
dataset.graph['edge_index'], dataset.graph['node_feat'] = \
dataset.graph['edge_index'].to(device), dataset.graph['node_feat'].to(device)
### Load method ###
model = parse_method(args, n, c, d, device)
### Loss function (Single-class, Multi-class) ###
if args.dataset in ('questions'):
criterion = nn.BCEWithLogitsLoss()
else:
criterion = nn.NLLLoss()
### Performance metric (Acc, AUC) ###
if args.metric == 'rocauc':
eval_func = eval_rocauc
else:
eval_func = eval_acc
logger = Logger(args.runs, args)
model.train()
print('MODEL:', model)
### Training loop ###
for run in range(args.runs):
if args.dataset in ('coauthor-cs', 'coauthor-physics', 'amazon-computer', 'amazon-photo'):
split_idx = split_idx_lst[0]
else:
split_idx = split_idx_lst[run]
train_idx = split_idx['train'].to(device)
model.reset_parameters()
model._global = False
optimizer = torch.optim.Adam(model.parameters(),weight_decay=args.weight_decay, lr=args.lr)
best_val = float('-inf')
best_test = float('-inf')
if args.save_model:
save_model(args, model, optimizer, run)
for epoch in range(args.local_epochs+args.global_epochs):
if epoch == args.local_epochs:
print("start global attention!!!!!!")
if args.save_model:
model, optimizer = load_model(args, model, optimizer, run)
model._global = True
model.train()
optimizer.zero_grad()
out = model(dataset.graph['node_feat'], dataset.graph['edge_index'])
if args.dataset in ('questions'):
if dataset.label.shape[1] == 1:
true_label = F.one_hot(dataset.label, dataset.label.max() + 1).squeeze(1)
else:
true_label = dataset.label
loss = criterion(out[train_idx], true_label.squeeze(1)[
train_idx].to(torch.float))
else:
out = F.log_softmax(out, dim=1)
loss = criterion(
out[train_idx], dataset.label.squeeze(1)[train_idx])
loss.backward()
optimizer.step()
result = evaluate(model, dataset, split_idx, eval_func, criterion, args)
logger.add_result(run, result[:-1])
if result[1] > best_val:
best_val = result[1]
best_test = result[2]
if args.save_model:
save_model(args, model, optimizer, run)
if epoch % args.display_step == 0:
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}%, '
f'Best Valid: {100 * best_val:.2f}%, '
f'Best Test: {100 * best_test:.2f}%')
logger.print_statistics(run)
results = logger.print_statistics()
### Save results ###
save_result(args, results)