forked from nuletizia/CORTICAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniform_noise.py
36 lines (28 loc) · 1.19 KB
/
uniform_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
class UniformNoise(Layer):
"""Apply additive uniform 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=-1.0, maxval=1.0, **kwargs):
super(UniformNoise, self).__init__(**kwargs)
self.supports_masking = True
self.minval = minval
self.maxval = maxval
def call(self, inputs, training=None):
def noised():
return inputs + K.random_uniform(shape=K.shape(inputs),
minval=self.minval,
maxval=self.maxval)
return K.in_train_phase(noised, inputs, training=training)
def get_config(self):
config = {'minval': self.minval, 'maxval': self.maxval}
base_config = super(UniformNoise, self).get_config()
return dict(list(base_config.items()) + list(config.items()))