forked from nuletizia/CORTICAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exponential_noise.py
36 lines (28 loc) · 1.19 KB
/
exponential_noise.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
from tensorflow.keras.layers import Layer
from tensorflow.python.keras import backend as K
from tensorflow.keras.layers import Lambda
class ExponentialNoise(Layer):
"""Apply exponential noise
Only active at training time since it is a regularization layer.
# Arguments
minval: Minimum value of the uniform distribution
maxval: Maximum value of the uniform distribution
# Input shape
Arbitrary.
# Output shape
Same as the input shape.
"""
def __init__(self, minval=0, maxval=1, **kwargs):
super(ExponentialNoise, self).__init__(**kwargs)
self.supports_masking = True
self.minval = minval
self.maxval = maxval
def call(self, inputs, training=None):
def noised():
exp_noise = -K.log(1-K.random_uniform(shape=K.shape(inputs),minval=self.minval, maxval=self.maxval))
return inputs*exp_noise
return K.in_train_phase(noised, inputs, training=training)
def get_config(self):
config = {'minval': self.minval, 'maxval': self.maxval}
base_config = super(ExponentialNoise, self).get_config()
return dict(list(base_config.items()) + list(config.items()))