-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·195 lines (154 loc) · 7.82 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
from layers import GraphConvolution, GraphConvolutionSparse, InnerProductDecoder
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 ARGA(Model):
def __init__(self, placeholders, num_features, features_nonzero, **kwargs):
super(ARGA, self).__init__(**kwargs)
self.inputs = placeholders['features']
self.input_dim = num_features
self.d2 = placeholders['fake_dist_for_d2']
self.features_nonzero = features_nonzero
self.adj = placeholders['adj']
self.dropout = placeholders['dropout']
self.build()
def _build(self):
with tf.variable_scope('Encoder', reuse=None):
self.hidden1 = GraphConvolutionSparse(input_dim=self.input_dim,
output_dim=FLAGS.hidden1,
adj=self.adj,
features_nonzero=self.features_nonzero,
act=tf.nn.relu,
dropout=self.dropout,
logging=self.logging,
name='e_dense_1')(self.inputs)
self.noise = gaussian_noise_layer(self.hidden1, 0.1)
self.embeddings = GraphConvolution(input_dim=FLAGS.hidden1,
output_dim=FLAGS.hidden2,
adj=self.adj,
act=lambda x: x,
dropout=self.dropout,
logging=self.logging,
name='e_dense_2')(self.noise)
self.z_mean = self.embeddings
self.reconstructions = InnerProductDecoder(input_dim=FLAGS.hidden2,
act=lambda x: x,
logging=self.logging)(self.embeddings)
class ARVGA(Model):
def __init__(self, placeholders, num_features, num_nodes, features_nonzero, **kwargs):
super(ARVGA, 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.build()
def _build(self):
with tf.variable_scope('Encoder'):
self.hidden1 = GraphConvolutionSparse(input_dim=self.input_dim,
output_dim=FLAGS.hidden1,
adj=self.adj,
features_nonzero=self.features_nonzero,
act=tf.nn.relu,
dropout=self.dropout,
logging=self.logging,
name='e_dense_1')(self.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,
name='e_dense_2')(self.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,
name='e_dense_3')(self.hidden1)
self.z = self.z_mean + tf.random_normal([self.n_samples, FLAGS.hidden2]) * tf.exp(self.z_log_std)
self.reconstructions = InnerProductDecoder(input_dim=FLAGS.hidden2,
act=lambda x: x,
logging=self.logging)(self.z)
self.embeddings = self.z
def dense(x, n1, n2, name):
"""
Used to create a dense layer.
:param x: input tensor to the dense layer
:param n1: no. of input neurons
:param n2: no. of output neurons
:param name: name of the entire dense layer.i.e, variable scope name.
:return: tensor with shape [batch_size, n2]
"""
with tf.variable_scope(name, reuse=None):
# np.random.seed(1)
tf.set_random_seed(1)
weights = tf.get_variable("weights", shape=[n1, n2],
initializer=tf.random_normal_initializer(mean=0., stddev=0.01))
bias = tf.get_variable("bias", shape=[n2], initializer=tf.constant_initializer(0.0))
out = tf.add(tf.matmul(x, weights), bias, name='matmul')
#print("model.py: ", out)
return out
class Discriminator(Model):
def __init__(self, **kwargs):
super(Discriminator, self).__init__(**kwargs)
self.act = tf.nn.relu
def construct(self, inputs, reuse = False):
# with tf.name_scope('Discriminator'):
with tf.variable_scope('Discriminator'):
if reuse:
tf.get_variable_scope().reuse_variables()
# np.random.seed(1)
tf.set_random_seed(1)
#print("model.py: ", inputs)
dc_den1 = tf.nn.relu(dense(inputs, FLAGS.hidden2, FLAGS.hidden3, name='dc_den1'))
dc_den2 = tf.nn.relu(dense(dc_den1, FLAGS.hidden3, FLAGS.hidden1, name='dc_den2'))
output = dense(dc_den2, FLAGS.hidden1, 1, name='dc_output')
#print("model.py: ", output)
return output
class Discriminator2(Model):
def __init__(self, **kwargs):
super(Discriminator2, self).__init__(**kwargs)
self.act = tf.nn.relu
def construct(self, inputs, reuse = False):
# with tf.name_scope('Discriminator'):
with tf.variable_scope('Discriminator2'):
if reuse:
tf.get_variable_scope().reuse_variables()
# np.random.seed(1)
tf.set_random_seed(1)
dc_den1 = tf.nn.relu(dense(inputs, 595, FLAGS.hidden3, name='dc2_den1'))
dc_den2 = tf.nn.relu(dense(dc_den1, FLAGS.hidden3, FLAGS.hidden1, name='dc2_den2'))
output = dense(dc_den2, FLAGS.hidden1, 1, name='dc2_output')
return output
def gaussian_noise_layer(input_layer, std):
noise = tf.random_normal(shape=tf.shape(input_layer), mean=0.0, stddev=std, dtype=tf.float32)
return input_layer + noise