-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayers.py
More file actions
163 lines (128 loc) · 6.22 KB
/
layers.py
File metadata and controls
163 lines (128 loc) · 6.22 KB
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
import tensorflow as tf
from tensorflow.keras.saving import register_keras_serializable
# Proportion of positive values
@register_keras_serializable()
class HardPPV(tf.keras.layers.Layer):
def __init__(self, axis=1, **kwargs):
super(HardPPV, self).__init__(**kwargs)
self.axis = axis
def call(self, inputs):
return tf.reduce_mean(tf.nn.relu(tf.sign(inputs)), axis=self.axis)
@register_keras_serializable()
class SoftPPV(tf.keras.layers.Layer):
def __init__(self, axis=1, **kwargs):
super(SoftPPV, self).__init__(**kwargs)
self.axis = axis
def call(self, inputs):
return tf.reduce_mean(tf.sigmoid(10000.0 * (inputs - 0.001)), axis=self.axis)
# HardPPV with Straight-Through Estimator
@register_keras_serializable()
class HardPPVSTE(tf.keras.layers.Layer):
def __init__(self, axis=1, **kwargs):
super(HardPPVSTE, self).__init__(**kwargs)
self.axis = axis
def call(self, inputs):
return self._skip_gradient(inputs)
@tf.custom_gradient
def _skip_gradient(self, inputs):
output = tf.reduce_mean(tf.nn.relu(tf.sign(inputs)), axis=self.axis)
def grad(dy):
input_shape = tf.shape(inputs)
dy_broadcast = tf.broadcast_to(tf.expand_dims(dy, axis=self.axis), input_shape)
return dy_broadcast
return output, grad
PPV = SoftPPV
# Mean of positive values
@register_keras_serializable()
class HardMPV(tf.keras.layers.Layer):
def __init__(self, axis=1, **kwargs):
super(HardMPV, self).__init__(**kwargs)
self.axis = axis
def call(self, inputs):
positive_mask = tf.cast(inputs > 0.0, tf.float32)
positive_sum = tf.reduce_sum(inputs * positive_mask, axis=self.axis)
positive_count = tf.reduce_sum(positive_mask, axis=self.axis)
return tf.math.divide_no_nan(positive_sum, positive_count)
@register_keras_serializable()
class SoftMPV(tf.keras.layers.Layer):
def __init__(self, axis=1, **kwargs):
super(SoftMPV, self).__init__(**kwargs)
self.axis = axis
def call(self, inputs):
positive_mask = tf.sigmoid(10000.0 * (inputs - 0.001))
positive_sum = tf.reduce_sum(inputs * positive_mask, axis=self.axis)
positive_count = tf.reduce_sum(positive_mask, axis=self.axis)
return positive_sum / (positive_count + 1e-8)
MPV = HardMPV
# Mean of Indices of Positive Values
@register_keras_serializable()
class HardMIPV(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(HardMIPV, self).__init__(**kwargs)
self.axis = 1
def call(self, inputs):
positive_mask = tf.cast(inputs > 0.0, tf.float32)
indices = tf.range(tf.shape(inputs)[self.axis], dtype=tf.float32)
# if self.axis == 1:
indices = tf.expand_dims(indices, axis=0)
indices = tf.expand_dims(indices, axis=-1)
# No UserWarning Gradients trick
positive_indices_sum = tf.reduce_sum(indices * tf.math.divide_no_nan(inputs * positive_mask, inputs), axis=self.axis)
positive_count = tf.reduce_sum(positive_mask, axis=self.axis)
return tf.math.divide_no_nan(positive_indices_sum, positive_count)
# UserWarning: Gradients do not exist for variables [previous layers]
positive_indices_sum = tf.reduce_sum(indices * positive_mask, axis=self.axis)
positive_count = tf.reduce_sum(positive_mask, axis=self.axis)
return tf.math.divide_no_nan(positive_indices_sum, positive_count)
@register_keras_serializable()
class SoftMIPV(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(SoftMIPV, self).__init__(**kwargs)
self.axis = 1
def call(self, inputs):
positive_mask = tf.sigmoid(10000.0 * (inputs - 0.001))
indices = tf.range(tf.shape(inputs)[self.axis], dtype=tf.float32)
# if self.axis == 1:
indices = tf.expand_dims(indices, axis=0)
indices = tf.expand_dims(indices, axis=-1)
# positive_indices_sum = tf.reduce_sum(indices * (inputs * positive_mask) / (inputs + 1e-8), axis=self.axis)
positive_indices_sum = tf.reduce_sum(indices * positive_mask, axis=self.axis)
positive_count = tf.reduce_sum(positive_mask, axis=self.axis)
return positive_indices_sum / (positive_count + 1e-8)
MIPV = SoftMIPV
# Multiple Static Average Pooling
@register_keras_serializable()
class MultiAveragePooling1D(tf.keras.layers.Layer):
def __init__(self, multi=5, **kwargs):
super(MultiAveragePooling1D, self).__init__(**kwargs)
self.multi = multi
def build(self, input_shape):
stride = input_shape[1]//self.multi + (0 if input_shape[1] % self.multi == 0 else 1)
pool = stride
self.pool = tf.keras.layers.AveragePooling1D(pool_size=pool, strides=stride, padding='same')
# self.pool = tf.keras.layers.AveragePooling1D(input_shape[1]//self.multi)
if input_shape[1] / stride > self.multi - 1:
self.reshape = tf.keras.layers.Reshape((self.multi*input_shape[2],))
else:
print('WARNING: MultiAveragePooling1D multi argument is too large, new value =', input_shape[1] / stride)
self.reshape = tf.keras.layers.Reshape((-1,)) # usefull for large multi values
def call(self, inputs):
return self.reshape(self.pool(inputs))
# Multiple Static Max Pooling
@register_keras_serializable()
class MultiMaxPooling1D(tf.keras.layers.Layer):
def __init__(self, multi=5, **kwargs):
super(MultiMaxPooling1D, self).__init__(**kwargs)
self.multi = multi
def build(self, input_shape):
stride = input_shape[1]//self.multi + (0 if input_shape[1] % self.multi == 0 else 1)
pool = stride
self.pool = tf.keras.layers.MaxPool1D(pool_size=pool, strides=stride, padding='same')
# self.pool = tf.keras.layers.AveragePooling1D(input_shape[1]//self.multi)
if input_shape[1] / stride > self.multi - 1:
self.reshape = tf.keras.layers.Reshape((self.multi*input_shape[2],))
else:
print('WARNING: MultiMaxPooling1D multi argument is too large, new value =', input_shape[1] / stride)
self.reshape = tf.keras.layers.Reshape((-1,)) # usefull for large multi values
def call(self, inputs):
return self.reshape(self.pool(inputs))