-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
238 lines (215 loc) · 7.95 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
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
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from data_preprocessing import embeddings, pca, process_data, train_test_random, word_vecs
import intel_extension_for_pytorch as ipex
import json
from model import GCN
import networkx as nx
import numpy as np
import os
import pickle
import shutil
import time
import torch
import torch.nn.functional as F
import torch.optim as optim
torch.set_default_dtype(torch.float64)
def accuracy(output, labels):
output = [0 if n < 0.5 else 1 for n in output]
acc = [1 if a == b else 0 for a, b in zip(output, labels)]
return sum(acc) / len(acc)
print("Creating pipeline metadata file...")
if not os.path.exists("pipeline.json"):
with open("pipeline.json", "w") as f:
data = {"module": 0}
json.dump(data, f)
print("Reading pipeline metadata...")
data = json.load(open("pipeline.json", "r"))
print("Reading in graph directory and splitting into train/test datasets...")
train_files, test_files = train_test_random("data", 0.8)
print("Creating word vectors object...")
word_vectors = word_vecs()
if data["module"] == 0:
print("Creating temp directories...")
os.makedirs("temp/n", exist_ok=True)
os.makedirs("temp/e", exist_ok=True)
print("Creating PCA object...")
node_pca = pca()
edge_pca = pca()
print("Collecting word embeddings...")
node_emb = []
edge_emb = []
data_files = train_files + test_files
i = 0
j = 0
for f in data_files:
g = nx.read_gml(f)
nes = np.asarray(embeddings(word_vectors, g.nodes))
ees = np.asarray(embeddings(word_vectors, [a[2] for a in g.edges.data("relation")]))
for ne in nes:
np.save("temp/n/" + str(i) + "_n.npy", ne)
i += 1
for ee in ees:
np.save("temp/e/" + str(j) + "_e.npy", ee)
j += 1
print("Training PCA models...")
n_filenames = ["temp/n/" + f for f in os.listdir("temp/n") if f.endswith("_n.npy")]
e_filenames = ["temp/e/" + f for f in os.listdir("temp/e") if f.endswith("_e.npy")]
for n in range(0, len(n_filenames), 750):
n_arr = []
n_subset = n_filenames[n : n + 750 if n + 750 < len(n_filenames) else len(n_filenames)]
for ns in n_subset:
n_arr.append(np.load(ns).reshape(1, -1))
n_arr = np.concatenate(tuple(n_arr), axis=0)
try:
node_pca.partial_fit(n_arr)
except:
break
for e in range(0, len(e_filenames), 750):
e_arr = []
e_subset = e_filenames[e : e + 750 if e + 750 < len(e_filenames) else len(e_filenames)]
for es in e_subset:
e_arr.append(np.load(es).reshape(1, -1))
e_arr = np.concatenate(tuple(e_arr), axis=0)
try:
edge_pca.partial_fit(e_arr)
except:
break
print("Deleting temp directory...")
shutil.rmtree("temp")
with open("node_pca.pickle", "wb") as p:
pickle.dump(node_pca, p)
with open("edge_pca.pickle", "wb") as p:
pickle.dump(edge_pca, p)
data["module"] = 1
with open("pipeline.json", "w") as f:
json.dump(data, f)
print("Creating model...")
model = GCN(0.4)
model = model.to(memory_format=torch.channels_last)
optimizer = optim.Adam(model.parameters(), lr=0.005)
model, optimizer = ipex.optimize(model, optimizer=optimizer)
criteria = F.binary_cross_entropy
acc_measure = accuracy
def train(epoch, nf, ef, eadj, nadj, t, label):
tim = time.time()
model.train()
optimizer.zero_grad()
output = model(nf, ef, eadj, nadj, t)
"""loss_train = criteria(torch.tensor(output), torch.LongTensor([[j for i in range(output.shape[0])] for j in label]))
acc_train = acc_measure(torch.tensor(output), torch.LongTensor([[j for i in range(output.shape[0])] for j in label]))"""
x = []
y = []
for i, j in zip(output, label):
x.append(i)
for k in range(i.shape[0]):
y.append(j)
x = torch.cat(x).squeeze()
y = torch.DoubleTensor(y)
loss_train = criteria(x, y)
loss_train.backward()
optimizer.step()
acc_train = acc_measure(x, y)
print("Epoch: ", epoch, 'time: {:.4f}s'.format(time.time() - tim))
"""
print('Epoch: {:04d}'.format(epoch+1),
'loss_train: {:.4f}'.format(loss_train.item()),
'acc_train: {:.4f}'.format(acc_train.item()),
'time: {:.4f}s'.format(time.time() - tim))
"""
return loss_train, acc_train
def test(nf, ef, eadj, nadj, t, label):
model.eval()
output = model(nf, ef, eadj, nadj, t)
"""loss_test = criteria(output, torch.LongTensor([[j for i in range(output.shape[0])] for j in label]))
acc_test = acc_measure(output, torch.LongTensor([[j for i in range(output.shape[0])] for j in label]))"""
x = []
y = []
for i, j in zip(output, label):
x.append(i)
for k in range(i.shape[0]):
y.append(j)
x = torch.cat(x).squeeze()
y = torch.DoubleTensor(y)
loss_test = criteria(x, y)
acc_test = acc_measure(x, y)
return loss_test, acc_test
print("Loading PCA models...")
node_pca = pickle.load(open("node_pca.pickle", "rb"))
edge_pca = pickle.load(open("edge_pca.pickle", "rb"))
log = open("log.txt", "w")
print("Training...")
t_total = time.time()
train_loss_test = []
train_acc_test = []
test_loss_test = []
test_acc_test = []
for epoch in range(10):
train_loss_test = []
train_acc_test = []
test_loss_test = []
test_acc_test = []
train_num = 0
test_num = 0
for f in range(0, len(train_files), 500):
data = []
for fil in train_files[f : f + 500 if f + 500 < len(train_files) else len(train_files)]:
dat = process_data(fil, word_vectors)
data.append(dat)
h_v0 = []
h_e0 = []
eam = []
nam = []
t = []
label = []
for d in data:
if "n_e" in d:
h_v0.append(torch.from_numpy(node_pca.transform(d["n_e"])))
h_e0.append(torch.from_numpy(edge_pca.transform(d["e_e"])))
eam.append(d["edge_adjacency_matrix"])
nam.append(d["node_adjacency_matrix"])
t.append(d["t"])
label.append(d["label"])
l, a = train(epoch, h_v0, h_e0, eam, nam, t, label)
train_loss_test.append(l)
train_acc_test.append(a)
train_num += 1
print("Train set results:",
"loss= {:.4f}".format(sum(train_loss_test)),
"accuracy= {:.4f}".format(sum(train_acc_test) / len(train_acc_test)))
log.write("Train set results: " +
"loss= {:.4f} ".format(sum(train_loss_test)) +
"accuracy= {:.4f}".format(sum(train_acc_test) / len(train_acc_test)) + "\n")
for f in range(0, len(test_files), 500):
data = []
for fil in test_files[f : f + 500 if f + 500 < len(test_files) else len(test_files)]:
dat = process_data(fil, word_vectors)
data.append(dat)
h_v0 = []
h_e0 = []
eam = []
nam = []
t = []
label = []
for d in data:
if "n_e" in d:
h_v0.append(torch.from_numpy(node_pca.transform(d["n_e"])))
h_e0.append(torch.from_numpy(edge_pca.transform(d["e_e"])))
eam.append(d["edge_adjacency_matrix"])
nam.append(d["node_adjacency_matrix"])
t.append(d["t"])
label.append(d["label"])
l, a = test(h_v0, h_e0, eam, nam, t, label)
test_loss_test.append(l)
test_acc_test.append(a)
test_num += 1
print("Test set results:",
"loss= {:.4f}".format(sum(test_loss_test)),
"accuracy= {:.4f}".format(sum(test_acc_test) / len(test_acc_test)))
log.write("Test set results: " +
"loss= {:.4f} ".format(sum(test_loss_test)) +
"accuracy= {:.4f}".format(sum(test_acc_test) / len(test_acc_test)) + "\n")
print("Optimization Finished!")
print("Total time elapsed: {:.4f}s".format(time.time() - t_total))
torch.save(model.state_dict(), "model.pt")
log.close()