forked from sailab-code/gnn
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGNN.py
More file actions
310 lines (257 loc) · 14.7 KB
/
Copy pathGNN.py
File metadata and controls
310 lines (257 loc) · 14.7 KB
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
import tensorflow as tf
import numpy as np
import datetime as time
# class for the core of the architecture
class GNN:
def __init__(self, net, input_dim, output_dim, state_dim, max_it=50, optimizer=tf.train.AdamOptimizer, learning_rate=0.01, threshold=0.01, graph_based=False,
param=str(time.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')), config=None, tensorboard=False, mask_flag=False):
"""
create GNN instance. Feed this parameters:
:net: Net instance - it contains state network, output network, initialized weights, loss function and metric;
:input_dim: dimension of the input
:output_dim: dimension of the output
:state_dim: dimension for the state
:max_it: maximum number of iteration of the state convergence procedure
:optimizer: optimizer instance
:learning_rate: learning rate value
:threshold: value to establish the state convergence
:graph_based: flag to denote a graph based problem
:param: name of the experiment
:config: ConfigProto protocol buffer object, to set configuration options for a session
:tensorboard: boolean flag to activate tensorboard
:mask_flag: boolean flag to activate semisupervised
"""
np.random.seed(0)
tf.set_random_seed(0)
self.tensorboard = tensorboard
self.max_iter = max_it
self.net = net
self.optimizer = optimizer(learning_rate, name="optim")
self.state_threshold = threshold
self.input_dim = input_dim
self.output_dim = output_dim
self.state_dim = state_dim
self.graph_based = graph_based
self.mask_flag = mask_flag
self.build()
self.session = tf.Session(config=config)
#self.session = tf.Session()
self.session.run(tf.global_variables_initializer())
self.init_l = tf.local_variables_initializer()
# parameter to monitor the learning via tensorboard and to save the model
if self.tensorboard:
self.merged_all = tf.summary.merge_all(key='always')
self.merged_train = tf.summary.merge_all(key='train')
self.merged_val = tf.summary.merge_all(key='val')
self.writer = tf.summary.FileWriter('tmp/' + param, self.session.graph)
# self.saver = tf.train.Saver()
# self.save_path = "tmp/" + param + "saves/model.ckpt"
def VariableState(self):
'''Define placeholders for input, output, state, state_old, arch-node conversion matrix'''
# placeholder for input and output
self.comp_inp = tf.placeholder(tf.float32, shape=(None, self.input_dim), name="input")
self.y = tf.placeholder(tf.float32, shape=(None, self.output_dim), name="target")
if self.mask_flag:
self.mask = tf.placeholder(tf.float32, name="mask")
# state(t) & state(t-1)
self.state = tf.placeholder(tf.float32, shape=(None, self.state_dim), name="state")
self.state_old = tf.placeholder(tf.float32, shape=(None, self.state_dim), name="old_state")
# arch-node conversion matrix
self.ArcNode = tf.sparse_placeholder(tf.float32, name="ArcNode")
# node-graph conversion matrix
if self.graph_based:
self.NodeGraph = tf.sparse_placeholder(tf.float32, name="NodeGraph")
else:
self.NodeGraph = tf.placeholder(tf.float32, name="NodeGraph")
def build(self):
'''build the architecture, setting variable, loss, training'''
# network
self.VariableState()
self.loss_op = self.Loop()
# loss
with tf.variable_scope('loss'):
if self.mask_flag:
self.loss = self.net.Loss(self.loss_op[0], self.y, mask=self.mask)
self.val_loss = self.net.Loss(self.loss_op[0], self.y, mask=self.mask)
else:
self.loss = self.net.Loss(self.loss_op[0], self.y)
# val loss
self.val_loss = self.net.Loss(self.loss_op[0], self.y)
if self.tensorboard:
self.summ_loss = tf.summary.scalar('loss', self.loss, collections=['train'])
self.summ_val_loss = tf.summary.scalar('val_loss', self.val_loss, collections=['val'])
# optimizer
with tf.variable_scope('train'):
self.grads = self.optimizer.compute_gradients(self.loss)
self.train_op = self.optimizer.apply_gradients(self.grads, name='train_op')
if self.tensorboard:
for index, grad in enumerate(self.grads):
tf.summary.histogram("{}-grad".format(self.grads[index][1].name), self.grads[index],
collections=['always'])
# metrics
with tf.variable_scope('metrics'):
if self.mask_flag:
self.metrics = self.net.Metric(self.y, self.loss_op[0], mask=self.mask)
else:
self.metrics = self.net.Metric(self.y, self.loss_op[0])
# val metric
with tf.variable_scope('val_metric'):
if self.mask_flag:
self.val_met = self.net.Metric(self.y, self.loss_op[0], mask=self.mask)
else:
self.val_met = self.net.Metric(self.y, self.loss_op[0])
if self.tensorboard:
self.summ_val_met = tf.summary.scalar('val_metric', self.val_met, collections=['always'])
def convergence(self, a, state, old_state, k):
with tf.variable_scope('Convergence'):
# body of the while cicle used to iteratively calculate state
# assign current state to old state
old_state = state
# grub states of neighboring node
gat = tf.gather(old_state, tf.cast(a[:, 1], tf.int32))
# slice to consider only label of the node and that of it's neighbor
# sl = tf.slice(a, [0, 1], [tf.shape(a)[0], tf.shape(a)[1] - 1])
# equivalent code
sl = a[:, 2:]
# concat with retrieved state
inp = tf.concat([sl, gat], axis=1)
# evaluate next state and multiply by the arch-node conversion matrix to obtain per-node states
layer1 = self.net.netSt(inp)
state = tf.sparse_tensor_dense_matmul(self.ArcNode, layer1)
# update the iteration counter
k = k + 1
return a, state, old_state, k
def condition(self, a, state, old_state, k):
# evaluate condition on the convergence of the state
with tf.variable_scope('condition'):
# evaluate distance by state(t) and state(t-1)
outDistance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(state, old_state)), 1) + 0.00000000001)
# vector showing item converged or not (given a certain threshold)
checkDistanceVec = tf.greater(outDistance, self.state_threshold)
c1 = tf.reduce_any(checkDistanceVec)
c2 = tf.less(k, self.max_iter)
return tf.logical_and(c1, c2)
def Loop(self):
# call to loop for the state computation and compute the output
# compute state
with tf.variable_scope('Loop'):
k = tf.constant(0)
res, st, old_st, num = tf.while_loop(self.condition, self.convergence,
[self.comp_inp, self.state, self.state_old, k])
if self.tensorboard:
self.summ_iter = tf.summary.scalar('iteration', num, collections=['always'])
if self.graph_based:
# stf = tf.transpose(tf.matmul(tf.transpose(st), self.NodeGraph))
stf = tf.sparse_tensor_dense_matmul(self.NodeGraph, st)
else:
stf = st
out = self.net.netOut(stf)
return out, num
def Train(self, inputs, ArcNode, target, step, nodegraph=0.0, mask=None):
''' train methods: has to receive the inputs, arch-node matrix conversion, target,
and optionally nodegraph indicator '''
# Creating a SparseTEnsor with the feeded ArcNode Matrix
arcnode_ = tf.SparseTensorValue(indices=ArcNode.indices, values=ArcNode.values,
dense_shape=ArcNode.dense_shape)
if self.graph_based:
nodegraph = tf.SparseTensorValue(indices=nodegraph.indices, values=nodegraph.values,
dense_shape=nodegraph.dense_shape)
if self.mask_flag:
fd = {self.NodeGraph: nodegraph, self.comp_inp: inputs,
self.state: np.zeros((ArcNode.dense_shape[0], self.state_dim)),
self.state_old: np.ones((ArcNode.dense_shape[0], self.state_dim)),
self.ArcNode: arcnode_, self.y: target, self.mask: mask}
else:
fd = {self.NodeGraph: nodegraph, self.comp_inp: inputs, self.state: np.zeros((ArcNode.dense_shape[0], self.state_dim)),
self.state_old: np.ones((ArcNode.dense_shape[0], self.state_dim)),
self.ArcNode: arcnode_, self.y: target}
if self.tensorboard:
_, loss, loop, merge_all, merge_tr = self.session.run(
[self.train_op, self.loss, self.loss_op, self.merged_all, self.merged_train],
feed_dict=fd)
if step % 100 == 0:
self.writer.add_summary(merge_all, step)
self.writer.add_summary(merge_tr, step)
else:
_, loss, loop = self.session.run(
[self.train_op, self.loss, self.loss_op],
feed_dict=fd)
return loss, loop[1]
def Validate(self, inptVal, arcnodeVal, targetVal, step, nodegraph=0.0, mask=None):
""" Takes care of the validation of the model - it outputs, regarding the set given as input,
the loss value, the accuracy (custom defined in the Net file), the number of iteration
in the convergence procedure """
arcnode_ = tf.SparseTensorValue(indices=arcnodeVal.indices, values=arcnodeVal.values,
dense_shape=arcnodeVal.dense_shape)
if self.graph_based:
nodegraph = tf.SparseTensorValue(indices=nodegraph.indices, values=nodegraph.values,
dense_shape=nodegraph.dense_shape)
if self.mask_flag:
fd_val = {self.NodeGraph: nodegraph, self.comp_inp: inptVal,
self.state: np.zeros((arcnodeVal.dense_shape[0], self.state_dim)),
self.state_old: np.ones((arcnodeVal.dense_shape[0], self.state_dim)),
self.ArcNode: arcnode_,
self.y: targetVal,
self.mask: mask}
else:
fd_val = {self.NodeGraph: nodegraph, self.comp_inp: inptVal,
self.state: np.zeros((arcnodeVal.dense_shape[0], self.state_dim)),
self.state_old: np.ones((arcnodeVal.dense_shape[0], self.state_dim)),
self.ArcNode: arcnode_,
self.y: targetVal}
if self.tensorboard:
loss_val, loop, merge_all, merge_val, metr = self.session.run(
[self.val_loss, self.loss_op, self.merged_all, self.merged_val, self.metrics], feed_dict=fd_val)
self.writer.add_summary(merge_all, step)
self.writer.add_summary(merge_val, step)
else:
loss_val, loop, metr = self.session.run(
[self.val_loss, self.loss_op, self.metrics], feed_dict=fd_val)
return loss_val, metr, loop[1]
def Evaluate(self, inputs, st, st_old, ArcNode, target):
'''evaluate method with initialized state -- not used for the moment: has to receive the inputs,
initialization for state(t) and state(t-1),
arch-node matrix conversion, target -- gives as output the accuracy on the set given as input'''
arcnode_ = tf.SparseTensorValue(indices=ArcNode.indices, values=ArcNode.values,
dense_shape=ArcNode.dense_shape)
fd = {self.comp_inp: inputs, self.state: st, self.state_old: st_old,
self.ArcNode: arcnode_, self.y: target}
_ = self.session.run([self.init_l])
met = self.session.run([self.metrics], feed_dict=fd)
return met
def Evaluate(self, inputs, ArcNode, target, nodegraph=0.0):
'''evaluate methods: has to receive the inputs, arch-node matrix conversion, target
-- gives as output the accuracy on the set given as input'''
arcnode_ = tf.SparseTensorValue(indices=ArcNode.indices, values=ArcNode.values,
dense_shape=ArcNode.dense_shape)
if self.graph_based:
nodegraph = tf.SparseTensorValue(indices=nodegraph.indices, values=nodegraph.values,
dense_shape=nodegraph.dense_shape)
fd = {self.NodeGraph: nodegraph, self.comp_inp: inputs, self.state: np.zeros((ArcNode.dense_shape[0], self.state_dim)),
self.state_old: np.ones((ArcNode.dense_shape[0], self.state_dim)),
self.ArcNode: arcnode_, self.y: target}
_ = self.session.run([self.init_l])
met = self.session.run([self.metrics], feed_dict=fd)
return met
def Predict(self, inputs, st, st_old, ArcNode):
''' predict methods with initialized state -- not used for the moment:: has to receive the inputs,
initialization for state(t) and state(t-1),
arch-node matrix conversion -- gives as output the output values of the output function (all the nodes output
for all the graphs (if node-based) or a single output for each graph (if graph based) '''
arcnode_ = tf.SparseTensorValue(indices=ArcNode.indices, values=ArcNode.values,
dense_shape=ArcNode.dense_shape)
fd = {self.comp_inp: inputs, self.state: st, self.state_old: st_old,
self.ArcNode: arcnode_}
pr = self.session.run([self.loss_op], feed_dict=fd)
return pr[0]
def Predict(self, inputs, ArcNode, nodegraph=0.0):
''' predict methods: has to receive the inputs, arch-node matrix conversion -- gives as output the output
values of the output function (all the nodes output
for all the graphs (if node-based) or a single output for each graph (if graph based) '''
arcnode_ = tf.SparseTensorValue(indices=ArcNode.indices, values=ArcNode.values,
dense_shape=ArcNode.dense_shape)
fd = {self.comp_inp: inputs, self.state: np.zeros((ArcNode.dense_shape[0], self.state_dim)),
self.state_old: np.ones((ArcNode.dense_shape[0], self.state_dim)),
self.ArcNode: arcnode_}
pr = self.session.run([self.loss_op], feed_dict=fd)
return pr[0]