-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.py
260 lines (201 loc) · 10.3 KB
/
model.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
from layers import GraphConvolution, GraphConvolutionSparse, InnerProductDecoder
from layers import *
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
class Model(object):
def __init__(self, **kwargs):
allowed_kwargs = {'name', 'logging'}
for kwarg in kwargs.keys():
assert kwarg in allowed_kwargs, 'Invalid keyword argument: ' + kwarg
for kwarg in kwargs.keys():
assert kwarg in allowed_kwargs, 'Invalid keyword argument: ' + kwarg
name = kwargs.get('name')
if not name:
name = self.__class__.__name__.lower()
self.name = name
logging = kwargs.get('logging', False)
self.logging = logging
self.vars = {}
def _build(self):
raise NotImplementedError
def build(self):
""" Wrapper for _build() """
with tf.variable_scope(self.name):
self._build()
variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)
self.vars = {var.name: var for var in variables}
def fit(self):
pass
def predict(self):
pass
class GCNModelVAE(Model):
'''VGAE Model for reconstructing graph edges from node representations.'''
def __init__(self, placeholders, num_features, num_nodes, features_nonzero, **kwargs):
super(GCNModelVAE, self).__init__(**kwargs)
self.inputs = placeholders['features']
self.input_dim = num_features
self.features_nonzero = features_nonzero
self.n_samples = num_nodes
self.adj = placeholders['adj']
self.dropout = placeholders['dropout']
self.adj_label = placeholders['adj_orig']
self.weight_norm = 0
self.build()
def encoder(self, inputs):
hidden1 = GraphConvolutionSparse(input_dim=self.input_dim,
output_dim=FLAGS.hidden1,
adj=self.adj,
features_nonzero=self.features_nonzero,
act=tf.nn.relu,
dropout=0.,
logging=self.logging)(inputs)
self.z_mean = GraphConvolution(input_dim=FLAGS.hidden1,
output_dim=FLAGS.hidden2,
adj=self.adj,
act=lambda x: x,
dropout=self.dropout,
logging=self.logging)(hidden1)
self.z_log_std = GraphConvolution(input_dim=FLAGS.hidden1,
output_dim=FLAGS.hidden2,
adj=self.adj,
act=lambda x: x,
dropout=self.dropout,
logging=self.logging)(hidden1)
def get_z(self, random):
z = self.z_mean + tf.random_normal([self.n_samples, FLAGS.hidden2]) * tf.exp(self.z_log_std)
if not random or not FLAGS.vae:
z = self.z_mean
return z
def make_decoder(self):
return
def decoder(self, z):
reconstructions = InnerProductDecoder(input_dim=FLAGS.hidden2,
act=lambda x: x,
dropout=0.,
logging=self.logging)(z)
reconstructions = tf.reshape(reconstructions, [-1])
return reconstructions
def _build(self):
self.encoder(self.inputs)
self.make_decoder()
z = self.get_z(random = True)
z_noiseless = self.get_z(random = False)
if not FLAGS.vae:
z = z_noiseless
self.reconstructions = self.decoder(z)
self.reconstructions_noiseless = self.decoder(z_noiseless)
class GCNModelFeedback(GCNModelVAE):
'''Graphite model for reconstructing graph edges from node representations and intermediate complete graph.'''
def __init__(self, placeholders, num_features, num_nodes, features_nonzero, **kwargs):
super(GCNModelFeedback, self).__init__(placeholders, num_features, num_nodes, features_nonzero, **kwargs)
def make_decoder(self):
self.l0 = GraphiteSparse(input_dim=self.input_dim,
output_dim=FLAGS.hidden3,
act=tf.nn.relu,
dropout=0.,
logging=self.logging)
self.l1 = Graphite(input_dim=FLAGS.hidden2,
output_dim=FLAGS.hidden3,
act=tf.nn.relu,
dropout=0.,
logging=self.logging)
self.l2 = Graphite(input_dim=FLAGS.hidden3,
output_dim=FLAGS.hidden2,
act=lambda x: x,
dropout=self.dropout,
logging=self.logging)
self.l3 = InnerProductDecoder(input_dim=FLAGS.hidden2,
act=lambda x: x,
logging=self.logging)
self.l4 = Scale(input_dim = FLAGS.hidden2, logging = self.logging)
def decoder(self, z):
# recon = self.l3(z)
# recon = tf.nn.sigmoid(recon)
# recon = self.l3(tf.nn.l2_normalize(z, dim = 1))
# recon += tf.ones_like(recon)
# d = tf.reduce_sum(recon, 1)
# d = tf.pow(d, -0.5)
# recon = tf.expand_dims(d, 0) * recon * tf.expand_dims(d, 1)
recon_1 = tf.nn.l2_normalize(z, dim = 1)
recon_2 = tf.ones_like(recon_1)
recon_2 /= tf.sqrt(tf.reduce_sum(recon_2, axis = 1, keepdims = True))
d = tf.matmul(recon_1, tf.expand_dims(tf.reduce_sum(recon_1, axis = 0), 1)) + tf.matmul(recon_2, tf.expand_dims(tf.reduce_sum(recon_2, axis = 0), 1))
d = tf.pow(d, -0.5)
recon_1 *= d
recon_2 *= d
update = self.l1((z, recon_1, recon_2)) + self.l0((self.inputs, recon_1, recon_2))
update = self.l2((update, recon_1, recon_2))
# update = tf.nn.l2_normalize(update, dim = 1)
# update = z + FLAGS.autoregressive_scalar * update
update = (1 - FLAGS.autoregressive_scalar) * z + FLAGS.autoregressive_scalar * update
reconstructions = self.l3(update)
reconstructions = tf.reshape(reconstructions, [-1])
return reconstructions
def sample(self):
z = tf.random_normal([self.n_samples, FLAGS.hidden2])
reconstruction = tf.nn.sigmoid(self.decoder(z))
reconstruction = tf.reshape(reconstruction, [self.n_samples, self.n_samples])
return reconstruction
class GCNModelSiemens(GCNModelVAE):
'''Model for generating graphs from multiple training graphs'''
def __init__(self, placeholders, num_features, num_nodes, features_nonzero, **kwargs):
super(GCNModelSiemens, self).__init__(placeholders, num_features, num_nodes, features_nonzero, **kwargs)
def make_decoder(self):
self.l0 = Dense(input_dim=self.input_dim,
output_dim=FLAGS.hidden3,
act=tf.nn.elu,
dropout=0.,
bias=True,
logging=self.logging)
self.l1 = Dense(input_dim=FLAGS.hidden2,
output_dim=FLAGS.hidden3,
act=tf.nn.elu,
dropout=0.,
bias=True,
logging=self.logging)
self.l2 = Dense(input_dim=FLAGS.hidden3,
output_dim=FLAGS.hidden2,
act=lambda x: x,
dropout=self.dropout,
bias=True,
logging=self.logging)
self.l3 = Dense(input_dim=2 * FLAGS.hidden2,
output_dim=FLAGS.hidden3,
act=tf.nn.elu,
dropout=self.dropout,
bias=True,
logging=self.logging)
self.l3p5 = Dense(input_dim=FLAGS.hidden3,
output_dim=FLAGS.hidden3,
act=tf.nn.elu,
dropout=self.dropout,
bias=True,
logging=self.logging)
self.l4 = Dense(input_dim=FLAGS.hidden3,
output_dim=1,
act=lambda x: x,
dropout=self.dropout,
bias=True,
logging=self.logging)
self.l5 = InnerProductDecoder(input_dim=FLAGS.hidden2,
act=lambda x: x,
logging=self.logging)
def decoder(self, z):
update = self.l1(z) + self.l0(tf.sparse_tensor_to_dense(self.inputs))
update = self.l2(update)
A = tf.abs(tf.expand_dims(update, 1) - tf.expand_dims(update, 0))
B = tf.expand_dims(update, 1) + tf.expand_dims(update, 0)
update = tf.concat((A,B), axis = 2)
update = tf.reshape(update, [-1, 2 * FLAGS.hidden2])
update = self.l3(update)
update = self.l3p5(update)
update = self.l4(update)
reconstructions = tf.squeeze(update)
self.full_recon = tf.nn.sigmoid(reconstructions)
return reconstructions
def sample(self):
z = tf.random_normal([self.n_samples, FLAGS.hidden2])
reconstruction = tf.nn.sigmoid(self.decoder(z))
reconstruction = tf.reshape(reconstruction, [self.n_samples, self.n_samples])
return reconstruction