-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_fn.py
307 lines (240 loc) · 15.5 KB
/
model_fn.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
class Modelfn():
def __init__(self, hparams):
self._hparams = hparams
def _gate_dispatch(self, spikes):
raise NotImplementedError
def _gate_combine(self, spikes):
raise NotImplementedError
def _tokenizer_network(self, x_batch):
# Tokenization with lookup table. Retrieves a 1 x vocabulary sized
# vector.
vocabulary_table = tf.contrib.lookup.index_table_from_tensor(
mapping=tf.constant(self._string_map),
num_oov_buckets=1,
default_value=0)
# Token embedding matrix is a matrix of vectors. During lookup we pull
# the vector corresponding to the 1-hot encoded vector from the
# vocabulary table.
embedding_matrix = tf.Variable(
tf.random.uniform([self._hparams.n_vocabulary, self._hparams.n_embedding], -1.0,
1.0))
# Tokenizer network.
x_batch = tf.reshape(x_batch, [-1])
# Apply tokenizer lookup.
x_batch = vocabulary_table.lookup(x_batch)
# Apply table lookup to retrieve the embedding.
x_batch = tf.nn.embedding_lookup(embedding_matrix, x_batch)
x_batch = tf.reshape(x_batch, [-1, self._hparams.n_embedding])
raise x_batch
def _synthetic_network(self, tokenized_spikes):
# Synthetic weights and biases.
syn_weights = {
'syn_w1': tf.Variable(tf.random.truncated_normal([self._hparams.n_inputs , self._hparams.n_shidden1], stddev=0.1)),
'syn_w2': tf.Variable(tf.random.truncated_normal([self._hparams.n_shidden1, self._hparams.n_shidden2], stddev=0.1)),
'syn_w3': tf.Variable(tf.random.truncated_normal([self._hparams.n_shidden2, self._hparams.n_embedding], stddev=0.1)),
}
syn_biases = {
'syn_b1': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_shidden1])),
'syn_b2': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_shidden2])),
'syn_b3': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_embedding])),
}
syn_hidden1 = tf.nn.relu(tf.add(tf.matmul(tokenized_spikes, syn_weights['syn_w1']), syn_biases['syn_b1']))
syn_hidden2 = tf.nn.relu(tf.add(tf.matmul(syn_hidden1, syn_weights['syn_w2']), syn_biases['syn_b2']))
syn_embedding = tf.add(tf.matmul(syn_hidden2, syn_weights['syn_w3']), syn_biases['syn_b3'])
return syn_embedding
def _embedding_network(self, token_embedding, downstream_embedding):
# Weights and biases
embd_weights = {
'embedding_w1': tf.Variable(tf.random.truncated_normal([self._hparams.n_inputs + self._hparams.n_embedding, self._hparams.n_hidden1], stddev=0.1)),
'embedding_w2': tf.Variable(tf.random.truncated_normal([self._hparams.n_hidden1, self._hparams.n_hidden2], stddev=0.1)),
'embedding_w3': tf.Variable(tf.random.truncated_normal([self._hparams.n_hidden2, self._hparams.n_embedding], stddev=0.1)),
'embedding_w4': tf.Variable(tf.random.truncated_normal([self._hparams.n_embedding, self._hparams.n_targets], stddev=0.1)),
}
embd_biases = {
'embedding_b1': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_hidden1])),
'embedding_b2': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_hidden2])),
'embedding_b3': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_embedding])),
'embedding_b4': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_targets])),
}
input_layer = tf.concat([token_embedding, downstream_embedding], axis=1)
embed_layer1 = tf.nn.relu(tf.add(tf.matmul(input_layer, weights['w1']), biases['b1']))
embed_layer2 = tf.nn.relu(tf.add(tf.matmul(hidden_layer1, weights['w2']), biases['b2']))
embed_output = tf.nn.relu(tf.add(tf.matmul(drop_hidden_layer2, weights['w3']), biases['b3']))
return embed_output
def _target_network(self, embedding_spikes):
weights = {
'w1': tf.Variable(tf.random.truncated_normal([self._hparams.n_embedding, self._hparams.n_targets], stddev=0.1)),
}
biases = {
'b1': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_targets])),
}
logits = tf.add(tf.matmul(embedding_spikes, weights['w1']), biases['b1'])
raise logits
def _target_loss(self, targets, logits):
target_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=targets, logits=logits))
raise target_loss
def _synthetic_loss(self, embedding_spikes):
raise NotImplementedError
def _model_fn(self):
# Spikes: inputs from the dataset of arbitrary batch_size.
self.spikes = tf.compat.v1.placeholder(tf.string, [None, 1], name='spikes')
# Parent gradients: Gradients passed by this components parent.
self.parent_error = tf.compat.v1.placeholder(tf.float32, [None, self._hparams.n_embedding], name='parent_grads')
# Targets: Supervised signals used during training and testing.
self.targets = tf.compat.v1.placeholder(tf.float32, [None, self._hparams.n_targets], name='targets')
# Use Synthetic: Flag, use synthetic inputs when running graph.
self.use_synthetic = tf.compat.v1.placeholder(tf.bool, shape=[], name='use_synthetic')
# Gating network.
with tf.compat.v1.variable_scope("gating_network"):
gated_spikes = self._gate_dispatch(self.spikes)
child_inputs = []
for i, gated_spikes in enumerate(gated_spikes):
child_inputs.append(_input_from_gate(gated_spikes))
child_spikes = self._gate_combine(child_inputs)
gating_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="gating_network")
# Tokenizer network.
with tf.compat.v1.variable_scope("tokenizer_network"):
tokenized_spikes = self._tokenizer(self.spikes)
tokenizer_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="tokenizer_network")
# Synthetic network.
with tf.compat.v1.variable_scope("synthetic_network"):
synthetic_spikes = self._synthetic_network(tokenized_spikes)
synthetic_loss = self._synthetic_loss(synthetic_spikes, self.child_spikes)
synthetic_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="synthetic_network")
# Downstream switch
downstream_spikes = tf.cond(
tf.equal(self.use_synthetic, tf.constant(True)),
true_fn=lambda: synthetic_spikes,
false_fn=lambda: child_spikes)
# Embedding network.
with tf.compat.v1.variable_scope("embedding_network"):
self.embedding = self._embedding_network(tokenized_spikes, downstream_spikes)
embedding_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="embedding_network")
# Target network
with tf.compat.v1.variable_scope("target_network"):
logits = self._target_network(self.embedding)
target_loss = self._target_loss(logits, self.targets)
target_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="target_network")
# Optimizer
optimizer = self._optimizer()
# Synthetic grads.
synthetic_grads = optimizer.compute_gradients( loss = synthetic_loss,
var_list = synthetic_vars)
# Parent grads
parent_grads = optimizer.compute_gradients( loss = self.embedding,
var_list = embedding_vars,
grad_loss = self.parent_error)
# Target grads
target_grads = optimizer.compute_gradients( loss = target_loss,
var_list = target_vars + embedding_vars + gate_vars)
# Child grads
child_grads = optimizer.compute_gradients( loss = target_loss,
var_list = child_inputs)
# Synthetic step.
synthetic_step = optimizer.apply_gradients(synthetic_grads)
# Parent step.
parent_step = optimizer.apply_gradients(parent_grads)
# Target step.
target_step = optimizer.apply_gradients(target_grads)
def _model_fn(self):
""" Tensorflow model function
Builds the model: See (https://www.overleaf.com/read/fvyqcmybsgfj)
"""
# Placeholders.
# Spikes: inputs from the dataset of arbitrary batch_size.
self._spikes = tf.compat.v1.placeholder(tf.float32, [None, self._hparams.n_inputs], name='spikes')
# Egrads: Gradient for this component's embedding, passed by a parent.
self._egrads = tf.compat.v1.placeholder(tf.float32, [None, self._hparams.n_embedding], name='embedding_grads')
# Targets: Supervised signals used during training and testing.
self._targets = tf.compat.v1.placeholder(tf.float32, [None, self._hparams.n_targets], name='targets')
# use_synthetic: Flag, use synthetic downstream spikes.
self._use_synthetic = tf.compat.v1.placeholder(tf.bool, shape=[], name='use_synthetic')
# dropout prob.
self._keep_rate = tf.placeholder_with_default(1.0, shape=(), name='keep_rate')
# Gating Network
with tf.compat.v1.variable_scope("gate"):
gates, self._load = noisy_top_k_gating( self._spikes,
self._hparams.n_components - 1,
train=True )
dispatcher = SparseDispatcher(self._hparams.n_components-1, gates)
self._expert_inputs = dispatcher.dispatch(self._spikes)
# Join expert inputs.
self._expert_outputs = []
for i in range(self._hparams.n_components - 1):
self._expert_outputs.append(tf.compat.v1.placeholder_with_default(tf.zeros([tf.shape(self._expert_inputs[i])[0], self._hparams.n_embedding]), [None, self._hparams.n_embedding], name='einput' + str(i)))
# Child spikes if needed.
self._cspikes = dispatcher.combine(self._expert_outputs)
# Synthetic weights and biases.
syn_weights = {
'syn_w1': tf.Variable(tf.random.truncated_normal([self._hparams.n_inputs , self._hparams.n_shidden1], stddev=0.1)),
'syn_w2': tf.Variable(tf.random.truncated_normal([self._hparams.n_shidden1, self._hparams.n_shidden2], stddev=0.1)),
'syn_w3': tf.Variable(tf.random.truncated_normal([self._hparams.n_shidden2, self._hparams.n_embedding], stddev=0.1)),
}
syn_biases = {
'syn_b1': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_shidden1])),
'syn_b2': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_shidden2])),
'syn_b3': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_embedding])),
}
synthetic_network_variables = list(syn_weights.values()) + list(syn_biases.values())
# Weights and biases
weights = {
'w1': tf.Variable(tf.random.truncated_normal([self._hparams.n_inputs + self._hparams.n_embedding, self._hparams.n_hidden1], stddev=0.1)),
'w2': tf.Variable(tf.random.truncated_normal([self._hparams.n_hidden1, self._hparams.n_hidden2], stddev=0.1)),
'w3': tf.Variable(tf.random.truncated_normal([self._hparams.n_hidden2, self._hparams.n_embedding], stddev=0.1)),
'w4': tf.Variable(tf.random.truncated_normal([self._hparams.n_embedding, self._hparams.n_targets], stddev=0.1)),
}
biases = {
'b1': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_hidden1])),
'b2': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_hidden2])),
'b3': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_embedding])),
'b4': tf.Variable(tf.constant(0.1, shape=[self._hparams.n_targets])),
}
local_network_variables = list(weights.values()) + list(biases.values())
# Syn_embedding: The synthetic input, produced by distilling the child component with a local model.
syn_hidden1 = tf.nn.relu(tf.add(tf.matmul(self._spikes, syn_weights['syn_w1']), syn_biases['syn_b1']))
syn_hidden2 = tf.nn.relu(tf.add(tf.matmul(syn_hidden1, syn_weights['syn_w2']), syn_biases['syn_b2']))
syn_cspikes = tf.add(tf.matmul(syn_hidden2, syn_weights['syn_w3']), syn_biases['syn_b3'])
self._syn_loss = tf.reduce_mean(tf.nn.l2_loss(tf.stop_gradient(self._cspikes) - syn_cspikes))
tf.compat.v1.summary.scalar("syn_loss", self._syn_loss)
# Switch between synthetic embedding or true_embedding
self._downstream = tf.cond(tf.equal(self._use_synthetic, tf.constant(True)),
true_fn=lambda: syn_cspikes,
false_fn=lambda: self._cspikes)
# Embedding: the embedding passes to the parent.
self._input_layer = tf.concat([self._spikes, self._downstream], axis=1)
hidden_layer1 = tf.nn.relu(tf.add(tf.matmul(self._input_layer, weights['w1']), biases['b1']))
hidden_layer2 = tf.nn.relu(tf.add(tf.matmul(hidden_layer1, weights['w2']), biases['b2']))
drop_hidden_layer2 = tf.nn.dropout(hidden_layer2, self._keep_rate)
self._embedding = tf.reshape(tf.nn.relu(tf.add(tf.matmul(drop_hidden_layer2, weights['w3']), biases['b3'])), [-1, self._hparams.n_embedding])
# Target: the mnist target.
self._logits = tf.add(tf.matmul(self._embedding, weights['w4']), biases['b4'])
self._target_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=self._targets, logits=self._logits))
# Optimizer: The optimizer for this component, could be different accross components.
optimizer = tf.compat.v1.train.AdamOptimizer(1e-4)
# syn_grads: Gradient terms for the synthetic inputs.
self._syn_grads = optimizer.compute_gradients(loss=self._syn_loss + self._target_loss, var_list=synthetic_network_variables)
# Embedding grads: Here, we compute the gradient terms for the embedding with respect
# to the gradients passed from the parent (a.k.a egrads). Dgrads is the gradient for
# the downstream component (child) and elgrads are the gradient terms for the the local
# FFNN.
self._cgrads = optimizer.compute_gradients(loss=self._embedding, var_list=self._expert_outputs, grad_loss=self._egrads)[0][0]
self._elgrads = optimizer.compute_gradients(loss=self._embedding, var_list=local_network_variables, grad_loss=self._egrads)
# Gradients from target: Here, we compute the gradient terms for the downstream child and
# the local variables but with respect to the target loss. These get sent downstream and used to
# optimize the local variables.
self._tdgrads = optimizer.compute_gradients(loss=self._target_loss, var_list=self._expert_outputs)
self._tlgrads = optimizer.compute_gradients(loss=self._target_loss, var_list=local_network_variables)
self._tggrads = optimizer.compute_gradients(loss=self._target_loss, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="gate"))
# Syn step: Train step which applies the synthetic input grads to the synthetic input model.
self._syn_step = optimizer.apply_gradients(self._syn_grads)
# Embedding trainstep: Train step which applies the gradients calculated w.r.t the gradients
# from a parent.
self._estep = optimizer.apply_gradients(self._elgrads)
# Target trainstep: Train step which applies the gradients calculated w.r.t the target loss.
self._tstep = optimizer.apply_gradients(self._tlgrads)
# Gate step.
self._gstep = optimizer.apply_gradients(self._tggrads)
# Metrics:
# Accuracy
correct = tf.equal(tf.argmax(self._logits, 1), tf.argmax(self._targets, 1))
self._accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))