-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtrain_grand.py
239 lines (199 loc) · 8.03 KB
/
train_grand.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from __future__ import division
from __future__ import print_function
import time
import argparse
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from pygcn.utils import load_data, accuracy
from pygcn.models import GCN, MLP
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# Training settings
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disables CUDA training.')
parser.add_argument('--fastmode', action='store_true', default=False,
help='Validate during training pass.')
parser.add_argument('--seed', type=int, default=42, help='Random seed.')
parser.add_argument('--epochs', type=int, default=5000,
help='Number of epochs to train.')
parser.add_argument('--lr', type=float, default=0.01,
help='Initial learning rate.')
parser.add_argument('--weight_decay', type=float, default=5e-4,
help='Weight decay (L2 loss on parameters).')
parser.add_argument('--hidden', type=int, default=32,
help='Number of hidden units.')
parser.add_argument('--input_droprate', type=float, default=0.5,
help='Dropout rate of the input layer (1 - keep probability).')
parser.add_argument('--hidden_droprate', type=float, default=0.5,
help='Dropout rate of the hidden layer (1 - keep probability).')
parser.add_argument('--dropnode_rate', type=float, default=0.5,
help='Dropnode rate (1 - keep probability).')
parser.add_argument('--patience', type=int, default=100, help='Patience')
parser.add_argument('--order', type=int, default=5, help='Propagation step')
parser.add_argument('--sample', type=int, default=4, help='Sampling times of dropnode')
parser.add_argument('--tem', type=float, default=0.5, help='Sharpening temperature')
parser.add_argument('--lam', type=float, default=1., help='Lamda')
parser.add_argument('--dataset', type=str, default='cora', help='Data set')
parser.add_argument('--cuda_device', type=int, default=4, help='Cuda device')
parser.add_argument('--use_bn', action='store_true', default=False, help='Using Batch Normalization')
#dataset = 'citeseer'
#dataset = 'pubmed'
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.cuda.set_device(args.cuda_device)
dataset = args.dataset
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
# Load data
A, features, labels, idx_train, idx_val, idx_test = load_data(dataset)
idx_unlabel = torch.range(idx_train.shape[0], labels.shape[0]-1, dtype=int)
# Model and optimizer
model = MLP(nfeat=features.shape[1],
nhid=args.hidden,
nclass=labels.max().item() + 1,
input_droprate=args.input_droprate,
hidden_droprate=args.hidden_droprate,
use_bn = args.use_bn)
optimizer = optim.Adam(model.parameters(),
lr=args.lr, weight_decay=args.weight_decay)
if args.cuda:
model.cuda()
features = features.cuda()
A = A.cuda()
labels = labels.cuda()
idx_train = idx_train.cuda()
idx_val = idx_val.cuda()
idx_test = idx_test.cuda()
idx_unlabel = idx_unlabel.cuda()
def propagate(feature, A, order):
#feature = F.dropout(feature, args.dropout, training=training)
x = feature
y = feature
for i in range(order):
x = torch.spmm(A, x).detach_()
#print(y.add_(x))
y.add_(x)
return y.div_(order+1.0).detach_()
def rand_prop(features, training):
n = features.shape[0]
drop_rate = args.dropnode_rate
drop_rates = torch.FloatTensor(np.ones(n) * drop_rate)
if training:
masks = torch.bernoulli(1. - drop_rates).unsqueeze(1)
features = masks.cuda() * features
else:
features = features * (1. - drop_rate)
features = propagate(features, A, args.order)
return features
def consis_loss(logps, temp=args.tem):
ps = [torch.exp(p) for p in logps]
sum_p = 0.
for p in ps:
sum_p = sum_p + p
avg_p = sum_p/len(ps)
#p2 = torch.exp(logp2)
sharp_p = (torch.pow(avg_p, 1./temp) / torch.sum(torch.pow(avg_p, 1./temp), dim=1, keepdim=True)).detach()
loss = 0.
for p in ps:
loss += torch.mean((p-sharp_p).pow(2).sum(1))
loss = loss/len(ps)
return args.lam * loss
def train(epoch):
t = time.time()
X = features
model.train()
optimizer.zero_grad()
X_list = []
K = args.sample
for k in range(K):
X_list.append(rand_prop(X, training=True))
output_list = []
for k in range(K):
output_list.append(torch.log_softmax(model(X_list[k]), dim=-1))
loss_train = 0.
for k in range(K):
loss_train += F.nll_loss(output_list[k][idx_train], labels[idx_train])
loss_train = loss_train/K
#loss_train = F.nll_loss(output_1[idx_train], labels[idx_train]) + F.nll_loss(output_1[idx_train], labels[idx_train])
#loss_js = js_loss(output_1[idx_unlabel], output_2[idx_unlabel])
#loss_en = entropy_loss(output_1[idx_unlabel]) + entropy_loss(output_2[idx_unlabel])
loss_consis = consis_loss(output_list)
loss_train = loss_train + loss_consis
acc_train = accuracy(output_list[0][idx_train], labels[idx_train])
loss_train.backward()
optimizer.step()
if not args.fastmode:
model.eval()
X = rand_prop(X,training=False)
output = model(X)
output = torch.log_softmax(output, dim=-1)
loss_val = F.nll_loss(output[idx_val], labels[idx_val])
acc_val = accuracy(output[idx_val], labels[idx_val])
print('Epoch: {:04d}'.format(epoch+1),
'loss_train: {:.4f}'.format(loss_train.item()),
'acc_train: {:.4f}'.format(acc_train.item()),
'loss_val: {:.4f}'.format(loss_val.item()),
'acc_val: {:.4f}'.format(acc_val.item()),
'time: {:.4f}s'.format(time.time() - t))
return loss_val.item(), acc_val.item()
def Train():
# Train model
t_total = time.time()
loss_values = []
acc_values = []
bad_counter = 0
# best = args.epochs + 1
loss_best = np.inf
acc_best = 0.0
loss_mn = np.inf
acc_mx = 0.0
best_epoch = 0
for epoch in range(args.epochs):
# if epoch < 200:
# l, a = train(epoch, True)
# loss_values.append(l)
# acc_values.append(a)
# continue
l, a = train(epoch)
loss_values.append(l)
acc_values.append(a)
print(bad_counter)
if loss_values[-1] <= loss_mn or acc_values[-1] >= acc_mx:# or epoch < 400:
if loss_values[-1] <= loss_best: #and acc_values[-1] >= acc_best:
loss_best = loss_values[-1]
acc_best = acc_values[-1]
best_epoch = epoch
torch.save(model.state_dict(), dataset +'.pkl')
loss_mn = np.min((loss_values[-1], loss_mn))
acc_mx = np.max((acc_values[-1], acc_mx))
bad_counter = 0
else:
bad_counter += 1
# print(bad_counter, loss_mn, acc_mx, loss_best, acc_best, best_epoch)
if bad_counter == args.patience:
print('Early stop! Min loss: ', loss_mn, ', Max accuracy: ', acc_mx)
print('Early stop model validation loss: ', loss_best, ', accuracy: ', acc_best)
break
print("Optimization Finished!")
print("Total time elapsed: {:.4f}s".format(time.time() - t_total))
# Restore best model
print('Loading {}th epoch'.format(best_epoch))
model.load_state_dict(torch.load(dataset +'.pkl'))
def test():
model.eval()
X = features
X = rand_prop(X, training=False)
output = model(X)
output = torch.log_softmax(output, dim=-1)
loss_test = F.nll_loss(output[idx_test], labels[idx_test])
acc_test = accuracy(output[idx_test], labels[idx_test])
print("Test set results:",
"loss= {:.4f}".format(loss_test.item()),
"accuracy= {:.4f}".format(acc_test.item()))
Train()
test()