forked from nuletizia/CORTICAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcauchy_noise.py
43 lines (34 loc) · 1.38 KB
/
cauchy_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
37
38
39
40
41
42
43
from tensorflow.keras.layers import Layer
from tensorflow.python.keras import backend as K
from tensorflow.keras.layers import Lambda
import tensorflow as tf
import math
def tan(x):
return tf.math.tan(x)
class CauchyNoise(Layer):
"""Apply additive Cauchy 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.5, maxval=0.5, gamma = 1, **kwargs):
super(CauchyNoise, self).__init__(**kwargs)
self.supports_masking = True
self.minval = minval
self.maxval = maxval
self.gamma = gamma
def call(self, inputs, training=None):
def noised():
sc_noise = K.random_uniform(shape=K.shape(inputs),minval=self.minval, maxval=self.maxval)
cauchy_noise = Lambda(lambda x: tan(x))(0.98*sc_noise) #to avoid errors
return inputs + self.gamma*cauchy_noise
return K.in_train_phase(noised, inputs, training=training)
def get_config(self):
config = {'minval': self.minval, 'maxval': self.maxval}
base_config = super(PolynomialNoise, self).get_config()
return dict(list(base_config.items()) + list(config.items()))