-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
341 lines (287 loc) · 12.1 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import time
import numpy as np
import tensorflow as tf
from utils import get_metrics
from layers import GCN_Layer
class Model:
"""
This class builds the HeteGCN model containing functions for the following:
1. Define the sequence of propagation for the path.
2. Build the tensorflow graph.
3. Define the classification and the auxiliary losses.
4. Define the placeholders and a mechanism to feed them.
5. Training loop.
6. Few other helper functions.
"""
def __init__(self, args):
self.args = args.copy()
self.setup_placeholders()
self.learning_rate = args["learning_rate"]
self.nodes = self.args["nodes"]
self.labels = self.args["labels"]
self.layers = self.args["layers"]
self.n_layers = len(self.layers)
self.layer_specs = self.args["layer_specs"]
self._build_graph()
self._loss()
def _build_graph(self):
####################################################
# Used to extract intermediate outputs for analysis
####################################################
self.model_weights = []
self.layer_outputs = []
self.aux_embeddings = []
####################################################
# features set to None if no features are used in
# in the first layer
features = self.args.get("feature_embeddings", None)
for i, layer in enumerate(self.layers):
with tf.variable_scope(layer + "_%d" % i):
# prepares the layer specification
layer_specs_dict = self._get_layer_specs(layer + "_%d" % i, features)
# HeteGCN Layer
model = GCN_Layer(layer_specs_dict)
# Query Layer Outputs
features = model()
# Apply the activation function for all layers except the final layers.
if i < (self.n_layers - 1):
features = tf.nn.relu(features)
####################################################
# Extracting the intermediate outputs
####################################################
self.layer_outputs.append(features)
self.model_weights.append(model.W)
self.aux_embeddings.append(model.aux_embeddings)
####################################################
self.all_predictions = features
self.predictions = tf.gather(self.all_predictions, self.nodes)
def _loss(self):
"""
Total loss = Classification Loss + Weight Regularization + Embedding Regularization
Classification Loss -> Cross Entropy Loss
"""
self.emb_reg = self.args["emb_reg"] * self._emb_reg()
self.wt_reg = self.args["wt_reg"] * self._wt_reg()
self.pred_error = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
logits=self.predictions, labels=self.labels
)
)
self.loss = self.pred_error + self.wt_reg + self.emb_reg
self.opt = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(
self.loss
)
def _emb_reg(self):
"""
If features or pretrained embeddings are used,
embedding regularization is set to zero.
"""
if "feature_embeddings" in self.placeholders:
return tf.constant(0.0)
return tf.reduce_mean(tf.square(tf.stack(tf.get_collection("embeddings"))))
def _wt_reg(self):
"""
Normalized Weight Regularization
"""
l2_sum = tf.reduce_sum(
[tf.reduce_sum(tf.square(w)) for w in tf.get_collection("weights")]
)
no_of_wts = tf.reduce_sum(
tf.cast(tf.get_collection("#_of_weights"), tf.float32)
)
wt_reg = l2_sum / no_of_wts
wt_reg = tf.where(tf.is_nan(wt_reg), tf.zeros_like(wt_reg), wt_reg)
return wt_reg
def setup_placeholders(self):
"""
Define the required placeholders based on the path.
"""
self.placeholders = {
"nodes": tf.placeholder(tf.int32, shape=(None, 1), name="nodes"),
"labels": tf.placeholder(
tf.float32, shape=(None, self.args["output_dims"]), name="labels"
),
"dropout": tf.placeholder_with_default(0.0, shape=(), name="dropout"),
"learning_rate": tf.placeholder_with_default(
0.0, shape=(), name="learning_rate"
),
}
if "NF" in self.args:
self.placeholders.update(
{"NF": tf.sparse_placeholder(tf.float32, name="NF")}
)
if "FN" in self.args:
self.placeholders.update(
{"FN": tf.sparse_placeholder(tf.float32, name="FN")}
)
if "NN" in self.args:
self.placeholders.update(
{"NN": tf.sparse_placeholder(tf.float32, name="NN")}
)
if "FF" in self.args:
self.placeholders.update(
{"FF": tf.sparse_placeholder(tf.float32, name="FF")}
)
if (
"feature_embeddings" in self.args
and not self.args["feature_embeddings"] is None
):
self.placeholders.update(
{
"feature_embeddings": tf.placeholder(
tf.float32,
shape=(self.args["n_features"], self.args["input_dims"]),
name="support_embeddings",
)
}
)
self.args.update(self.placeholders)
def construct_feed_dict(self, args):
"""
Helper function to construct the feed dict.
"""
feed_dict = {}
for key, placeholder in self.placeholders.items():
feed_dict.update({placeholder: args[key]})
return feed_dict
def _get_layer_specs(self, layer_ID, features=None):
"""
Constructs the specification for each layer with the following details.
1. A
2. X
3. Dropout
"""
layer_specs_dict = self.layer_specs[layer_ID]
layer_specs_dict["dropout"] = self.args["dropout"]
layer_specs_dict["A"] = self.args.get(layer_ID.split("_")[0], None)
if features is not None:
layer_specs_dict["X"] = features
return layer_specs_dict
def fit(self, sess, args):
"""
The training loop is defined in this function.
"""
####################################################
# Setting up the data and auxiliary variables
####################################################
learning_rate = args["learning_rate"]
early_stopping = args["early_stopping"]
decay_rate = args["decay_rate"]
decay_freq = args["decay_freq"]
epochs = args["epochs"]
# Deep Copy
train_data = args["train_data"].copy()
val_data = args["val_data"].copy()
test_data = args["test_data"].copy()
# For Early Stopping
patience = 0
metrics = {}
best_metrics = {}
####################################################
# Training Loop
for epoch in range(1, epochs + 1):
####################################################
# Train
####################################################
start_time = time.time()
# Learning Rate Decay
if epoch % decay_freq == 0:
learning_rate = learning_rate * decay_rate
# Shuffling the training Data
np.random.shuffle(train_data)
nodes = train_data[:, 0].reshape(-1, 1)
labels = train_data[:, 1:]
n_nodes = train_data.shape[0]
batch_start = 0
loss = 0
pred_error = 0
emb_reg = 0
wt_reg = 0
while batch_start < n_nodes:
nodes_batch = nodes[
batch_start : min(n_nodes, batch_start + args["batch_size_train"])
]
labels_batch = labels[
batch_start : min(n_nodes, batch_start + args["batch_size_train"])
]
batch_start = batch_start + args["batch_size_train"]
args["nodes"] = nodes_batch
args["labels"] = labels_batch
args["learning_rate"] = learning_rate
feed_dict = self.construct_feed_dict(args)
outs = sess.run(
[self.opt, self.loss, self.pred_error, self.emb_reg, self.wt_reg],
feed_dict=feed_dict,
)
loss += outs[1] * labels_batch.shape[0]
pred_error += outs[2] * labels_batch.shape[0]
emb_reg += outs[3] * labels_batch.shape[0]
wt_reg += outs[4] * labels_batch.shape[0]
loss = loss / n_nodes
pred_error = pred_error / n_nodes
emb_reg = emb_reg / n_nodes
wt_reg = wt_reg / n_nodes
####################################################
####################################################
# Evaluation
####################################################
epoch_time = time.time() - start_time
# Get Predictions for all the nodes
all_predictions = self.get_predictions(sess, args)
# Get Predicted Class Labels
train_preds = all_predictions[train_data[:, 0]].argmax(axis=1)
val_preds = all_predictions[val_data[:, 0]].argmax(axis=1)
test_preds = all_predictions[test_data[:, 0]].argmax(axis=1)
# Get True Class Labels
y_train = train_data[:, 1:].argmax(axis=1)
y_val = val_data[:, 1:].argmax(axis=1)
y_test = test_data[:, 1:].argmax(axis=1)
# Get metrics like accuracy, micro and macro scores.
train_metrics = get_metrics(train_preds, y_train, "Train")
val_metrics = get_metrics(val_preds, y_val, "Val")
test_metrics = get_metrics(test_preds, y_test, "Test")
metrics.update(train_metrics)
metrics.update(val_metrics)
metrics.update(test_metrics)
####################################################
if args["verbose"]:
print_str = "\nEpoch %03d - Loss: %0.06f PredError: %0.06f EmbReg: %0.06f WtReg: %0.06f (%0.03fs)"
print(
print_str % (epoch, loss, pred_error, emb_reg, wt_reg, epoch_time)
)
print("Train Acc - %0.03f" % metrics["TrainAccuracy"])
print("Val Acc - %0.03f" % metrics["ValAccuracy"])
print("Test Acc - %0.03f" % metrics["TestAccuracy"])
if metrics["ValAccuracy"] > best_metrics.get("ValAccuracy", 0.0):
best_metrics.update(metrics)
dump = self.get_model_params(sess, args)
patience = 0
else:
patience = patience + 1
if patience == early_stopping:
# Early Stopping
break
return best_metrics, dump
def get_predictions(self, sess, args):
"""
Gets predictions for all the nodes.
"""
args["dropout"] = 0.0
feed_dict = self.construct_feed_dict(args)
all_predictions = sess.run(self.all_predictions, feed_dict=feed_dict)
return all_predictions
def get_model_params(self, sess, args):
"""
Extracts various intermediate outputs from the model for analysis.
"""
args["dropout"] = 0.0
feed_dict = self.construct_feed_dict(args)
model_weights = sess.run(self.model_weights, feed_dict=feed_dict)
layer_outputs = sess.run(self.layer_outputs, feed_dict=feed_dict)
aux_embeddings = sess.run(self.aux_embeddings, feed_dict=feed_dict)
model_params = {
"model_weights": model_weights,
"layer_outputs": layer_outputs,
"aux_embeddings": aux_embeddings,
}
return model_params