-
Notifications
You must be signed in to change notification settings - Fork 6
/
exp_gap.py
134 lines (124 loc) · 6.53 KB
/
exp_gap.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
import time
import argparse
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from utils import load_citation, sgc_precompute, set_seed, stack_feat
from models import get_model
from metrics import accuracy
import pickle as pkl
from args import get_citation_args
from time import perf_counter
from train import train_regression, test_regression,\
train_gcn, test_gcn,\
train_kgcn, test_kgcn, train_mlp, train_gfnn
# Arguments
args = get_citation_args()
if args.tuned:
if args.model == "SGC" or args.model == "KGCN":
with open("{}-tuning/{}.txt".format(args.model, args.dataset), 'rb') as f:
args.weight_decay = pkl.load(f)['weight_decay']
print("using tuned weight decay: {}".format(args.weight_decay))
else:
raise NotImplemented
# setting random seeds
set_seed(args.seed, args.cuda)
adj, features, labels, idx_train,\
idx_val, idx_test = load_citation(args.dataset,
args.normalization,
args.cuda,
args.invlap_alpha,
args.shuffle)
# Monkey patch for Stacked Logistic Regression
if args.model == "SLG":
nfeat = features.size(1) * (args.degree+1)
else:
nfeat = features.size(1)
model = get_model(model_opt=args.model,
nfeat=nfeat,
nclass=labels.max().item()+1,
nhid=args.hidden,
dropout=args.dropout,
cuda=args.cuda,
degree=args.degree)
if args.model == "SGC" or args.model == "gfnn":
features, precompute_time = sgc_precompute(features, adj, args.degree)
print("{:.4f}s".format(precompute_time))
if args.model == "gfnn":
model, acc_val, train_time = train_gfnn(model,
features[idx_train],
labels[idx_train],
features[idx_val],
labels[idx_val],
epochs=args.epochs,
weight_decay=args.weight_decay,
lr=args.lr,
bs=args.batch_size,
patience=10,
verbose=True)
else:
model, acc_val, train_time = train_regression(model,
features[idx_train],
labels[idx_train],
features[idx_val],
labels[idx_val],
args.epochs,
args.weight_decay,
args.lr,
args.dropout)
acc_test = test_regression(model, features[idx_test], labels[idx_test])
print("Validation Accuracy: {:.4f} Test Accuracy: {:.4f}".format(acc_val,\
acc_test))
print("Pre-compute time: {:.4f}s, train time: {:.4f}s, total: {:.4f}s".format(precompute_time, train_time, precompute_time+train_time))
if args.model == "SLG":
features, precompute_time = stack_feat(features, adj, args.degree)
features = torch.FloatTensor(features).float()
if args.cuda:
features = features.cuda()
print("{:.4f}s".format(precompute_time))
model, acc_val, train_time = train_regression(model,
features[idx_train],
labels[idx_train],
features[idx_val],
labels[idx_val],
args.epochs,
args.weight_decay,
args.lr,
args.dropout)
acc_test = test_regression(model, features[idx_test], labels[idx_test])
print("Validation Accuracy: {:.4f} Test Accuracy: {:.4f}".format(acc_val,\
acc_test))
print("Pre-compute time: {:.4f}s, train time: {:.4f}s, total: {:.4f}s".format(precompute_time, train_time, precompute_time+train_time))
if args.model == "GCN":
model, acc_val, train_time = train_gcn(model,
adj,
features,
labels,
idx_train,
idx_val,
args.epochs,
args.weight_decay,
args.lr,
args.dropout)
acc_test = test_gcn(model, adj, features, labels, idx_test)
print("Validation Accuracy: {:.4f} Test Accuracy: {:.4f}".format(acc_val,\
acc_test))
precompute_time = 0
print("Pre-compute time: {:.4f}s, train time: {:.4f}s, total: {:.4f}s".format(precompute_time, train_time, precompute_time+train_time))
if args.model == "KGCN":
model, acc_val, train_time = train_kgcn(model,
adj,
features,
labels,
idx_train,
idx_val,
args.epochs,
args.weight_decay,
args.lr,
args.dropout)
acc_test = test_kgcn(model, adj, features, labels, idx_test)
precompute_time = 0
print("Validation Accuracy: {:.4f} Test Accuracy: {:.4f}".format(acc_val,\
acc_test))
print("Pre-compute time: {:.4f}s, train time: {:.4f}s, total: {:.4f}s".format(precompute_time, train_time, precompute_time+train_time))