-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
557 lines (481 loc) · 23.7 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""Fully Asynchronous Learning Component.
This file contains the Mach class. This class runs on its own thread.
It contains a unqiue copy to the dataset and trains against the global loss.
During training it first queries the final activation layer ('embedding') from
its child and uses this as additional input to its own model. The class concurrently
trains a distillation model or ('synthetic input') using the child's outputs.
The target loss and the distilled model train concurrently.
During testing/validation or when a post-sequential node queries us, we do not
recursively query our own child, instead we use the distilled model as input to
our own network. This cuts the recursion and stops gradients from passing farther
through the network.
Example:
$ mach = Mach ( name = 0,
mnist = load_data_and_constants(),
hparams = hparams_from_args(),
tblogger = TBLogger())
"""
from loguru import logger
import numpy as np
import tensorflow as tf
import threading
class Mach:
def __init__(self, name, dataset, hparams, tblogger):
"""Initialize a Mach learning component.
Args:
name: component name.
dataset: dataset from utils.load_data_and_constants, specified via arguments.
hparams: component hyperparameters from arguments.
tblogger: tensorboard logger class.
"""
self.name = name
self._hparams = hparams
self._train = dataset['train']
self._test = dataset['test']
self._batch = 0
self._tblogger = tblogger
self._child = None
self._running = False
self._graph = tf.Graph()
self._session = tf.compat.v1.Session(graph=self._graph)
with self._graph.as_default():
self._model_fn()
self._session.run(tf.compat.v1.global_variables_initializer())
def set_child(self, child):
"""Sets passed component as child.
"""
assert (type(child) == type(self))
self._child = child
def start(self):
"""Start the training loop. Stops on call to stop.
"""
self.running = True
self._thread = threading.Thread(target=self._run, daemon=True)
logger.info('starting thread {}', self.name)
self._thread.start()
def stop(self):
"""Joins training thread and stops.
"""
self.running = False
logger.info('joining thread {}', self.name)
self._thread.join()
def next_batch(self):
# Get next training batch
if self._batch >= len(
self._train
): # self._train is already divided into batches here
# epoch is done
self._batch = 0
for t in range(0, len(self._train)):
np.random.shuffle(self._train[t])
training_batch = self._train[self._batch]
batch_x = [t[0] for t in training_batch]
batch_y = [t[1] for t in training_batch]
# update to the next batch
self._batch = self._batch + 1
return batch_x, batch_y
def _run(self):
"""Loops train and test continually.
"""
step = 0
while self.running:
# Training step.
batch_x, batch_y = self.next_batch()
# Train child.
self._run_graph(batch_x,
batch_y,
keep_prop=0.95,
use_synthetic=False,
do_train=True,
do_metrics=False)
# Train synthetic net.
self._run_graph(batch_x,
batch_y,
keep_prop=0.95,
use_synthetic=True,
do_train=True,
do_metrics=False)
# Validation and tensorboard logs.
if step % self._hparams.n_print == 0:
# Train / Validation with and without synthetic models.
tr_x, tr_y = self.next_batch()
val_x = [t[0] for t in self._test]
val_y = [t[1] for t in self._test]
syn_tr_out = self._run_graph(tr_x,
tr_y,
keep_prop=1.0,
use_synthetic=True,
do_train=False,
do_metrics=True)
tr_out = self._run_graph(tr_x,
tr_y,
keep_prop=1.0,
use_synthetic=False,
do_train=False,
do_metrics=True)
syn_val_out = self._run_graph(val_x,
val_y,
keep_prop=1.0,
use_synthetic=True,
do_train=False,
do_metrics=True)
val_out = self._run_graph(val_x,
val_y,
keep_prop=1.0,
use_synthetic=False,
do_train=False,
do_metrics=True)
# Accuracy metrics.
self._tblogger.log_scalar('validation accuracy using child',
syn_val_out['accuracy'][0], step)
self._tblogger.log_scalar('validation accuracy using synthetic',
val_out['accuracy'][0], step)
self._tblogger.log_scalar('training accuracy using synthetic',
syn_tr_out['accuracy'][0], step)
self._tblogger.log_scalar('training_accuracy using child',
tr_out['accuracy'][0], step)
# Target loss.
self._tblogger.log_scalar(
'training target loss using synthetic',
syn_tr_out['target_loss'][0], step)
self._tblogger.log_scalar('training target loss using child',
tr_out['target_loss'][0], step)
# Synthetic loss.
self._tblogger.log_scalar('training synthetic loss',
tr_out['synthetic_loss'], step)
self._tblogger.log_scalar('validation synthetic loss',
val_out['synthetic_loss'], step)
# Inputs score.
self._tblogger.log_scalar('inputs pruning score',
tr_out['inputs_pruning_score'][0],
step)
self._tblogger.log_scalar('inputs absolute sum of gradients',
tr_out['inputs_absolute_gradient'][0],
step)
self._tblogger.log_scalar('inputs weight magnitude',
tr_out['inputs_weight_magnitude'][0],
step)
# Downstream score.
self._tblogger.log_scalar('downstream pruning score',
tr_out['downstream_pruning_score'][0],
step)
self._tblogger.log_scalar(
'downstream absolute sum of gradients',
tr_out['downstream_absolute_gradient'][0], step)
self._tblogger.log_scalar(
'downstream weight magnitude',
tr_out['downstream_weight_magnitude'][0], step)
# Downstream Integrate gradients.
self._tblogger.log_scalar(
'downstream integrated gradients score',
syn_val_out['downstream_integrated_gradients_score'], step)
self._tblogger.log_scalar(
'inputs integrated gradients score',
syn_val_out['input_integrated_gradients_score'], step)
logger.info('{}: [val: {} - {} tr: {} - {}]', self.name,
val_out['accuracy'][0], syn_val_out['accuracy'][0],
tr_out['accuracy'][0], syn_tr_out['accuracy'][0])
if step > self._hparams.n_train_steps:
self.running = False
step += 1
def _run_graph(self, spikes, targets, keep_prop, use_synthetic, do_train,
do_metrics):
"""Runs the graph and returns fetch outputs.
Args:
spikes (numpy): mnist inputs [batch_size, 784]
targets (numpy): mnist targets [batch_size, 10]
keep_prop (float): dropout rate.
use_synthetic (bool): do we use synthetic inputs or query child.
do_train (bool): do we trigger training step.
"""
# Query child if exists.
cspikes = np.zeros((np.shape(spikes)[0], self._hparams.n_embedding))
if self._child and not use_synthetic:
cspikes = self._child.spike(spikes)
# Build feeds.
feeds = {
self._spikes: spikes, # Mnist 784 input.
self._cspikes: cspikes, # Child inputs.
self._targets: targets, # Mnist 1-hot Targets.
self._use_synthetic: use_synthetic, # Do we se synthetic inputs.
self._keep_rate: keep_prop, # Dropout.
}
# Build fetches.
fetches = {}
if do_train:
fetches['target_step'] = self._tstep
fetches['synthetic_step'] = self._syn_step # Synthetic step.
fetches['child_gradients'] = self._tdgrads
# We train the synthetic model when we query our child.
if not use_synthetic:
fetches['synthetic_loss'] = self._syn_loss # Distillation loss.
if do_metrics:
fetches['accuracy'] = self._accuracy, # Classification accuracy.
fetches['target_loss'] = self._target_loss, # Target accuracy.
fetches[
'inputs_pruning_score'] = self._inputs_pruning_score, # Salience of inputs.
fetches['inputs_weight_magnitude'] = self._inputs_weight_magnitude,
fetches[
'inputs_absolute_gradient'] = self._inputs_absolute_gradient,
fetches[
'downstream_pruning_score'] = self._downstream_pruning_score, # Salience of downstream.
fetches[
'downstream_weight_magnitude'] = self._downstream_weight_magnitude,
fetches[
'downstream_absolute_gradient'] = self._downstream_absolute_gradient,
fetches['input_integrated_gradients_score'] = self._input_ig
fetches[
'downstream_integrated_gradients_score'] = self._downstream_ig
# Run graph.
run_output = self._session.run(fetches, feeds)
# Pass the gradients to the child.
if self._child and do_train and not use_synthetic:
self._child.grade(spikes, run_output['child_gradients'])
# Return the batch accuracy.
return run_output
def spike(self, spikes):
""" External query on this node.
Spikes the local node returning its representational output given the
input.
Args:
spikes (numpy): mnist inputs [batch_size, 784]
"""
# Return using synthetic inputs as input to this component.
zeros = np.zeros((np.shape(spikes)[0], self._hparams.n_embedding))
feeds = {
self._spikes: spikes, # Mnist inputs.
self._cspikes: zeros, # Zeros from children (not used)
self._use_synthetic: True, # Use synthetic children.
self._keep_rate: 1.0, # No dropout.
}
# Return the embedding.
return self._session.run(self._embedding, feeds)
def grade(self, spikes, grads):
""" Grade the child node.
Computes and applies the gradients to the local node given the
passed signal.
Args:
spikes (numpy): mnist inputs [batch_size, 784]
grads (numpy): gradients [batch_size, n_embedding]
"""
zeros = np.zeros((np.shape(spikes)[0], self._hparams.n_embedding))
feeds = {
self._spikes: spikes, # Spikes from query.
self._egrads: grads, # Embedding gradients from parent.
self._cspikes: zeros, # Zeros from children.
self._use_synthetic: False, # Do not use Synthetic.
self._keep_rate: 1.0 # No Dropout.
}
# Run the embedding step.
self._session.run([self._estep], feeds)
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])
# Cspikes: inputs from previous component. Size is the same as the embeddings produced
# by this component.
self._cspikes = tf.compat.v1.placeholder(
tf.float32, [None, self._hparams.n_embedding])
# Egrads: Gradient for this component's embedding, passed by a parent.
self._egrads = tf.compat.v1.placeholder(
tf.float32, [None, self._hparams.n_embedding])
# Targets: Supervised signals used during training and testing.
self._targets = tf.compat.v1.placeholder(
tf.float32, [None, self._hparams.n_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.compat.v1.placeholder_with_default(1.0, shape=())
# 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.nn.relu(
tf.add(tf.matmul(drop_hidden_layer2, weights['w3']), biases['b3']))
# 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(
self._hparams.learning_rate)
# 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._cspikes],
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._cspikes
])[0][0]
self._tlgrads = optimizer.compute_gradients(
loss=self._target_loss, var_list=local_network_variables)
# 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)
# 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))
# Input information score.
self._inputs_grads = optimizer.compute_gradients(loss=self._target_loss,
var_list=self._spikes)
self._inputs_absolute_gradient = tf.reduce_sum(
tf.reduce_sum(tf.abs(self._inputs_grads[0][0])))
self._inputs_pruning_score = self._pruning_score(
self._spikes, self._inputs_grads[0][0])
self._inputs_weight_magnitude = tf.reduce_sum(
tf.reduce_sum(tf.slice(weights['w1'], [0, 0], [784, -1])))
# Downstream information score.
self._downstream_grads = optimizer.compute_gradients(
loss=self._target_loss, var_list=self._downstream)
self._downstream_absolute_gradient = tf.reduce_sum(
tf.reduce_sum(tf.abs(self._downstream_grads[0][0])))
self._downstream_pruning_score = self._pruning_score(
self._downstream, self._downstream_grads[0][0])
self._downstream_weight_magnitude = tf.reduce_sum(
tf.reduce_sum(tf.slice(weights['w1'], [784, 0], [-1, -1])))
# Integrated Gradients.
integrated_gradients = tf.abs(
self._integrated_gradients(self._logits, self._input_layer, 10))
total_ig = tf.reduce_sum(integrated_gradients)
downstream_ig = tf.reduce_sum(
tf.slice(integrated_gradients, [0, 784], [-1, -1]))
input_ig = tf.reduce_sum(
tf.slice(integrated_gradients, [0, 0], [-1, 784]))
self._downstream_ig = downstream_ig / total_ig
self._input_ig = input_ig / total_ig
def _integrated_gradients(self, target, input_tensor, steps):
baseline = tf.zeros_like(input_tensor)
scaled_inputs = [
baseline + (float(i) / steps) * (input_tensor - baseline)
for i in range(0, steps + 1)
]
grads = []
for next_inp in scaled_inputs:
grads.append(tf.gradients(ys=target, xs=input_tensor)[0])
trapazoidal_grads = []
for i in range(len(grads) - 1):
trapazoidal_grads.append((grads[i] + grads[i + 1]) / 2)
avg_trapazoidal = tf.add_n(trapazoidal_grads) / len(grads)
avg_grads = tf.reduce_mean(avg_trapazoidal, axis=0)
integrated_gradients = (input_tensor -
baseline) * avg_grads # shape: <inp.shape>
return integrated_gradients
def _pruning_score(self, value, gradient):
g = tf.tensordot(-value, gradient, axes=2)
gxgx = tf.multiply(gradient, gradient)
H = tf.tensordot(-value, gxgx, axes=2)
score = tf.reduce_sum(g + H)
return score