-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_layers.py
244 lines (161 loc) · 8.95 KB
/
custom_layers.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
import tensorflow as tf
import numpy as np
class FourierLayer(tf.keras.layers.Layer):
def __init__(self, units, scale):
super(FourierLayer, self).__init__()
self.units = units
self.scale = scale
def build(self, input_shape):
self.in_features = int(input_shape[-1])
self.B = tf.random.normal((self.in_features, self.units))
super(FourierLayer, self).build(input_shape)
def call(self, inputs):
out = tf.concat([tf.cos(self.scale * tf.matmul(inputs, self.B)), tf.sin(self.scale * tf.matmul(inputs, self.B))], axis=-1)
return out
class SineLayer(tf.keras.layers.Layer):
def __init__(self, in_features, units, bias=True, is_first=False, omega_0=30.):
super(SineLayer, self).__init__()
self.in_features = in_features
self.units = units
self.is_first = is_first
self.omega_0 = omega_0
self.dense = tf.keras.layers.Dense(self.units,
use_bias=bias,
kernel_initializer=self.init_weights(),
input_shape=(self.in_features,))
def init_weights(self):
if self.is_first:
return tf.keras.initializers.RandomUniform(minval=-1 / self.in_features,
maxval= 1 / self.in_features)
else:
return tf.keras.initializers.RandomUniform(minval=-np.sqrt(6. / self.in_features) / self.omega_0,
maxval= np.sqrt(6. / self.in_features) / self.omega_0)
def call(self, input_tensor):
befor_activation = self.dense(input_tensor)
after_activation = tf.sin(self.omega_0 * befor_activation)
return after_activation
class PureSparseLayer(tf.keras.layers.Layer):
def __init__(self, units, alpha=1, use_bias=True, activation=None, kernel_initializer=None, full="output"):
super(PureSparseLayer, self).__init__()
self.units = units
self.alpha = alpha
self.activation = activation
self.use_bias = use_bias
self.kernel_initializer = kernel_initializer
self.full = full
def build(self, input_shape):
self.in_features = int(input_shape[-1])
if self.full not in ["input", "output"]:
raise NameError('full argument must be "input" or "output"')
if self.alpha > self.in_features:
self.alpha = self.in_features
print(f"alpha set to : {self.alpha}")
n_sparse_parameters = self.alpha * self.units
if self.full == "input":
Total_Indexs = []
for_each_row = n_sparse_parameters // self.in_features
remain = n_sparse_parameters % self.in_features
remain_index = np.random.choice(self.in_features, remain, replace=False)
row_indexs = np.random.choice(self.in_features, self.in_features, replace=False)
for counter, row_index in enumerate(row_indexs):
if row_index in remain_index:
column_indexs = np.random.choice(self.units, for_each_row + 1, replace=False)
else:
column_indexs = np.random.choice(self.units, for_each_row, replace=False)
Total_Indexs.append(np.stack([row_index * np.ones_like(column_indexs), column_indexs], axis=1))
self.Total_Indexs = np.concatenate(Total_Indexs, axis=0)
elif self.full == "output":
Total_Indexs = []
for_each_column = n_sparse_parameters // self.units
remain = n_sparse_parameters % self.units
remain_index = np.random.choice(self.units, remain, replace=False)
column_indexs = np.random.choice(self.units, self.units, replace=False)
for counter, column_index in enumerate(column_indexs):
if column_index in remain_index:
row_indexs = np.random.choice(self.in_features, for_each_column + 1, replace=False)
else:
row_indexs = np.random.choice(self.in_features, for_each_column, replace=False)
Total_Indexs.append(np.stack([row_indexs, column_index * np.ones_like(row_indexs)], axis=1))
self.Total_Indexs = np.concatenate(Total_Indexs, axis=0)
else:
raise NameError('full argument must be "input" or "output"')
if self.kernel_initializer is None:
self.kernel = tf.Variable(tf.initializers.glorot_uniform()((n_sparse_parameters,)), trainable=True)
else:
self.kernel = tf.Variable(self.kernel_initializer((n_sparse_parameters,)), trainable=True)
if self.use_bias:
self.bias = tf.Variable(tf.zeros((self.units,)), trainable=True)
super(PureSparseLayer, self).build(input_shape)
def call(self, inputs):
new_kernel = tf.SparseTensor(indices=self.Total_Indexs,
values=self.kernel,
dense_shape=(self.in_features, self.units))
out = tf.sparse.sparse_dense_matmul(inputs, new_kernel)
if self.use_bias:
out = out + self.bias
if self.activation is not None:
return self.activation(out)
return out
class QuasiSparseLayer(tf.keras.layers.Layer):
def __init__(self, units, alpha=1, use_bias=True, activation=None, kernel_initializer=None, full="output"):
super(QuasiSparseLayer, self).__init__()
self.units = units
self.alpha = alpha
self.activation = activation
self.use_bias = use_bias
self.kernel_initializer = kernel_initializer
self.full = full
def build(self, input_shape):
self.in_features = int(input_shape[-1])
if self.full not in ["input", "output"]:
raise NameError('full argument must be "input" or "output"')
if self.alpha > self.in_features:
self.alpha = self.in_features
print(f"alpha set to : {self.alpha}")
n_sparse_parameters = self.alpha * self.units
if self.full == "input":
Total_Indexs = []
for_each_row = n_sparse_parameters // self.in_features
remain = n_sparse_parameters % self.in_features
remain_index = np.random.choice(self.in_features, remain, replace=False)
row_indexs = np.random.choice(self.in_features, self.in_features, replace=False)
for counter, row_index in enumerate(row_indexs):
if row_index in remain_index:
column_indexs = np.random.choice(self.units, for_each_row + 1, replace=False)
else:
column_indexs = np.random.choice(self.units, for_each_row, replace=False)
Total_Indexs.append(np.stack([row_index * np.ones_like(column_indexs), column_indexs], axis=1))
self.Total_Indexs = np.concatenate(Total_Indexs, axis=0)
elif self.full == "output":
Total_Indexs = []
for_each_column = n_sparse_parameters // self.units
remain = n_sparse_parameters % self.units
remain_index = np.random.choice(self.units, remain, replace=False)
column_indexs = np.random.choice(self.units, self.units, replace=False)
for counter, column_index in enumerate(column_indexs):
if column_index in remain_index:
row_indexs = np.random.choice(self.in_features, for_each_column + 1, replace=False)
else:
row_indexs = np.random.choice(self.in_features, for_each_column, replace=False)
Total_Indexs.append(np.stack([row_indexs, column_index * np.ones_like(row_indexs)], axis=1))
self.Total_Indexs = np.concatenate(Total_Indexs, axis=0)
else:
raise NameError('full argument must be "input" or "output"')
self.Mask = np.zeros((self.in_features, self.units))
self.Mask[self.Total_Indexs[:,0], self.Total_Indexs[:,1]] = 1
self.Mask = tf.constant(self.Mask, dtype=tf.float32)
if self.kernel_initializer is None:
self.kernel = tf.Variable(tf.initializers.glorot_uniform()((self.in_features, self.units)), trainable=True)
else:
self.kernel = tf.Variable(self.kernel_initializer((self.in_features, self.units)), trainable=True)
if self.use_bias:
self.bias = tf.Variable(tf.zeros((self.units,)), trainable=True)
super(QuasiSparseLayer, self).build(input_shape)
def call(self, inputs):
new_kernel = self.Mask * self.kernel
out = tf.matmul(inputs, new_kernel)
if self.use_bias:
out = out + self.bias
if self.activation is not None:
return self.activation(out)
return out