-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_gwnn.py
365 lines (260 loc) · 13.6 KB
/
train_gwnn.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#### Importing all the packages
from __future__ import print_function
from __future__ import division
### We import torch functionis
import torch
from torchvision import datasets , transforms
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
import torch.nn as nn
#### We import custom functions
import sys
import os
sys.path.append(os.getcwd())
from src. builder import graphdataload
from src. builder .graphdataload import classnames
from src. models.models import gwnn
from src. utils.base_utils import increment_path ,gwnn_waveletbais
from src.cfg.load_yaml import load_yamlcfg
from src. viz.viz_graph import t_SNE,plot_train_val_loss,plot_train_val_acc
from src. viz.viz_graph import pca_tsne , tsne_legend
from src. metrics.metric import classify ,accuracy
#### We import default functions
import time
import argparse
import numpy as np
import glob
import os
import logging
import random
from pathlib import Path
import pyfiglet
#### Logging of the data into the txt file
logging.getLogger().setLevel(logging.INFO)
def train(model, optimizer, features, adj, y_train,y_val,train_mask,val_mask,epoch,valmode):
model.train()
optimizer.zero_grad()
y_pred = model(features)
loss_train = F.nll_loss(y_pred[train_mask], y_train,reduction='mean')
acc_train = accuracy(y_pred[train_mask], y_train)
loss_train.backward()
optimizer.step()
if not valmode:
with torch.no_grad():
model.eval()
y_pred = model(features)
loss_val = F.nll_loss(y_pred[val_mask], y_val)
acc_val = accuracy(y_pred[val_mask], y_val)
return loss_train.data.item(), acc_train.data.item() , loss_val.data.item(),acc_val.data.item()
def test(model, features, adj, test_mask, y_test,data_type,outputviz,fig_path):
model.eval()
y_pred = model(features)
loss_test = F.nll_loss(y_pred[test_mask], y_test)
acc_test = accuracy(y_pred[test_mask], y_test)
print("Test set results:","loss= {:.4f}".format(loss_test.data.item()),"accuracy= {:.4f}".format(acc_test.data.item()))
logging.info("Testing loss: {:.4f} acc: {:.4f} ".format((loss_test.data.item()),(acc_test.data.item())))
report = classify(y_pred[test_mask],y_test,classnames[data_type])
logging.info('GCN Classification Report: \n {}'.format(report))
if outputviz :
logging.info("\n[STEP 5]: Visualization {} results.".format(data_type))
## Make a copy for pca and tsneplot
outs = y_pred[test_mask]
label=y_test
y_pred = y_pred.cpu().detach().numpy()
y_test = y_test.cpu().detach().numpy()
result_all_2d = t_SNE(y_pred[test_mask], y_test,2,fig_path)
pca_tsne(outs,label,fig_path)
tsne_legend(outs.detach().cpu().numpy(), y_test, classnames[data_type], 'test_set',fig_path)
def main():
parser = argparse.ArgumentParser(description="GNN architectures")
parser.add_argument('--config_path', action='store_true', \
default='E:\\Freelance_projects\\GNN\\Tutsv2\\pyGNN_NC_XAI_V2\\GWNN\\config\\gwnn_pubmed.yaml', help='Provide the config path')
#### to create an inc of directory when running test and saving results
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument("--approximation-order",type=int,default=4,
help="Order of Chebyshev polynomial. Default is 4.")
parser.add_argument("--fast",default = False ,action='store_true',
help="Use fast graph wavelets with Chebyshev polynomial approximation.")
args = parser.parse_args()
###### Params loading from config File
config_path = args.config_path
configs = load_yamlcfg(config_file= config_path)
data_type = configs['Data']['datatype']
data_saveresult= configs['Data']['save_results']
train_datapath = configs['Data']['datapath']
train_seedvalue = configs['random_state']
train_modelsave = configs['Data']['model_save_path']
train_savefig = configs['Data']['save_fig']
test_outputviz = configs['Data']['output_viz']
#--------------------------------------------------------------#
use_bn = configs['Model']['use_bn']
model_hidden=configs['Model']['hidden_dim']
model_droput=configs['Model']['dropout']
model_type=configs['Model']['type']
model_approximation_order =configs['Model']['approximation_order']
model_scale =configs['Model']['scale']
model_threshold =configs['Model']['threshold']
model_fastmode =configs['Model']['fastmode']
#--------------------------------------------------------------#
train_lr = configs['Hyper']['LR']
train_wtdecay = configs['Hyper']['weight_decay']
train_epochs = configs['Hyper']['epochs']
train_valmode = False
train_patience=configs['Hyper']['Patience']
### Creating an incremental Directories
save_dir = Path(increment_path(Path(data_saveresult) / 'exp', exist_ok=args.exist_ok)) # increment run
#### Creating and saving into the log file
logsave_dir= "./"
logging.basicConfig(level=logging.INFO,
handlers=[logging.FileHandler(os.path.join(logsave_dir+model_type + '_log.txt')),
logging.StreamHandler() ],
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p'
)
####Bannering
ascii_banner = pyfiglet.figlet_format("GWNN !")
print(ascii_banner)
logging.info(ascii_banner)
###### To check if cuda is available else use the cpu
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logging.info(f'Using: {device}')
logging.info("Using seed {}.".format(train_seedvalue))
#### Initialize the manual seed from argument
np.random.seed(train_seedvalue)
torch.manual_seed(train_seedvalue)
if device.type == 'cuda' :
torch.cuda.manual_seed(train_seedvalue)
###### Data loading based on the dataset
if data_type == 'cora' or data_type == 'pubmed' or data_type == 'citeseer':
citedata = graphdataload.Graph_data(train_datapath,data_type,'SemiSupervised')
citedata.load_data()
adj = getattr(citedata, data_type+'_adjlist')
adj_unnorm = getattr(citedata, data_type+'_adjunnorm')
features = getattr(citedata, data_type+'_features')
features_unnorm = getattr(citedata, data_type+'_featuresunnorm')
y_train = getattr(citedata, data_type+'_ytrain')
y_val = getattr(citedata, data_type+'_yval')
y_test = getattr(citedata, data_type+'_ytest')
n_class = getattr(citedata, data_type+'_classes_num')
train_mask = getattr(citedata, data_type+'_trainmask')
val_mask = getattr(citedata, data_type+'_valmask')
test_mask = getattr(citedata, data_type+'_testmask')
logging.info("\n[STEP 1]: Processing {} dataset.".format(data_type))
logging.info("| # of nodes : {}".format(adj.shape[0]))
logging.info("| # of features : {}".format(features_unnorm.shape[1]))
logging.info("| # of clases : {}".format(n_class))
logging.info("| # of train set : {}".format(len(y_train)))
logging.info("| # of val set : {}".format(len(y_val)))
logging.info("| # of test set : {}".format(len(y_test)))
logging.info("| # of number of classes : {}".format(n_class))
else:
raise NotImplementedError(data_type)
logging.info("Dataset Used {}.".format(data_type))
#######Data Loading is completed
###Intialization of variables
wavebasis = gwnn_waveletbais()
scale = model_scale
threshold = model_threshold
fastmode = model_fastmode
approx_order = model_approximation_order
node_count = (adj_unnorm.shape)[0]
n_features = (features_unnorm.shape)[1]
if model_type == 'gwnn':
logging.info("\n[STEP 2]: Model {} definition.".format(model_type))
logging.info("\n[STEP 2a]: Calculate the wavelet basis for {} .".format(model_type))
if fastmode:
wavelets, wavelet_inv = wavebasis.fast_wavelet_basis(adj_unnorm,scale, threshold,approx_order )
else:
wavelets, wavelet_inv = wavebasis.wavelet_basis(adj_unnorm,scale,threshold)
print('Wavelet basis completed')
wavelets, wavelet_inv = (torch.from_numpy(wavelets.toarray())).float().to(device),\
(torch.from_numpy(wavelet_inv.toarray())).float().to(device)
model = gwnn(node_count,n_features,model_hidden,n_class,wavelets,wavelet_inv,model_droput)
optimizer = optim.Adam(model.parameters(),lr=train_lr, weight_decay=train_wtdecay)
logging.info("Model Architecture Used {}.".format(model_type))
logging.info(str(model))
tot_params = sum([np.prod(p.size()) for p in model.parameters()])
logging.info(f"Total number of parameters: {tot_params}")
logging.info(f"Number of epochs: {train_epochs}")
if device.type == 'cuda':
model.cuda()
features_unnorm = (torch.from_numpy(features_unnorm.toarray())).float().cuda()
adj = adj.cuda()
y_train = y_train.cuda()
y_val = y_val.cuda()
y_test = y_test.cuda()
train_loss_history = []
val_loss_history = []
train_acc_history = []
val_acc_history = []
bad_counter = 0
loss_best = np.inf
loss_mn = np.inf
acc_best = 0.0
acc_mx = 0.0
best_epoch = 0
t_total = time.time()
logging.info("\n[STEP 3]: Model {} Training for epochs {}.".format(model_type,train_epochs))
############################################## Training Started ##############
for epoch in range(train_epochs):
to= time.time()
train_loss,train_acc,val_loss,val_acc = train(model, optimizer, features_unnorm,\
adj_unnorm, y_train,y_val,train_mask,val_mask,epoch,valmode= train_valmode)
print('Epoch: {:04d}'.format(epoch+1),'loss_train: {:.4f}'.format(train_loss),
'acc_train: {:.4f}'.format(train_acc),'loss_val: {:.4f}'.format(val_loss),
'acc_val: {:.4f}'.format(val_acc),'time: {:.4f}s'.format(time.time() - to))
logging.info("Epoch:{:04d} loss_train:{:.4f} acc_train:{:.4f} loss_val:{:.4f} acc_val:{:.4f} time:{:.4f}s.".format((epoch+1),(train_loss),(train_acc),(val_loss),(val_acc),(time.time()-to)))
train_loss_history.append(train_loss)
train_acc_history.append(train_acc)
val_loss_history.append(val_loss)
val_acc_history.append(val_acc)
##saving of the model
path = os.path.join(train_modelsave, '{}_{}.pkl'.format(model_type, epoch))
if val_loss_history[-1] <= loss_mn or val_acc_history[-1] >= acc_mx:
if val_loss_history[-1] <= loss_best:
loss_best = val_loss_history[-1]
acc_best = val_acc_history[-1]
best_epoch = epoch
torch.save(model.state_dict(), path)
loss_mn = np.min((val_loss_history[-1], loss_mn))
acc_mx = np.max((val_acc_history[-1], acc_mx))
bad_counter = 0
else:
bad_counter += 1
if bad_counter == train_patience:
print('Early stop! Min loss: ', loss_mn, ', Max accuracy: ', acc_mx)
print('Early stop model validation loss: ', loss_best, ', accuracy: ', acc_best)
train_epochs=epoch+1
break
for f in glob.glob(os.path.join(train_modelsave,'*.pkl')):
epoch_nb = int(f.split(os.path.sep)[-1].split('_')[-1].split('.')[0])
if epoch_nb < best_epoch:
os.remove(f)
for f in glob.glob(os.path.join(train_modelsave,'*.pkl')):
epoch_nb =int(f.split(os.path.sep)[-1].split('_')[-1].split('.')[0])
if epoch_nb > best_epoch:
os.remove(f)
logging.info(f"Total Training Completed :{(time.time() - t_total)}")
if train_savefig:
logging.info("\n[STEP 3a]: Saving the Plot of Model {} Training(loss/acc)vs Validation(loss/acc).".format(model_type))
(save_dir / 'train_plot' if train_savefig else save_dir).mkdir(parents=True, exist_ok=True)
save_path = str(save_dir / 'train_plot')
num_epochs = range(1, train_epochs + 1)
plot_train_val_loss(num_epochs,train_loss_history,val_loss_history,save_path)
plot_train_val_acc(num_epochs,train_acc_history,val_acc_history,save_path)
############################################## Training Completed ##############
############################################## Testing Started
print('Loading {}th epoch'.format(best_epoch))
loadpath = os.path.join(train_modelsave, '{}_{}.pkl'.format(model_type, best_epoch))
model.load_state_dict(torch.load(loadpath))
if test_outputviz:
(save_dir / 'test_fig' if test_outputviz else save_dir).mkdir(parents=True, exist_ok=True)
testsave_fig = str(save_dir / 'test_fig')
logging.info("\n[STEP 4]: Testing {} final model.".format(model_type))
test(model, features_unnorm, adj_unnorm, test_mask,y_test,data_type,test_outputviz,testsave_fig)
else:
raise NotImplementedError(model_type)
if __name__ == "__main__":
main()
torch.cuda.empty_cache()