-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmasked_networks.py
301 lines (264 loc) · 14.6 KB
/
masked_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
# Copyright (c) 2019 Uber Technologies, Inc.
# Licensed under the Uber Non-Commercial License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at the root directory of this project.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from __future__ import division
from tf_plus import Conv2D, MaxPooling2D, Flatten, Dense, relu, softmax, Activation
from tf_plus import Layers, SequentialNetwork, l2reg
from tf_plus import learning_phase
# use tensorflow's version of keras, or else get version incompatibility errors
from tensorflow.python import keras as tfkeras
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.framework import ops
import tensorflow as tf
import numpy as np
#import tensorflow_probability as tfp
glorot_normal = tf.keras.initializers.glorot_normal()
'''
Methods to set up special network architectures with binary masks being the only things trained
'''
def build_fc_supermask(args):
kwargs = {}
if args.signed_constant:
kwargs['signed_constant'] = True
kwargs['const_multiplier'] = args.signed_constant_multiplier
if args.dynamic_scaling:
kwargs['dynamic_scaling'] = True
return SequentialNetwork([
Flatten(),
MaskedDense(300, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_1', **kwargs),
MaskedDense(100, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_2', **kwargs),
MaskedDense(10, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=None, kernel_regularizer=l2reg(args.l2),
name='fc_3', **kwargs)
])
def build_conv2_supermask(args):
kwargs = {}
if args.signed_constant:
kwargs['signed_constant'] = True
kwargs['const_multiplier'] = args.signed_constant_multiplier
if args.dynamic_scaling:
kwargs['dynamic_scaling'] = True
return SequentialNetwork([
MaskedConv2D(64, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_1', **kwargs),
Activation('relu'),
MaskedConv2D(64, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_2', **kwargs),
Activation('relu'),
MaxPooling2D((2, 2), (2, 2)),
Flatten(),
MaskedDense(256, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_1', **kwargs),
MaskedDense(256, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_2', **kwargs),
MaskedDense(10, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=None, kernel_regularizer=l2reg(args.l2),
name='fc_3', **kwargs)
])
def build_conv4_supermask(args):
kwargs = {}
if args.signed_constant:
kwargs['signed_constant'] = True
kwargs['const_multiplier'] = args.signed_constant_multiplier
if args.dynamic_scaling:
kwargs['dynamic_scaling'] = True
return SequentialNetwork([
MaskedConv2D(64, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_1', **kwargs),
Activation('relu'),
MaskedConv2D(64, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_2', **kwargs),
Activation('relu'),
MaxPooling2D((2, 2), (2, 2)),
MaskedConv2D(128, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_3', **kwargs),
Activation('relu'),
MaskedConv2D(128, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_4', **kwargs),
Activation('relu'),
MaxPooling2D((2, 2), (2, 2)),
Flatten(),
MaskedDense(256, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_1', **kwargs),
MaskedDense(256, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_2', **kwargs),
MaskedDense(10, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=None, kernel_regularizer=l2reg(args.l2),
name='fc_3', **kwargs)
])
def build_conv6_supermask(args):
kwargs = {}
if args.signed_constant:
kwargs['signed_constant'] = True
kwargs['const_multiplier'] = args.signed_constant_multiplier
if args.dynamic_scaling:
kwargs['dynamic_scaling'] = True
return SequentialNetwork([
MaskedConv2D(64, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_1', **kwargs),
Activation('relu'),
MaskedConv2D(64, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_2', **kwargs),
Activation('relu'),
MaxPooling2D((2, 2), (2, 2)),
MaskedConv2D(128, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_3', **kwargs),
Activation('relu'),
MaskedConv2D(128, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_4', **kwargs),
Activation('relu'),
MaxPooling2D((2, 2), (2, 2)),
MaskedConv2D(256, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_5', **kwargs),
Activation('relu'),
MaskedConv2D(256, 3, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, padding='same', kernel_regularizer=l2reg(args.l2),
name='conv2D_6', **kwargs),
Activation('relu'),
MaxPooling2D((2, 2), (2, 2)),
Flatten(),
MaskedDense(256, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_1', **kwargs),
MaskedDense(256, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=relu, kernel_regularizer=l2reg(args.l2),
name='fc_2', **kwargs),
MaskedDense(10, kernel_initializer=glorot_normal, sigmoid_bias=args.sigmoid_bias,
round_mask=args.round_mask, activation=None, kernel_regularizer=l2reg(args.l2),
name='fc_3', **kwargs)
])
# used in any masked layer's call()
def get_effective_mask(self):
if self.round_mask:
# during train, clamp a random 50% to their rounded values, and backprop the other 50% directly
# during test, clamp all of them to their rounded values
which_to_clamp = tf.cond(learning_phase(),
lambda: gen_math_ops.round(tf.random.uniform(self.kernel_mask.shape, minval=0, maxval=1)),
lambda: tf.ones(self.kernel_mask.shape))
binary_mask = gen_math_ops.round(tf.nn.sigmoid(self.kernel_mask))
else:
# during train, clamp all of them to 0's and 1's sampled by bernoulli and backprop the probabilities
# during test, clamp all of them to their rounded values
# actually, sample them too
which_to_clamp = tf.ones(self.kernel_mask.shape)
binary_mask = tf.cond(learning_phase(),
lambda: tf.cast(tf.distributions.Bernoulli(probs=tf.nn.sigmoid(self.kernel_mask)).sample(), dtype=tf.float32)
+ tf.nn.sigmoid(self.kernel_mask)
- tf.stop_gradient(tf.nn.sigmoid(self.kernel_mask)),
lambda: tf.cast(tf.distributions.Bernoulli(probs=tf.nn.sigmoid(self.kernel_mask)).sample(), dtype=tf.float32))
return which_to_clamp * binary_mask + (1 - which_to_clamp) * tf.nn.sigmoid(self.kernel_mask)
# used to make a signed constant kernel
def make_signed_consts(kernel, multiplier=1.0):
'''Take a kernel tensor, change each value to a constant while respecting the original sign'''
mean, var = tf.nn.moments(kernel, axes = [x for x in range(len(kernel.shape))])
val = tf.sqrt(var)
# val = tf.math.reduce_std(kernel)
return tf.sign(kernel) * val * multiplier
class MaskedDense(Dense):
# untrainable normal Dense layer
# trainable mask, that is sigmoided (maybe squished) and then multiplied to Dense
def __init__(self, units, sigmoid_bias=0, round_mask=False, signed_constant=False, const_multiplier=1, dynamic_scaling=False, *args, **kwargs):
super(MaskedDense, self).__init__(units, *args, **kwargs)
self._uses_learning_phase = True
self.sigmoid_bias = sigmoid_bias # bias to add before rounding to adjust prune percentage
self.round_mask = round_mask # round instead of bernoulli sampling
self.signed_constant = signed_constant
self.const_multiplier = const_multiplier
self.dynamic_scaling = dynamic_scaling
def build(self, input_shape):
super(MaskedDense, self).build(input_shape)
mask_init = tfkeras.initializers.Constant(self.sigmoid_bias)
self._trainable_weights.remove(self.kernel)
self._non_trainable_weights.append(self.kernel)
if self.use_bias:
self._trainable_weights.remove(self.bias)
self._non_trainable_weights.append(self.bias)
self.kernel_mask = tf.get_variable('mask',
shape=self.kernel.shape,
dtype=self.dtype,
initializer=mask_init,
trainable=True)
self._trainable_weights.append(self.kernel_mask)
# same as original call() except round some sample to {0, 1} based on a sample
def call(self, inputs):
if self.signed_constant:
self.kernel = make_signed_consts(self.kernel, self.const_multiplier)
effective_mask = get_effective_mask(self)
effective_kernel = self.kernel * effective_mask
if self.dynamic_scaling:
self.ones_in_mask = tf.reduce_sum(effective_mask)
self.multiplier = tf.div(tf.to_float(tf.size(effective_mask)), self.ones_in_mask)
effective_kernel = self.multiplier * effective_kernel
# original code from https://github.com/tensorflow/tensorflow/blob/r1.13/tensorflow/python/keras/layers/core.py:
inputs = ops.convert_to_tensor(inputs)
outputs = gen_math_ops.mat_mul(inputs, effective_kernel)
if self.use_bias:
outputs = tf.nn.bias_add(outputs, self.bias)
if self.activation is not None:
return self.activation(outputs)
return outputs
class MaskedConv2D(Conv2D):
# untrainable original conv2d layer, trainable max
def __init__(self, filters, kernel_size, sigmoid_bias=0, round_mask=False, signed_constant=False, const_multiplier=1, dynamic_scaling=False, *args, **kwargs):
super(MaskedConv2D, self).__init__(filters, kernel_size, *args, **kwargs)
self._uses_learning_phase = True
self.sigmoid_bias = sigmoid_bias # bias to add before rounding to adjust prune percentage
self.round_mask = round_mask # round instead of bernoulli sampling
self.signed_constant = signed_constant
self.const_multiplier = const_multiplier
self.dynamic_scaling = dynamic_scaling
def build(self, input_shape):
super(MaskedConv2D, self).build(input_shape)
mask_init = tfkeras.initializers.Constant(self.sigmoid_bias)
self._trainable_weights.remove(self.kernel)
self._non_trainable_weights.append(self.kernel)
if self.use_bias:
self._trainable_weights.remove(self.bias)
self._non_trainable_weights.append(self.bias)
self.kernel_mask = tf.get_variable('mask',
shape=self.kernel.shape,
dtype=self.dtype,
initializer=mask_init,
trainable=True)
self._trainable_weights.append(self.kernel_mask)
# same as original call() except apply binary mask
def call(self, inputs):
if self.signed_constant:
self.kernel = make_signed_consts(self.kernel, self.const_multiplier)
effective_mask = get_effective_mask(self)
effective_kernel = self.kernel * effective_mask
if self.dynamic_scaling:
self.ones_in_mask = tf.reduce_sum(effective_mask)
self.multiplier = tf.div(tf.to_float(tf.size(effective_mask)), self.ones_in_mask)
effective_kernel = self.multiplier * effective_kernel
# original code from https://github.com/keras-team/keras/blob/master/keras/layers/convolutional.py:
outputs = self._convolution_op(inputs, effective_kernel)
if self.use_bias:
if self.data_format == 'channels_first':
outputs = tf.nn.bias_add(outputs, self.bias, data_format='NCHW')
if self.activation is not None:
return self.activation(outputs)
return outputs