-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrain.py
157 lines (130 loc) · 4.04 KB
/
train.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
# ************************************************************
# Author : Bumsoo Kim, 2018
# Github : https://github.com/meliketoy/graph-tutorial.pytorch
#
# Korea University, Data-Mining Lab
# Graph Convolutional Neural Network
#
# Description : train.py
# The main code for training Graph Attention Networks.
# ***********************************************************
import time
import random
import os
import sys
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from utils import *
from models import GAT
from opts import TrainOptions
"""
N : number of nodes
D : number of features per node
E : number of classes
@ input :
- adjacency matrix (N x N)
- feature matrix (N x D)
- label matrix (N x E)
@ dataset :
- citeseer
- cora
- pubmed
"""
opt = TrainOptions().parse()
# Data upload
adj, features, labels, idx_train, idx_val, idx_test = load_data(path=opt.dataroot, dataset=opt.dataset)
use_gpu = torch.cuda.is_available()
model, optimizer = None, None
best_acc = 0
early_stop = 0
# Define the model and optimizer
if (opt.model == 'attention'):
print("| Constructing Graph Attention Network model...")
model = GAT(
nfeat = features.shape[1],
nhid = opt.num_hidden,
nclass = int(labels.max().item()) + 1,
dropout = opt.dropout,
nheads = opt.nb_heads,
nouts = opt.nb_outs,
alpha = opt.alpha
)
else:
raise NotImplementedError
if (opt.optimizer == 'sgd'):
optimizer = optim.SGD(
model.parameters(),
lr = opt.lr,
weight_decay = opt.weight_decay,
momentum = 0.9
)
elif (opt.optimizer == 'adam'):
optimizer = optim.Adam(
model.parameters(),
lr = opt.lr,
weight_decay = opt.weight_decay
)
else:
raise NotImplementedError
if use_gpu:
model.cuda()
features, adj, labels, idx_train, idx_val, idx_test = \
list(map(lambda x: x.cuda(), [features, adj, labels, idx_train, idx_val, idx_test]))
features, adj, labels = list(map(lambda x : Variable(x), [features, adj, labels]))
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
save_point = os.path.join('./checkpoint', opt.dataset)
if not os.path.isdir(save_point):
os.mkdir(save_point)
# Train
def train(epoch):
global best_acc, early_stop
t = time.time()
model.train()
optimizer.lr = opt.lr
optimizer.zero_grad()
output = model(features, adj)
loss_train = F.nll_loss(output[idx_train], labels[idx_train])
acc_train = accuracy(output[idx_train], labels[idx_train])
loss_train.backward()
optimizer.step()
# Validation for each epoch
model.eval()
output = model(features, adj)
loss_val = F.nll_loss(output[idx_val], labels[idx_val])
acc_val = accuracy(output[idx_val], labels[idx_val])
if acc_val > best_acc:
best_acc = acc_val
state = {
'model': model,
'acc': best_acc,
'epoch': epoch,
}
torch.save(state, os.path.join(save_point, '%s.t7' %(opt.model)))
early_stop = 0
else:
early_stop += 1
if (early_stop > 100):
return True
sys.stdout.flush()
sys.stdout.write('\r')
sys.stdout.write("=> Training Epoch #{} : lr = {:.4f}".format(epoch, optimizer.lr))
sys.stdout.write(" | Training acc : {:6.2f}%".format(acc_train.data.cpu().numpy() * 100))
sys.stdout.write(" | Best acc : {:.2f}%". format(best_acc.data.cpu().numpy() * 100))
return False
# Main code for training
if __name__ == "__main__":
print("\n[STEP 2] : Obtain (adjacency, feature, label) matrix")
print("| Adjacency matrix : {}".format(adj.shape))
print("| Feature matrix : {}".format(features.shape))
print("| Label matrix : {}".format(labels.shape))
# Training
print("\n[STEP 3] : Training")
for epoch in range(1, opt.epoch+1):
stopped = train(epoch)
if (stopped):
break
print("\n=> Training finished!")