forked from PhilippeNguyen/nested_dropout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
special.py
452 lines (354 loc) · 16.6 KB
/
special.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
import tensorflow as tf
import tensorflow_probability as tfp
import tensorflow.keras as keras
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Layer,Lambda,Multiply,Dropout,Dense,Activation
from tensorflow.keras.layers import Add
from tensorflow.keras.callbacks import Callback,ModelCheckpoint
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras import initializers
import numpy as np
import warnings
from tensorflow.python.platform import tf_logging as logging
class TrainSequence(keras.utils.Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return int(np.ceil(len(self.x) / float(self.batch_size)))
def __getitem__(self, idx):
if self.y is None:
batch_y = None
else:
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
return batch_x,batch_y
def tanh_crossentropy(y_true,y_pred):
bin_cross = binary_crossentropy((y_true+1)/2,(y_pred+1)/2)
return K.mean(K.sum(bin_cross,axis=(1,2)))
def kl_from_uniform_bernoulli(latent_tensor,geo_index):
#latent_tensor should have shape (batche_size,latent_size)
base_dist = tfp.distributions.Bernoulli(logits=0.)
latent_dist = tfp.distributions.Bernoulli(logits=latent_tensor[:,:geo_index])
kl_dist = base_dist.kl_divergence(latent_dist)
return kl_dist
class UpdateExtraParams(Callback):
def __init__(self,
geom_drop_layer,
stop_grad_layer,
bern_layer,
monitor='val_loss',
verbose=0,
mode='auto',
update_count=3,
):
super(UpdateExtraParams, self).__init__()
self.geom_drop_layer = geom_drop_layer
self.stop_grad_layer = stop_grad_layer
self.bern_layer = bern_layer
self.monitor = monitor
self.verbose = verbose
self.count = 0
self.update_count = update_count
if mode not in ['auto', 'min', 'max']:
warnings.warn('EarlyStopping mode %s is unknown, '
'fallback to auto mode.' % mode,
RuntimeWarning)
mode = 'auto'
if mode == 'min':
self.monitor_op = np.less
elif mode == 'max':
self.monitor_op = np.greater
else:
if 'acc' in self.monitor:
self.monitor_op = np.greater
else:
self.monitor_op = np.less
def on_train_begin(self, logs=None):
self.best = np.Inf if self.monitor_op == np.less else -np.Inf
def on_epoch_end(self, epoch, logs=None):
# current = self.get_monitor_value(logs)
# if current is None:
# return
#
# if self.monitor_op(current, self.best):
# self.best = current
# else:
# geom_val = self.geom_drop_layer.get_geom_val()+1
# self.geom_drop_layer.set_geom_val(geom_val)
#
# stop_idx = self.stop_grad_layer.get_stop_idx()+1
# self.stop_grad_layer.set_stop_idx(stop_idx)
self.count+=1
if self.count >self.update_count:
self.bern_layer.set_temp(self.bern_layer.init_temp)
geom_val = self.geom_drop_layer.get_geom_val()+1
self.geom_drop_layer.set_geom_val(geom_val)
stop_idx = self.stop_grad_layer.get_stop_idx()+1
self.stop_grad_layer.set_stop_idx(stop_idx)
self.count = 0
else:
temp = self.bern_layer.get_temp()*0.7
self.bern_layer.set_temp(temp)
def get_monitor_value(self, logs):
monitor_value = logs.get(self.monitor)
if monitor_value is None:
warnings.warn(
'Early stopping conditioned on metric `%s` '
'which is not available. Available metrics are: %s' %
(self.monitor, ','.join(list(logs.keys()))), RuntimeWarning
)
return monitor_value
#
class FixedModelCheckpoint(ModelCheckpoint):
def __init__(self, filepath, save_model,**kwargs):
super(FixedModelCheckpoint, self).__init__(filepath,**kwargs)
self.save_model = save_model
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
self.epochs_since_last_save += 1
if self.epochs_since_last_save >= self.period:
self.epochs_since_last_save = 0
filepath = self.filepath.format(epoch=epoch + 1, **logs)
if self.save_best_only:
current = logs.get(self.monitor)
if current is None:
warnings.warn('Can save best model only with %s available, '
'skipping.' % (self.monitor), RuntimeWarning)
else:
if self.monitor_op(current, self.best):
if self.verbose > 0:
print('\nEpoch %05d: %s improved from %0.5f to %0.5f,'
' saving model to %s'
% (epoch + 1, self.monitor, self.best,
current, filepath))
self.best = current
if self.save_weights_only:
self.save_model.set_weights(self.model.get_weights())
self.save_model.save_weights(filepath, overwrite=True)
else:
self.save_model.set_weights(self.model.get_weights())
self.save_model.save(filepath, overwrite=True)
else:
if self.verbose > 0:
print('\nEpoch %05d: %s did not improve from %0.5f' %
(epoch + 1, self.monitor, self.best))
else:
if self.verbose > 0:
print('\nEpoch %05d: saving model to %s' % (epoch + 1, filepath))
if self.save_weights_only:
self.save_model.set_weights(self.model.get_weights())
self.save_model.save_weights(filepath, overwrite=True)
else:
self.save_model.set_weights(self.model.get_weights())
self.save_model.save(filepath, overwrite=True)
class BernoulliSampling(Layer):
def __init__(self,init_temp,from_logits=False,**kwargs):
super(BernoulliSampling, self).__init__(**kwargs)
self.supports_masking = True
self.init_temp = init_temp
self.init = initializers.Constant(init_temp)
self.from_logits = from_logits
def set_temp(self,temperature):
print('setting temperature: ', temperature)
self.set_weights([np.asarray(temperature)])
def get_temp(self):
return self.get_weights()[0]
def build(self,input_shape):
self.temperature = self.add_weight(name="temperature",
shape=(),
dtype=K.floatx(),
initializer=self.init,
trainable=False)
def call(self, inputs, training=None):
_,latent_size = inputs.shape.as_list()
def sampled():
if self.from_logits:
dist = tfp.distributions.RelaxedBernoulli(logits=inputs,
temperature=self.temperature)
else:
dist = tfp.distributions.RelaxedBernoulli(probs=inputs,
temperature=self.temperature)
sample = dist.sample(sample_shape=())
return sample
def quantized():
differentiable_round = tf.maximum(inputs-0.499,0)
differentiable_round = differentiable_round * 10000
differentiable_round = tf.minimum(differentiable_round, 1)
return differentiable_round
return K.in_train_phase(sampled, quantized, training=training)
def get_config(self):
config = {'init_temp':self.init_temp,'from_logits':self.from_logits}
base_config = super(BernoulliSampling, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
class GeometricDropout(Layer):
def __init__(self, rate, geom_val=0, **kwargs):
super(GeometricDropout, self).__init__(**kwargs)
self.supports_masking = True
self.rate = rate
self.geom_val = geom_val
self.latent_size = None
self.indices = None
self.valid_mask = None
def set_geom_val(self,geom_val):
self.geom_val = geom_val
self.set_geom_indices()
def get_geom_val(self):
return self.geom_val
def set_geom_indices(self):
print('updating geom dropout indices, now at :',self.geom_val)
_indices = np.expand_dims(np.arange(0,self.latent_size)-self.geom_val,axis=0)
valid_mask = np.zeros(((1,self.latent_size,)))
valid_mask[...,:self.geom_val+1] = 1
self.set_weights([_indices,valid_mask])
def build(self,input_shape):
self.latent_size = input_shape.as_list()[1]
self.indices = self.add_weight(name="geom_indices",
shape=(1,self.latent_size,),
dtype=K.floatx(),
trainable=False)
self.valid_mask = self.add_weight(
name="valid_mask",
shape=(1,self.latent_size,),
dtype=K.floatx(),
trainable=False)
self.set_geom_indices()
def call(self, inputs, training=None):
if 0 < self.rate < 1:
def noised():
indices = tf.broadcast_to(self.indices,K.shape(inputs))
geom = tfp.distributions.Geometric(probs=[self.rate])
geom_sample = geom.sample(sample_shape=(K.shape(inputs)[0]))
drop = tf.cast(indices <= geom_sample,tf.float32)
return inputs * drop
def valid_masked_only():
valid_mask = tf.broadcast_to(self.valid_mask,K.shape(inputs))
return inputs*valid_mask
return K.in_train_phase(noised, valid_masked_only, training=training)
return inputs
def get_config(self):
config = {'rate': self.rate,'geom_val':self.geom_val}
base_config = super(GeometricDropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
class StopGradientMask(Add):
def __init__(self, stop_idx=0,**kwargs):
super(StopGradientMask, self).__init__(**kwargs)
self.stop_idx = stop_idx
self.stop_gradient_mask = None
self.gradient_mask = None
def set_stop_idx(self,stop_idx):
self.stop_idx = stop_idx
self.set_masks()
def get_stop_idx(self):
return self.stop_idx
def set_masks(self):
print('setting grad mask : ', self.stop_idx)
stop_gradient_mask = np.zeros(((1,self.latent_size,)))
stop_gradient_mask[...,:self.stop_idx] = 1
gradient_mask = np.ones(((1,self.latent_size,)))
gradient_mask[...,:self.stop_idx] = 0
self.set_weights([stop_gradient_mask,gradient_mask])
def build(self,input_shape):
super(StopGradientMask, self).build(input_shape)
assert len(input_shape) == 2
self.latent_size = input_shape[0].as_list()[1]
self.stop_gradient_mask = self.add_weight(
name="stop_gradient_mask",
shape=(1,self.latent_size,),
dtype=K.floatx(),
trainable=False)
self.gradient_mask = self.add_weight(
name="gradient_mask",
shape=(1,self.latent_size,),
dtype=K.floatx(),
trainable=False)
self.set_masks()
def _merge_function(self, inputs):
assert len(inputs) == 2
stopped_input,grad_input = inputs
stop_gradient_mask = tf.broadcast_to(self.stop_gradient_mask,
K.shape(stopped_input))
gradient_mask= tf.broadcast_to(self.gradient_mask,
K.shape(grad_input))
return (tf.stop_gradient(stopped_input*stop_gradient_mask)
+ grad_input*gradient_mask)
def get_config(self):
config = {'stop_idx': self.stop_idx}
base_config = super(StopGradientMask, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class Quantizer(Layer):
def __init__(self, **kwargs):
super(Quantizer, self).__init__(**kwargs)
def call(self, inputs):
differentiable_round = tf.maximum(inputs-0.499,0)
differentiable_round = differentiable_round * 10000
differentiable_round = tf.minimum(differentiable_round, 1)
return differentiable_round
class RepeatBatch(Layer):
def __init__(self, num_repeats,**kwargs):
super(RepeatBatch,self).__init__(**kwargs)
self.supports_masking = True
self.num_repeats = num_repeats
def call(self, inputs, training=None):
return K.repeat_elements(inputs, self.num_repeats, axis=0)
def get_config(self):
config = {'num_repeats': self.num_repeats,}
base_config = super(RepeatBatch, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
def build_geosamp_block(input_shape,geom_rate,
init_temp=0.5,use_grad_stop_mask=True,
sampling=True,dropout=True,
from_logits=False):
input_layer = keras.layers.Input(shape=input_shape,)
x = input_layer
if sampling:
bern_layer = BernoulliSampling(init_temp=init_temp,
from_logits=from_logits,
name='bern_sampler')
x_sampled = bern_layer(x)
x_discrete = Quantizer()(x)
x = StopGradientMask(name='stop_grad_mask')([x_discrete,x_sampled])
tanh = Lambda(lambda x : (x-0.5)*2)(x)
if dropout:
out = GeometricDropout(geom_rate,name='geom_dropout')(tanh)
else:
out = tanh
return keras.models.Model([input_layer],[out],name='geosampler')
def build_repeat_block(input_shape,num_repeats):
input_layer = keras.layers.Input(shape=input_shape,name='repeat_input')
out = RepeatBatch(num_repeats)(input_layer)
return keras.models.Model([input_layer],[out],name='repeat_block')
def build_dropout_block(input_shape):
input_layer = keras.layers.Input(shape=input_shape,name='dropout_input')
out = Dropout(0.5)(input_layer)
return keras.models.Model([input_layer],[out],name='dropout_block')
def build_latent_params(input_shape,latent_size,activation):
input_layer = keras.layers.Input(shape=input_shape,name='latent_params_input')
x = Dense(latent_size,activation=activation,name='latent_params_dense')(input_layer)
return keras.models.Model([input_layer],[x],name='latent_params')
def build_sig_to_tanh_converter(input_shape):
input_layer = keras.layers.Input(shape=input_shape)
tanh = Lambda(lambda x : (x-0.5)*2)(input_layer)
return keras.models.Model([input_layer],[tanh],name='sig_to_tanh_converter')
def build_lin_to_tanh_converter(input_shape):
input_layer = keras.layers.Input(shape=input_shape)
tanh = Activation('tanh')(input_layer)
return keras.models.Model([input_layer],[tanh],name='lin_to_tanh_converter')
custom_objs = globals()
if __name__ == '__main__':
pass
# sess = K.get_session()
# num_samples = 50
# indices = np.expand_dims(np.arange(0,100),axis=0)
# indices = np.repeat(indices,num_samples,axis=0)
# indices = tf.convert_to_tensor(indices,dtype=tf.float32)
# geom = tfp.distributions.Geometric(probs=[0.03]).sample(sample_shape=(num_samples))
# drop = tf.cast(indices <= geom,tf.uint8)
# out = sess.run(fetches=[drop,geom])
#