-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_networks.py
820 lines (659 loc) · 26.2 KB
/
dynamic_networks.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#################################################
# Implements a dynamical dense layer that allows
# both adding and removing both input and output
# features and a simple update step for both.
#
# Inspired by "Lifelong Learning with Dynamically
# Expandable Networks", ICLR, 2017 (arXiv:1708.01547)
#################################################
import tensorflow as tf
import numpy as np
class DynamicMatrix:
"""The dynamic matrix that allows adding and removing features"""
def __init__(self, shape, std=0.1):
self.gradient_step = tf.Variable(0.0, trainable=False)
if shape is not None:
self.mat = tf.Variable(tf.random.normal(shape, stddev=std), trainable=True)
self.mom = tf.Variable(np.zeros(shape).astype("float32"), trainable=False)
self.mom2 = tf.Variable(np.zeros(shape).astype("float32"), trainable=False)
self.dim = len(shape)
@classmethod
def from_state(cls, state):
obj = cls(None)
obj.mat = state[0]
obj.mom = state[1]
obj.mom2 = state[2]
return obj
def expand_out(self, n, std):
"""Add a random output feature"""
new_row = tf.random.normal(self.mat.shape[:-1] + (n,), stddev=std)
self.mat = tf.Variable(
tf.concat([self.mat, new_row], self.dim - 1), trainable=True
)
# Set momenta for the new row to zero
mom_row = tf.Variable(np.zeros((self.mom.shape[:-1] + (n,))).astype("float32"))
self.mom = tf.Variable(
tf.concat([self.mom, mom_row], self.dim - 1), trainable=False
)
mom2_row = tf.Variable(
np.zeros((self.mom2.shape[:-1] + (n,))).astype("float32")
)
self.mom2 = tf.Variable(
tf.concat([self.mom2, mom2_row], self.dim - 1), trainable=False
)
def contract_out(self, n, index):
"""Remove a random output feature"""
if self.shape[-1] > 1:
start = [0 for x in self.shape]
size = list(self.shape)
size[-1] = n * index
new_mat = tf.slice(self.mat, start, size)
new_mom = tf.slice(self.mom, start, size)
new_mom2 = tf.slice(self.mom2, start, size)
start[-1] = n * (index + 1)
size[-1] = self.shape[-1] - n * (index + 1)
new_mat = tf.concat(
[new_mat, tf.slice(self.mat, start, size)], self.dim - 1
)
new_mom = tf.concat(
[new_mom, tf.slice(self.mom, start, size)], self.dim - 1
)
new_mom2 = tf.concat(
[new_mom2, tf.slice(self.mom2, start, size)], self.dim - 1
)
self.mat = tf.Variable(new_mat, trainable=True)
self.mom = tf.Variable(new_mom, trainable=False)
self.mom2 = tf.Variable(new_mom2, trainable=False)
def expand_in(self, n, std):
"""Add a random input feature"""
new_column = tf.random.normal(
self.mat.shape[:-2] + (n, self.mat.shape[-1]), stddev=std
)
self.mat = tf.Variable(
tf.concat([self.mat, new_column], self.dim - 2), trainable=True
)
# Set momenta for the new row to zero
mom_column = tf.Variable(
np.zeros(self.mom.shape[:-2] + (n, self.mom.shape[-1])).astype("float32")
)
self.mom = tf.Variable(
tf.concat([self.mom, mom_column], self.dim - 2), trainable=False
)
mom2_column = tf.Variable(
np.zeros(self.mom2.shape[:-2] + (n, self.mom2.shape[-1])).astype("float32")
)
self.mom2 = tf.Variable(
tf.concat([self.mom2, mom2_column], self.dim - 2), trainable=False
)
def contract_in(self, n, index):
"""Remove a random input feature"""
if self.mat.shape[-2] > 1:
start = [0 for x in self.shape]
size = list(self.shape)
size[-2] = n * index
new_mat = tf.slice(self.mat, start, size)
new_mom = tf.slice(self.mom, start, size)
new_mom2 = tf.slice(self.mom2, start, size)
start[-2] = n * (index + 1)
size[-2] = self.shape[-2] - n * (index + 1)
new_mat = tf.concat(
[new_mat, tf.slice(self.mat, start, size)], self.dim - 2
)
new_mom = tf.concat(
[new_mom, tf.slice(self.mom, start, size)], self.dim - 2
)
new_mom2 = tf.concat(
[new_mom2, tf.slice(self.mom2, start, size)], self.dim - 2
)
self.mat = tf.Variable(new_mat, trainable=True)
self.mom = tf.Variable(new_mom, trainable=False)
self.mom2 = tf.Variable(new_mom2, trainable=False)
def colsum(self, n, index):
"""Find the L1 sum of a given column
"""
start = [0 for x in self.shape]
start[-2] = n * index
size = list(self.shape)
size[-2] = n
abs = tf.slice(self.mat, start, size)
abs = tf.math.abs(abs)
value = tf.math.reduce_sum(abs, keepdims=False)
return value
def rowsum(self, n, index):
"""Find the L1 sum of a given row
"""
start = [0 for x in self.shape]
start[-2] = n * index
size = list(self.shape)
size[-2] = n
abs = tf.slice(self.mat, start, size)
abs = tf.math.abs(abs)
value = tf.math.reduce_sum(abs, keepdims=False)
return value
def get_state(self):
return (self.mat, self.mom, self.mom2)
def set_state(self, state):
assert not isinstance(state[0], tf.Tensor)
assert not isinstance(state[1], tf.Tensor)
assert not isinstance(state[2], tf.Tensor)
self.mat = state[0]
self.mom = state[1]
self.mom2 = state[2]
def apply_adam(self, gradient, alpha=0.001, beta1=0.9, beta2=0.999, epsilon=1e-8):
"""The Adam gradient descent method"""
t = self.gradient_step.assign_add(1.0)
mom = self.mom.assign(beta1 * self.mom + (1 - beta1) * gradient)
mom2 = self.mom2.assign(beta2 * self.mom2 + (1 - beta2) * gradient * gradient)
mom_hat = mom / (1 - tf.pow(beta1, t))
mom2_hat = mom2 / (1 - tf.pow(beta2, t))
self.mat.assign_add(-alpha * mom_hat / (tf.sqrt(mom2_hat) + epsilon))
@property
def shape(self):
return self.mat.get_shape().as_list()
class DynamicDenseLayer:
"""A single dense layer with dynamic input and output size"""
def __init__(
self, input_size, output_size,
new_weight_std=0.1,
miminum_input_size=None,
miminum_output_size=None
):
"""Create the layer with a given initial configuration"""
if input_size is not None:
self.w = DynamicMatrix((input_size, output_size), new_weight_std)
self.b = DynamicMatrix((1, output_size), new_weight_std)
self.dynamic = True
self.input_size = input_size
self.output_size = output_size
self.new_weight_std = new_weight_std
if miminum_input_size is None:
self.miminum_input_size = input_size
else:
self.miminum_input_size = miminum_input_size
if miminum_output_size is None:
self.miminum_output_size = output_size
else:
self.miminum_output_size = miminum_output_size
@classmethod
def from_state(cls, state, new_weight_std=0.01):
"""Initialize from state tuple (or list)"""
obj = cls(None, None)
obj.w = DynamicMatrix.from_state(state[0])
obj.b = DynamicMatrix.from_state(state[1])
obj.input_size = state[2]
obj.output_size = state[3]
obj.new_weight_std = new_weight_std
return obj
def expand_out(self):
"""Add a random output feature"""
self.w.expand_out(1, self.new_weight_std)
self.b.expand_out(1, self.new_weight_std)
self.output_size = self.output_size + 1
def contract_out(self, index):
"""Remove a random output feature"""
if self.output_size > self.miminum_output_size:
self.w.contract_out(1, index)
self.b.contract_out(1, index)
self.output_size = self.output_size - 1
def expand_in(self):
"""Add a random input feature"""
self.w.expand_in(1, self.new_weight_std)
self.input_size = self.input_size + 1
def contract_in(self, index):
"""Remove a random input feature"""
if self.input_size > self.miminum_input_size:
self.w.contract_in(1, index)
self.input_size = self.input_size - 1
def prune(self, index, treshhold=0.001):
"""Remove any features with combined weight values below
the threshhold
"""
if self.output_size > self.miminum_input_size:
if self.w.rowsum(1, index) < treshhold:
self.contract_in(index)
return True
return False
@property
def trainable_variables(self):
"""Returns a list of trainable variables"""
return [self.w.mat, self.b.mat]
def get_state(self):
"""Returns the current state of the layer"""
return (
self.w.get_state(),
self.b.get_state(),
self.input_size,
self.output_size,
)
# the given state
def set_state(self, state):
"""Overwrite the current state of the layer with
with the given state
"""
assert not isinstance(state[0], tf.Tensor)
assert not isinstance(state[1], tf.Tensor)
self.w.set_state(state[0])
self.b.set_state(state[1])
self.input_size = state[2]
self.output_size = state[3]
def weight_count(self):
"""Return the number of weights in the layer"""
return self.input_size * self.output_size + self.output_size
def summary_string(self):
return "({}, {})".format(self.input_size, self.output_size)
def apply_adam(self, gradients, alpha=0.001, beta1=0.9, beta2=0.999, epsilon=1e-8):
self.w.apply_adam(gradients[0], alpha, beta1, beta2, epsilon)
self.b.apply_adam(gradients[1], alpha, beta1, beta2, epsilon)
def __call__(self, inputs):
"""Apply the layer"""
assert self.w.shape == [self.input_size, self.output_size]
assert self.b.shape == [1, self.output_size]
return tf.matmul(inputs, self.w.mat) + self.b.mat
class DynamicConv2DLayer:
"""A convolution layer with dynamic filter size"""
def __init__(
self, width, input_size, output_size,
new_weight_std=0.1,
miminum_input_size=None,
miminum_output_size=None
):
"""Create the layer with a given initial configuration"""
if input_size is not None:
self.w = DynamicMatrix((width, width, input_size, output_size), new_weight_std)
self.dynamic = True
self.width = width
self.input_size = input_size
self.output_size = output_size
self.new_weight_std = new_weight_std
if miminum_input_size is None:
self.miminum_input_size = input_size
else:
self.miminum_input_size = miminum_input_size
if miminum_output_size is None:
self.miminum_output_size = output_size
else:
self.miminum_output_size = miminum_output_size
@classmethod
def from_state(cls, state, new_weight_std=0.01):
"""Initialize from state tuple (or list)"""
obj = cls(None, None)
obj.w = DynamicMatrix.from_state(state[0])
obj.width = state[1]
obj.input_size = state[2]
obj.output_size = state[3]
obj.new_weight_std = new_weight_std
return obj
def expand_out(self):
"""Add a random output feature"""
self.w.expand_out(1, self.new_weight_std)
self.output_size = self.output_size + 1
def contract_out(self, n):
"""Remove a random output feature"""
if self.output_size > self.miminum_output_size:
self.w.contract_out(1, n)
self.output_size = self.output_size - 1
def contract_in(self, n):
"""Remove a random input feature"""
if self.input_size > self.miminum_input_size:
self.w.contract_in(1, n)
self.input_size = self.input_size - 1
def prune(self, index, treshhold=0.001):
"""Remove any features with combined weight values below
the threshhold
"""
if self.output_size > self.miminum_input_size:
if self.w.rowsum(1, index) < treshhold:
self.contract_in(index)
return True
return False
def expand_in(self):
"""Add a random input feature"""
self.w.expand_in(1, self.new_weight_std)
self.input_size = self.input_size + 1
@property
def trainable_variables(self):
"""Returns a list of trainable variables"""
return [self.w.mat]
def get_state(self):
"""Returns the current state of the layer"""
return (self.w.get_state(), self.width, self.input_size, self.output_size)
# the given state
def set_state(self, state):
"""Overwrite the current state of the layer with
the given state
"""
assert not isinstance(state[0], tf.Tensor)
self.w.set_state(state[0])
self.width = state[1]
self.input_size = state[2]
self.output_size = state[3]
def weight_count(self):
"""Return the number of weights in the layer"""
return self.width * self.width * self.input_size * self.output_size
def summary_string(self):
return "({}, {}, {}, {})".format(
self.width, self.width, self.input_size, self.output_size
)
def apply_adam(self, gradients, alpha=0.001, beta1=0.9, beta2=0.999, epsilon=1e-8):
self.w.apply_adam(gradients[0], alpha, beta1, beta2, epsilon)
def __call__(self, inputs):
"""Apply the layer"""
assert self.w.shape == [
self.width,
self.width,
self.input_size,
self.output_size,
]
return tf.nn.conv2d(inputs, self.w.mat, 2, "SAME")
class DynamicConv2DToDenseLayer:
"""Flattens the output of a conv2d layer and allows
adding and removing neurons correctly in between
"""
def __init__(
self, pixels, features, output_size,
new_weight_std=0.1,
miminum_features=None,
miminum_output_size=None
):
"""Create the layer with a given initial configuration"""
if pixels is not None:
self.w = DynamicMatrix((pixels * features, output_size), new_weight_std)
self.b = DynamicMatrix((1, output_size), new_weight_std)
self.dynamic = True
self.pixels = pixels
self.features = features
self.output_size = output_size
self.new_weight_std = new_weight_std
if miminum_features is None:
self.miminum_features = features
else:
self.miminum_features = miminum_features
if miminum_output_size is None:
self.miminum_output_size = output_size
else:
self.miminum_output_size = miminum_output_size
@classmethod
def from_state(cls, state, new_weight_std=0.01):
"""Initialize from state tuple (or list)"""
obj = cls(None, None)
obj.w = DynamicMatrix.from_state(state[0])
obj.b = DynamicMatrix.from_state(state[1])
obj.features = state[2]
obj.output_size = state[3]
obj.new_weight_std = new_weight_std
return obj
def expand_out(self):
"""Add a random output feature"""
self.w.expand_out(1, self.new_weight_std)
self.b.expand_out(1, self.new_weight_std)
self.output_size = self.output_size + 1
def contract_out(self, n):
"""Remove a random output feature"""
if self.output_size > self.miminum_output_size:
self.w.contract_out(1, n)
self.b.contract_out(1, n)
self.output_size = self.output_size - 1
def expand_in(self):
"""Add a random input feature"""
self.w.expand_in(self.pixels, self.new_weight_std)
self.features = self.features + 1
def contract_in(self, n):
"""Remove a random input feature"""
if self.features > self.miminum_features:
self.w.contract_in(self.pixels, n)
self.features = self.features - 1
def prune(self, index, treshhold=0.001):
"""Remove any features with combined weight values below
the threshhold
"""
if self.features > self.miminum_features:
if self.w.rowsum(self.pixels, index) < treshhold:
self.contract_in(self.pixels, index)
return True
return False
@property
def trainable_variables(self):
"""Returns a list of trainable variables"""
return [self.w.mat, self.b.mat]
def get_state(self):
"""Returns the current state of the layer"""
return (
self.w.get_state(),
self.b.get_state(),
self.pixels,
self.features,
self.output_size,
)
def set_state(self, state):
"""Overwrite the current state of the layer with the given state"""
assert not isinstance(state[0], tf.Tensor)
assert not isinstance(state[1], tf.Tensor)
self.w.set_state(state[0])
self.b.set_state(state[1])
self.pixels = state[2]
self.features = state[3]
self.output_size = state[4]
def weight_count(self):
"""Return the number of weights in the layer"""
return self.pixels * self.features * self.output_size + self.output_size
def summary_string(self):
return "({}, {}, {})".format(self.pixels, self.features, self.output_size)
def apply_adam(self, gradients, alpha=0.001, beta1=0.9, beta2=0.999, epsilon=1e-8):
self.w.apply_adam(gradients[0], alpha, beta1, beta2, epsilon)
self.b.apply_adam(gradients[1], alpha, beta1, beta2, epsilon)
def __call__(self, inputs):
"""Apply the layer"""
assert self.w.shape == [self.pixels * self.features, self.output_size]
assert self.b.shape == [1, self.output_size]
# Move pixels to the last columns, so that it is easier to add and remove
x = tf.transpose(inputs, perm=[0, 3, 1, 2])
# Now flatten
x = tf.reshape(x, [x.shape[0], -1])
x = tf.matmul(x, self.w.mat) + self.b.mat
return x
class DynamicModel:
"""A model formed of a number of dynamical dense layers"""
def __init__(self, layers, new_weight_std=0.1, activation=tf.nn.relu):
"""Create the initial model configuration"""
# A list of layersr in this model
self.layers = layers
# Variables related to fully connected part
self.new_weight_std = new_weight_std
self.input_size = self.layers[0].input_size
self.output_size = self.layers[-1].output_size
self.activation = activation
def weight_count(self):
"""Returns the number of weights currently in the model"""
count = 0
for layer in self.layers:
if layer.dynamic:
count += layer.weight_count()
return count
def summary(self):
"""Print a summary of the layers in this model"""
num_weights = 0
for i, l in enumerate(self.layers):
if l.dynamic:
l_weights = l.weight_count()
num_weights += l_weights
print(
"Layer {}: {}, number of weights {}".format(
i, l.summary_string(), l_weights
)
)
print("Total: {} weights".format(num_weights))
def expand(self):
"""Add a feature"""
# Pick a layer
nl = (int)((len(self.layers) - 1) * np.random.rand())
l1 = self.layers[nl]
l2 = self.layers[nl + 1]
if not l1.dynamic or not l2.dynamic:
return
# Expand the number of outputs in the layer
# and the number of inputs in the next one
l1.expand_out()
l2.expand_in()
def contract(self):
"""Remove a random feature"""
# Pick a layer
nl = (int)((len(self.layers) - 1) * np.random.rand())
l1 = self.layers[nl]
l2 = self.layers[nl + 1]
if not l1.dynamic or not l2.dynamic:
return
# Choose a random feature
n = (int)(l1.output_size * np.random.rand())
# remove it from both the layer and the next one
l1.contract_out(n)
l2.contract_in(n)
def prune(self, treshhold=0.01):
"""Remove any features with combined weight values below
the threshhold
"""
for nl in range(len(self.layers)-1):
l1 = self.layers[nl]
l2 = self.layers[nl + 1]
n = 0
while n < l1.output_size:
if l2.prune(n, treshhold):
l1.contract_out(n)
else:
n += 1
def stochastic_update(
self, data, update_function, loss_function, weight_penalty
):
"""Stochastic update: change the network and accept the
change if it decreases the loss function
"""
# Get the current loss, including the weight penalty
initial_loss = loss_function(data)
initial_loss += weight_penalty * self.weight_count()
# Make note of the current state
initial_state = self.get_state()
# Update the network
update_function()
# Calculate the loss in the new network
new_loss = loss_function(data)
new_loss += weight_penalty * self.weight_count()
# and the change in the loss
dloss = new_loss - initial_loss
# If the loss increases, return to the original state
if dloss > 0:
self.set_state(initial_state)
accepted = False
else:
accepted = True
return accepted
def stochastic_add_feature(
self, data, loss_function, weight_penalty=0,
layer_change_rate=0.1
):
"""Stochastic update: add a feature if it decreases
the loss function
"""
accepted = self.stochastic_update(
data, self.expand, loss_function, weight_penalty
)
return accepted
def update_features(
self, data, loss_function, weight_penalty=0,
layer_change_rate=0.1
):
"""Stochastic update: add or remove a feature if it
decreases the loss function
"""
# Randomly choose whether to add or remove
if np.random.rand() > 0.5:
update_function = self.expand
else:
update_function = self.contract
accepted = self.stochastic_update(
data, update_function, loss_function, weight_penalty
)
return accepted
def trainable_variables(self):
"""Returns a list of trainable variables"""
return [var for layer in self.layers for var in layer.trainable_variables]
def get_state(self):
"""Returns the current state of the model"""
state = []
for layer in self.layers:
if layer.dynamic:
state.append(layer.get_state())
return state
def set_state(self, state):
"""Overwrite the current state"""
i = 0
for layer in self.layers:
if layer.dynamic:
layer.set_state(state[i])
i = i + 1
def apply_adam(self, gradients, alpha=0.001, beta1=0.9, beta2=0.999, epsilon=1e-8):
"""Update the weights using the ADAM update method"""
var_index = 0
for layer in self.layers:
n_vars = len(layer.trainable_variables)
layer.apply_adam(
gradients[var_index : var_index + n_vars], alpha, beta1, beta2, epsilon
)
var_index += n_vars
def __call__(self, inputs):
"""Apply the model"""
x = inputs
for layer in self.layers[:-1]:
x = layer(x)
x = self.activation(x)
x = self.layers[-1](x)
return x
# -------------------------------
# Add or remove dense layers
# -------------------------------
def add_layer(self):
"""Add a dense layer.
The new layer starts close to an identity operation.
"""
# Pick a layer
nl = (int)((len(self.layers) - 1) * np.random.rand())
l1 = self.layers[nl]
# Build an intermediate layer. Start close to one
stdiv = self.new_weight_std / (l1.output_size)
new_w = tf.Variable(
tf.eye(l1.output_size)
+ tf.random.normal((l1.output_size, l1.output_size), stddev=stdiv),
trainable=True,
)
new_b = tf.Variable(
tf.random.normal((l1.output_size,), stddev=stdiv), trainable=True
)
new_layer = DynamicDenseLayer.from_state(
(new_w, new_b, l1.output_size, l1.output_size)
)
self.layers.insert(nl + 1, new_layer)
def remove_layer(self):
"""Remove a layer.
Remove the activation function between two layers and merge
the now linear operations.
"""
if len(self.layers) > 2:
# Pick a layer
nl = (int)((len(self.layers) - 1) * np.random.rand())
# Just drop the activation between the layer and the next,
# reducing them to a single linear operation
l1 = self.layers[nl]
l2 = self.layers[nl + 1]
# Pull the states of the two layers and construct new variables
st1 = l1.get_state()
st2 = l2.get_state()
new_w = tf.Variable(tf.matmul(st1[0], st2[0]), trainable=True)
new_b = tf.Variable(
tf.matmul(tf.expand_dims(st1[1], 0), st2[0])[0, :] + st2[1],
trainable=True,
)
assert new_w.shape == (l1.input_size, l2.output_size)
# Build the new layer
state = [new_w, new_b, l1.input_size, l2.output_size]
new_layer = DynamicDenseLayer.from_state(state)
del self.layers[nl]
del self.layers[nl]
self.layers.insert(nl, new_layer)