-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.py
299 lines (251 loc) · 12.6 KB
/
models.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
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
from layers import *
from metrics import align_loss, class_loss, label_loss
from inits import *
flags = tf.app.flags
FLAGS = flags.FLAGS
class Model(object):
def __init__(self, **kwargs):
allowed_kwargs = {'name', 'logging', "task", "rel_update"}
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
task = kwargs.get('task')
self.task = task
rel_update = kwargs.get('rel_update')
self.rel_update = rel_update
self.vars = {}
self.placeholders = {}
self.layers = []
self.activations = []
self.inputs = None
self.outputs = None
self.loss = 0
self.accuracy = 0
self.optimizer = None
self.opt_op = None
def _build(self):
raise NotImplementedError
def build(self):
""" Wrapper for _build() """
with tf.variable_scope(self.name):
self._build()
# Build sequential layer model
self.activations.append(self.inputs)
for layer in self.layers:
hidden = layer(self.activations[-1])
self.activations.append(hidden)
self.outputs = self.activations[-1]
# Store model variables for easy access
variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)
self.vars = {var.name: var for var in variables}
# Build metrics
self._loss()
self._accuracy()
self.opt_op = self.optimizer.minimize(self.loss)
def predict(self):
pass
def _loss(self):
raise NotImplementedError
def _accuracy(self):
raise NotImplementedError
def save(self, sess=None):
if not sess:
raise AttributeError("TensorFlow session not provided.")
saver = tf.train.Saver(self.vars)
save_path = saver.save(sess, "tmp/%s.ckpt" % self.name)
print("Model saved in file: %s" % save_path)
def load(self, sess=None):
if not sess:
raise AttributeError("TensorFlow session not provided.")
saver = tf.train.Saver(self.vars)
save_path = "tmp/%s.ckpt" % self.name
saver.restore(sess, save_path)
print("Model restored from file: %s" % save_path)
class AutoRGCN_Align(Model):
def __init__(self, placeholders, input_dim, hidden_dim, output_dim, train_labels, dataset, REL=None,
mode="None", embed="random", alpha=0.5, beta=0.5, layer_num=0, sparse_inputs=False,
featureless=True, rel_align_loss=False, names_neg=None, **kwargs):
super(AutoRGCN_Align, self).__init__(**kwargs)
print("####### Model: AutoRGCN_Align #########")
# inputs for layers
self.inputs = placeholders['features']
self.input_dim = input_dim
self.output_dim = output_dim
self.hidden_dim = hidden_dim
self.placeholders = placeholders
self.train_labels = train_labels
self.REL = REL
self.sparse_inputs = sparse_inputs
self.featureless = featureless
self.rel_align_loss = rel_align_loss
self.mode = mode
self.embed = embed
self.alpha = alpha
self.beta = beta
self.layer_num = layer_num
self.dataset = dataset
self.names_neg = names_neg
if FLAGS.optim == "GD":
self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=FLAGS.learning_rate)
elif FLAGS.optim == "Adam":
self.optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate)
else:
exit("Wrong optimizer")
self.build()
def _loss(self):
if self.task == "align":
# knowledge graph alignment
self.align_loss = align_loss(self.outputs[0], self.train_labels, FLAGS.gamma, FLAGS.num_negs, self.names_neg[0], FLAGS.loss_mode)
if not self.rel_align_loss:
self.loss += self.align_loss
else:
self.rel_loss = rel_align_loss(self.outputs[1], self.REL, FLAGS.gamma, FLAGS.num_rel_negs, self.names_neg[1], FLAGS.loss_mode)
self.loss += self.align_loss * (1-FLAGS.rel_weight) + self.rel_loss * FLAGS.rel_weight
elif self.task == "class":
# multi-class classification
self.loss += class_loss(self.outputs[0], self.train_labels[0], self.train_labels[1])
elif self.task == "label":
# multi-label classification
self.loss += label_loss(self.outputs[0], self.train_labels[0], self.train_labels[1])
def build(self):
""" Wrapper for _build() """
with tf.variable_scope(self.name):
if self.task == "align":
self._build_align()
elif self.task in ["class", "label"]:
self._build_class()
else:
exit("build error! wrong task!")
# Build sequential layer model
self.activations.append(self.inputs)
for layer in self.layers:
hidden = layer(self.activations[-1])
self.activations.append(hidden)
# output the last layer representation for prediction
self.outputs = self.activations[-1]
# For KG alignment task, if we use TransD, then only the first half embeddings
# are used for candidate ranking
if self.mode == "TransD" and self.task == "align":
ent_dim = tf.cast(tf.shape(self.outputs[0])[1]/2, tf.int32)
rel_dim = tf.cast(tf.shape(self.outputs[1])[1]/2, tf.int32)
self.outputs = [self.outputs[0][:, :ent_dim], self.outputs[1][:, :rel_dim]]
# Store model variables for easy access
variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)
self.vars = {var.name: var for var in variables}
# Build metrics
self._loss()
self.opt_op = self.optimizer.minimize(self.loss)
def _build_align(self):
init_e = trunc_normal
init_r = trunc_normal
if self.mode == "DistMult":
init_e = one_trunc_normal
init_r = one_trunc_normal
self.layers.append(InitLayer(input_dim=self.input_dim,
output_dim=self.hidden_dim,
placeholders=self.placeholders,
embed=self.embed,
dataset=self.dataset,
featureless=self.featureless,
sparse_inputs=self.sparse_inputs,
init=[init_e, init_r],
logging=self.logging))
self.layers.append(AutoRelGraphConvolution(input_dim=self.hidden_dim,
output_dim=self.hidden_dim,
placeholders=self.placeholders,
act=tf.nn.relu,
dropout=False,
alpha=self.alpha,
beta=self.beta,
mode=self.mode,
bias=False,
transform=False,
init=[trunc_normal, trunc_normal],
logging=self.logging))
for i in range(self.layer_num):
self.layers.append(AutoRelGraphConvolution(input_dim=self.hidden_dim,
output_dim=self.hidden_dim,
placeholders=self.placeholders,
act=tf.nn.relu,
dropout=False,
alpha=self.alpha,
beta=self.beta,
mode=self.mode,
bias=False,
transform=False,
init=[trunc_normal, trunc_normal],
logging=self.logging))
self.layers.append(AutoRelGraphConvolution(input_dim=self.hidden_dim,
output_dim=self.output_dim,
placeholders=self.placeholders,
act=lambda x: x,
dropout=False,
alpha=self.alpha,
beta=self.beta,
mode=self.mode,
bias=False,
transform=False,
init=[trunc_normal, trunc_normal],
logging=self.logging))
for layer in self.layers:
layer.rel_update = self.rel_update
def _build_class(self):
init_e = trunc_normal
init_r = trunc_normal
if self.mode == "DistMult":
init_e = one_trunc_normal
init_r = ones_static
self.layers.append(InitLayer(input_dim=self.input_dim,
output_dim=self.hidden_dim,
placeholders=self.placeholders,
embed=self.embed,
dataset=self.dataset,
featureless=self.featureless,
sparse_inputs=self.sparse_inputs,
init=[init_e, init_r],
logging=self.logging))
self.layers.append(AutoRelGraphConvolution(input_dim=self.hidden_dim,
output_dim=self.hidden_dim,
placeholders=self.placeholders,
act=tf.nn.relu,
dropout=False,
alpha=self.alpha,
beta=self.beta,
mode=self.mode,
bias=False,
transform=False,
init=[glorot, glorot],
logging=self.logging))
for i in range(self.layer_num):
self.layers.append(AutoRelGraphConvolution(input_dim=self.hidden_dim,
output_dim=self.hidden_dim,
placeholders=self.placeholders,
act=tf.nn.relu,
dropout=False,
alpha=self.alpha,
beta=self.beta,
mode=self.mode,
bias=False,
transform=False,
init=[glorot, glorot],
logging=self.logging))
self.layers.append(AutoRelGraphConvolution(input_dim=self.hidden_dim,
output_dim=self.output_dim,
placeholders=self.placeholders,
act=lambda x:x,
dropout=False,
alpha=self.alpha,
beta=self.beta,
mode=self.mode,
bias=False,
transform=True,
truncate_ent=True,
init=[glorot, glorot],
logging=self.logging))
for layer in self.layers:
layer.rel_update = self.rel_update