forked from sunilkmaurya/FSGNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_class.py
163 lines (133 loc) · 5.73 KB
/
node_class.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
from __future__ import division
from __future__ import print_function
import time
import random
import argparse
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from process import *
from utils import *
from model import *
import uuid
import pickle
# Training settings
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=42, help='Random seed.') #Default seed same as GCNII
parser.add_argument('--epochs', type=int, default=1500, help='Number of epochs to train.')
parser.add_argument('--layer', type=int, default=3, help='Number of layers.')
parser.add_argument('--hidden', type=int, default=64, help='hidden dimensions.')
parser.add_argument('--dropout', type=float, default=0.5, help='Dropout rate (1 - keep probability).')
parser.add_argument('--patience', type=int, default=100, help='Patience')
parser.add_argument('--data', default='cora', help='dateset')
parser.add_argument('--dev', type=int, default=0, help='device id')
parser.add_argument('--layer_norm',type=int, default=1, help='layer norm')
parser.add_argument('--w_att',type=float, default=0.0005, help='Weight decay scalar')
parser.add_argument('--w_fc2',type=float, default=0.0005, help='Weight decay layer-2')
parser.add_argument('--w_fc1',type=float, default=0.0005, help='Weight decay layer-1')
parser.add_argument('--lr_fc',type=float, default=0.02, help='Learning rate 2 fully connected layers')
parser.add_argument('--lr_att',type=float, default=0.02, help='Learning rate Scalar')
#parser.add_argument('--hop_select', type=int, default=0, help='Select hop combinations')
args = parser.parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
num_layer = args.layer
layer_norm = bool(int(args.layer_norm))
print("==========================")
print(f"Dataset: {args.data}")
print(f"Dropout:{args.dropout}, layer_norm: {layer_norm}")
print(f"w_att:{args.w_att}, w_fc2:{args.w_fc2}, w_fc1:{args.w_fc1}, lr_fc:{args.lr_fc}, lr_att:{args.lr_att}")
cudaid = "cuda:"+str(args.dev)
device = torch.device(cudaid)
checkpt_file = 'pretrained/'+uuid.uuid4().hex+'.pt'
def train_step(model,optimizer,labels,list_mat,idx_train):
model.train()
optimizer.zero_grad()
output = model(list_mat, layer_norm)
acc_train = accuracy(output[idx_train], labels[idx_train].to(device))
loss_train = F.nll_loss(output[idx_train], labels[idx_train].to(device))
loss_train.backward()
optimizer.step()
return loss_train.item(),acc_train.item()
def validate_step(model,labels,list_mat,idx_val):
model.eval()
with torch.no_grad():
output = model(list_mat, layer_norm)
loss_val = F.nll_loss(output[idx_val], labels[idx_val].to(device))
acc_val = accuracy(output[idx_val], labels[idx_val].to(device))
return loss_val.item(),acc_val.item()
def test_step(model,labels,list_mat,idx_test):
model.load_state_dict(torch.load(checkpt_file))
model.eval()
with torch.no_grad():
output = model(list_mat, layer_norm)
loss_test = F.nll_loss(output[idx_test], labels[idx_test].to(device))
acc_test = accuracy(output[idx_test], labels[idx_test].to(device))
#print(mask_val)
return loss_test.item(),acc_test.item()
def train(datastr,splitstr):
adj, adj_i, features, labels, idx_train, idx_val, idx_test, num_features, num_labels = full_load_data(datastr,splitstr)
features = features.to(device)
adj = adj.to(device)
adj_i = adj_i.to(device)
list_mat = []
list_mat.append(features)
no_loop_mat = features
loop_mat = features
for ii in range(args.layer):
no_loop_mat = torch.spmm(adj, no_loop_mat)
loop_mat = torch.spmm(adj_i, loop_mat)
list_mat.append(no_loop_mat)
list_mat.append(loop_mat)
model = FSGNN(nfeat=num_features,
nlayers=2*args.layer + 1,
nhidden=args.hidden,
nclass=num_labels,
dropout=args.dropout).to(device)
optimizer_sett = [
{'params': model.fc2.parameters(), 'weight_decay': args.w_fc2, 'lr': args.lr_fc},
{'params': model.fc1.parameters(), 'weight_decay': args.w_fc1, 'lr': args.lr_fc},
{'params': model.att, 'weight_decay': args.w_att, 'lr': args.lr_att},
]
optimizer = optim.Adam(optimizer_sett)
bad_counter = 0
best = 999999999
for epoch in range(args.epochs):
loss_tra,acc_tra = train_step(model,optimizer,labels,list_mat,idx_train)
loss_val,acc_val = validate_step(model,labels,list_mat,idx_val)
#Uncomment following lines to see loss and accuracy values
'''
if(epoch+1)%1 == 0:
print('Epoch:{:04d}'.format(epoch+1),
'train',
'loss:{:.3f}'.format(loss_tra),
'acc:{:.2f}'.format(acc_tra*100),
'| val',
'loss:{:.3f}'.format(loss_val),
'acc:{:.2f}'.format(acc_val*100))
'''
if loss_val < best:
best = loss_val
torch.save(model.state_dict(), checkpt_file)
bad_counter = 0
else:
bad_counter += 1
if bad_counter == args.patience:
break
test_out = test_step(model,labels,list_mat,idx_test)
acc = test_out[1]
return acc*100
t_total = time.time()
acc_list = []
for i in range(10):
datastr = args.data
splitstr = 'splits/'+args.data+'_split_0.6_0.2_'+str(i)+'.npz'
accuracy_data = train(datastr,splitstr)
acc_list.append(accuracy_data)
##print(i,": {:.2f}".format(acc_list[-1]))
print("Train cost: {:.4f}s".format(time.time() - t_total))
#print("Test acc.:{:.2f}".format(np.mean(acc_list)))
print(f"Test accuracy: {np.mean(acc_list)}, {np.round(np.std(acc_list),2)}")