forked from chingyaoc/ggnn.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_ggnn.py
147 lines (127 loc) · 6.06 KB
/
main_ggnn.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
import argparse
import random
import torch
import torch.nn as nn
import torch.optim as optim
import pickle
from utils.model import GGNN
from utils.model import ClassPrediction
from utils.train_ggnn import train
from utils.test_ggnn import test
from utils.data.dataset import MonoLanguageProgramData
from utils.data.dataloader import bAbIDataloader
from tensorboardX import SummaryWriter
import os
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)
parser.add_argument('--train_batch_size', type=int, default=5, help='input batch size')
parser.add_argument('--test_batch_size', type=int, default=5, help='input batch size')
parser.add_argument('--state_dim', type=int, default=30, help='GGNN hidden state size')
parser.add_argument('--n_steps', type=int, default=10, help='propogation steps number of GGNN')
parser.add_argument('--niter', type=int, default=150, help='number of epochs to train for')
parser.add_argument('--lr', type=float, default=0.01, help='learning rate')
parser.add_argument('--cuda', action='store_true', help='enables cuda')
parser.add_argument('--verbal', type=bool, default=True, help='print training info or not')
parser.add_argument('--manualSeed', type=int, help='manual seed')
parser.add_argument('--n_classes', type=int, default=10, help='manual seed')
parser.add_argument('--directory', default="program_data/github_java_sort_function_babi", help='program data')
parser.add_argument('--model_path', default="model/model.ckpt", help='path to save the model')
parser.add_argument('--n_hidden', type=int, default=50, help='number of hidden layers')
parser.add_argument('--size_vocabulary', type=int, default=59, help='maximum number of node types')
parser.add_argument('--is_training_ggnn', type=bool, default=True, help='Training GGNN or BiGGNN')
parser.add_argument('--training', action="store_true",help='is training')
parser.add_argument('--testing', action="store_true",help='is testing')
parser.add_argument('--training_percentage', type=float, default=1.0 ,help='percentage of data use for training')
parser.add_argument('--log_path', default="logs/" ,help='log path for tensorboard')
parser.add_argument('--epoch', type=int, default=0, help='epoch to test')
parser.add_argument('--pretrained_embeddings', default="embedding/fast_pretrained_vectors.pkl", help='pretrained embeddings url, there are 2 objects in this file, the first object is the embedding matrix, the other is the lookup dictionary')
opt = parser.parse_args()
print(opt)
if opt.training and opt.log_path != "":
previous_runs = os.listdir(opt.log_path)
if len(previous_runs) == 0:
run_number = 1
else:
run_number = max([int(s.split("run-")[1]) for s in previous_runs]) + 1
writer = SummaryWriter("%s/run-%03d" % (opt.log_path, run_number))
# writer = SummaryWriter(opt.log_path)
else:
writer = None
if opt.manualSeed is None:
opt.manualSeed = random.randint(1, 10000)
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
if opt.cuda:
torch.cuda.manual_seed_all(opt.manualSeed)
# This part is the implementation to illustrate Graph-Level output from program data
def main(opt):
with open(opt.pretrained_embeddings, 'rb') as fh:
embeddings, embed_lookup = pickle.load(fh,encoding='latin1')
# print(len(embeddings))
# print(embed_lookup.keys())
opt.embeddings = embeddings
opt.embed_lookup = embed_lookup
if opt.training:
train_dataset = MonoLanguageProgramData(opt.size_vocabulary, opt.embeddings, opt.directory, True, opt.n_classes, opt.training_percentage)
train_dataloader = bAbIDataloader(train_dataset, batch_size=opt.train_batch_size, \
shuffle=True, num_workers=2)
test_dataset = MonoLanguageProgramData(opt.size_vocabulary, opt.embeddings, opt.directory, False, opt.n_classes)
test_dataloader = bAbIDataloader(test_dataset, batch_size=opt.test_batch_size, \
shuffle=True, num_workers=2)
opt.annotation_dim = 1 # for bAbI
if opt.training:
opt.n_edge_types = train_dataset.n_edge_types
opt.n_node = train_dataset.n_node
else:
opt.n_edge_types = test_dataset.n_edge_types
opt.n_node = test_dataset.n_node
if opt.testing:
filename = "{}.{}".format(opt.model_path, opt.epoch)
epoch = opt.epoch
else:
filename = opt.model_path
epoch = -1
if os.path.exists(filename):
if opt.testing:
print("Using No. {} saved model....".format(opt.epoch))
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
epochs = os.listdir(dirname)
if len(epochs) > 0:
for s in epochs:
if s.startswith(basename) and basename != s:
x = s.split(os.extsep)
e = x[len(x) - 1]
epoch = max(epoch, int(e))
if epoch != -1:
print("Using No. {} of the saved models...".format(epoch))
filename = "{}.{}".format(opt.model_path, epoch)
if epoch != -1:
print("Using No. {} saved model....".format(epoch))
else:
print("Using saved model....")
net = torch.load(filename)
else:
net = GGNN(opt)
net.double()
criterion = nn.CrossEntropyLoss()
if opt.cuda:
net.cuda()
criterion.cuda()
optimizer = optim.Adam(net.parameters(), lr=opt.lr)
# embedding_matrix = train_dataset.embedding_matrix
if opt.training:
for epoch in range(epoch+1, epoch + opt.niter):
train(epoch, train_dataloader, net, criterion, optimizer, opt, writer)
writer.close()
if opt.testing:
filename = "{}.{}".format(opt.model_path, epoch)
if os.path.exists(filename):
net = torch.load(filename)
net.cuda()
optimizer = optim.Adam(net.parameters(), lr=opt.lr)
test(test_dataloader, net, criterion, optimizer, opt)
if __name__ == "__main__":
main(opt)