forked from mtiezzi/torch_gnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_subgraph.py
161 lines (133 loc) · 5.9 KB
/
main_subgraph.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
import torch
import torchvision
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import argparse
import utils
import dataloader
from gnn_wrapper import GNNWrapper
import net
from itertools import product
import time
#
# # fix random seeds for reproducibility
# SEED = 123
# torch.manual_seed(SEED)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
# np.random.seed(SEED)
def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch')
parser.add_argument('--epochs', type=int, default=10000, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.0001, metavar='LR',
help='learning rate (default: 0.0001)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--cuda_dev', type=int, default=0,
help='select specific CUDA device for training')
parser.add_argument('--n_gpu_use', type=int, default=1,
help='select number of CUDA device for training')
parser.add_argument('--log-interval', type=int, default=50, metavar='N',
help='logging training status cadency')
parser.add_argument('--tensorboard', action='store_true', default=True,
help='For logging the model in tensorboard')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
if not use_cuda:
args.n_gpu_use = 0
device = utils.prepare_device(n_gpu_use=args.n_gpu_use, gpu_id=args.cuda_dev)
# kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
# torch.manual_seed(args.seed)
# # fix random seeds for reproducibility
# SEED = 123
# torch.manual_seed(SEED)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
# np.random.seed(SEED)
# configugations
cfg = GNNWrapper.Config()
cfg.use_cuda = use_cuda
cfg.device = device
cfg.log_interval = args.log_interval
cfg.tensorboard = args.tensorboard
# cfg.batch_size = args.batch_size
# cfg.test_batch_size = args.test_batch_size
# cfg.momentum = args.momentum
cfg.dataset_path = './data'
cfg.epochs = args.epochs
cfg.lrw = args.lr
cfg.activation = nn.Sigmoid()
cfg.state_transition_hidden_dims = [10, ]
cfg.output_function_hidden_dims = [ 5]
cfg.state_dim = 10 #
cfg.max_iterations = 50
cfg.convergence_threshold = 0.01
cfg.graph_based = False
cfg.log_interval = 10
cfg.lrw = 0.01
cfg.task_type = "multiclass"
# model creation
# model_tr = GNNWrapper(cfg)
# model_val = GNNWrapper(cfg)
# model_tst = GNNWrapper(cfg)
cfg.dset_name = "sub_30_15_200"
cfg.aggregation_type = "degreenorm"
# dataset creation
dset = dataloader.get_subgraph(set=cfg.dset_name, aggregation_type=cfg.aggregation_type, sparse_matrix=True) # generate the dataset
cfg.label_dim = dset["train"].node_label_dim
state_nets = [
net.StateTransition(cfg.state_dim, cfg.label_dim,
mlp_hidden_dim=cfg.state_transition_hidden_dims,
activation_function=cfg.activation),
net.GINTransition(cfg.state_dim, cfg.label_dim,
mlp_hidden_dim=cfg.state_transition_hidden_dims,
activation_function=cfg.activation),
net.GINPreTransition(cfg.state_dim, cfg.label_dim,
mlp_hidden_dim=cfg.state_transition_hidden_dims,
activation_function=cfg.activation)
]
lrs = [0.05, 0.01, 0.001]
hyperparameters = dict(lr=lrs, state_net=state_nets)
hyperparameters_values = [v for v in hyperparameters.values()]
start_0 = time.time()
for lr, state_net in product(*hyperparameters_values):
cfg.lrw = lr
cfg.state_net = state_net
print(f"learning_rate:{lr}, state_dim:{cfg.state_dim}, aggregation function:{str(state_net).split('(')[0]} ")
# model creation
model_tr = GNNWrapper(cfg)
model_val = GNNWrapper(cfg)
model_tst = GNNWrapper(cfg)
# 24.3.21 STOPPER
early_stopper = utils.EarlyStopper(cfg)
model_tr(dset["train"], state_net=state_net) # dataset initalization into the GNN
model_val(dset["validation"], state_net=model_tr.gnn.state_transition_function,
out_net=model_tr.gnn.output_function) # dataset initalization into the GNN
model_tst(dset["test"], state_net=model_tr.gnn.state_transition_function,
out_net=model_tr.gnn.output_function) # dataset initalization into the GNN
# training code
start = time.time()
for epoch in range(1, args.epochs + 1):
acc_train = model_tr.train_step(epoch)
if epoch % 10 == 0:
acc_tst = model_tst.test_step(epoch)
acc_val = model_val.valid_step(epoch)
stp = early_stopper(acc_train, acc_val, acc_tst, epoch)
# return -1 keeps training the model!
if stp == -1:
print(f"{early_stopper.best_epoch}, \t {early_stopper.best_train}, \t, {early_stopper.best_val}, \t {early_stopper.best_test}")
break
# model_tst.test_step(epoch)
time_sample = time.time() - start
print(f"time taken for one set: {str(time_sample)} seconds")
time_whole = time.time() - start_0
print(f"time taken for the whole experiment: {str(time_whole)} seconds")
# if args.save_model:
# torch.save(model.gnn.state_dict(), "mnist_cnn.pt")
if __name__ == '__main__':
main()