-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
305 lines (225 loc) · 9.93 KB
/
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# common layers for deep learning
# Almost all layers listed are already implemented efficiently in Keras.
from __future__ import print_function
from __future__ import unicode_literals
import tensorflow as tf
tf.keras.Model.save_weights()
tf.keras.layers.Attention
class Dense(tf.keras.layers.Layer):
def __init__(self, out_features, name=None):
super().__init__(name=name)
self.out_features = out_features
def build(self, input_shape):
self.w = tf.Variable(
tf.random.normal([input_shape[-1], self.out_features]), name='w')
self.b = tf.Variable(tf.zeros([self.out_features]), name='b')
def call(self, x):
y = tf.matmul(x, self.w) + self.b
return tf.nn.relu(y)
# TODO
class Convo(tf.keras.layers.Layer):
def __init__(self):
pass
def call(self, inputs, **kwargs):
raise NotImplementedError("implement this first")
class RNNCell(tf.keras.layers.Layer):
# to be used in RNNLayer
def __init__(self, hidden_dimension, num_classes=2, name=None):
super(RNNCell, self).__init__(name=name)
if hidden_dimension is None:
raise ValueError("hidden_dimension cannot be None")
self.hidden_dimension = hidden_dimension
self.num_classes = num_classes
def build(self, input_shape):
self.w_one = tf.Variable(
tf.random.normal([input_shape[-1], self.hidden_dimension]), name='w_one')
self.w_two = tf.Variable(
tf.random.normal([self.hidden_dimension, self.hidden_dimension]), name='w_two')
self.b = tf.Variable(tf.zeros([self.hidden_dimension]), name='b')
self.v = tf.Variable(
tf.random.uniform([self.hidden_dimension, self.num_classes]), name='v')
self.c = tf.Variable(tf.zeros([self.num_classes]), name='c')
def call(self, inputs, states):
# input shape --> (batch size, embed dimension)
at = tf.linalg.matmul(inputs, self.w_one) + tf.linalg.matmul(states, self.w_two) + self.b
state = tf.nn.tanh(at)
ot = self.c + tf.linalg.matmul(state, self.v)
yt = tf.nn.softmax(ot)
return yt, state
class RNNLayer(tf.keras.layers.Layer):
def __init__(self, hidden_dimension, num_classes=2, name=None):
super(RNNLayer, self).__init__(name=name)
self.hidden_dimension = hidden_dimension
self.num_classes = num_classes
self.rnn_cell = RNNCell(
hidden_dimension=self.hidden_dimension,
num_classes=self.num_classes
)
def build(self, input_shape):
pass
def call(self, inputs):
time_first_input = tf.transpose(inputs, perm=[1, 0, 2]) # steps * batch * embedding
current_state = tf.zeros(shape=(time_first_input.shape[1], self.hidden_dimension))
states = []
outputs = []
# TODO void doing lists
for i in range(time_first_input.shape[0]):
yt, current_state = self.rnn_cell(inputs=time_first_input[i], states=current_state)
states.append(current_state)
outputs.append(yt)
outputs = tf.stack(outputs) # stack array
states = tf.stack(states)
outputs = tf.transpose(outputs, perm=[1, 0, 2])
states = tf.transpose(states, perm=[1, 0, 2])
return outputs, states
# Note: keras does offer the implementation of additive and dot product attention
class AttentionLayer(tf.keras.layers.Layer):
"""
Args:
use_scale: bool. if true then scale the scale the attention weights. not used currently
attention_type: The attention type. Options {"additive", "dot_product"}
default is "additive"
"""
def __init__(self, use_scale=False, attention_type="additive"):
super(AttentionLayer, self).__init__()
self.use_scale = use_scale
self.attention_type = attention_type
@staticmethod
def additive_attention_score(query, key):
addition = tf.expand_dims(query, axis=-2) + tf.expand_dims(key, axis=-3) # (batch size, seq, seq, dim)
e_values = tf.reduce_sum(tf.math.tanh(addition), axis=-1) # (batch size, seq, seq)
weights = tf.math.softmax(e_values, axis=-1) # (batch size, seq, seq)
return weights
@staticmethod
def dot_product_attention_score(query, key):
e_values = tf.matmul(query, key, transpose_b=True) # (batch size, seq, dim)
weights = tf.math.softmax(e_values, axis=-1) # (batch size, seq, seq)
return weights
@property
def attention_function(self):
func_map = {"additive": self.additive_attention_score,
"dot_product": self.dot_product_attention_score}
return func_map[self.attention_type]
def compute_mask(self, inputs, mask=None):
pass
def call(self, inputs, **kwargs):
if len(inputs) < 2:
raise ValueError("Expected 2 or 3 inputs "
"as [query, value] or [query, value, key]")
q = inputs[0]
v = inputs[1]
k = inputs[2] if len(inputs) > 2 else v
weights = self.attention_function(query=q, key=k)
out = tf.matmul(weights, v) # (batch size, seq, dim)
return out
class AdditiveSelfAttentionLayer(tf.keras.layers.Layer):
"""Additive self attention or Bahdanau Self Attention
input -- > (batch size, query, dim)
output --> (batch size, query, dim)
This is the same as tf.keras.layers.AdditiveAttention with use_scale=False. You can verify below
Note this is self attention and hence query and key are both
the same(you pass query and key separately in keras attention layers)
>> inputs = tf.random.uniform((10,3,4))
>> at = tf.keras.layers.AdditiveAttention(use_scale=False)
>> att = AdditiveSelfAttentionLayer()
>> tf.equal(at([inputs, inputs]), att(inputs))
<tf.Tensor: shape=(10, 3, 4), dtype=bool, numpy=
array([[[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]],
[[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]],
[[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]],
.
.
.
.
]]
"""
def __int__(self):
super(AdditiveSelfAttentionLayer, self).__int__()
def compute_mask(self, inputs, mask=None):
pass
def call(self, inputs, **kwargs):
# inputs - (batch size, seq, dim)
addition = tf.expand_dims(inputs, axis=-2) + tf.expand_dims(inputs, axis=-3) # (batch size, seq, seq, dim)
e_values = tf.reduce_sum(tf.math.tanh(addition), axis=-1) # (batch size, seq, seq)
weights = tf.math.softmax(e_values, axis=-1) # (batch size, seq, seq)
out = tf.matmul(weights, inputs) # (batch size, seq, dim)
return out
class DotProductSelfAttentionLayer(tf.keras.layers.Layer):
"""Dot product self attention
input -- > (batch size, query, dim)
output --> (batch size, query, dim)
This is same as tf.keras.layers.Attention with use_scale=False.
Note this is self attention and hence query and key are both
the same(you pass query and key separately in keras attention layers)
"""
def __int__(self):
super(DotProductSelfAttentionLayer, self).__int__()
def call(self, inputs, **kwargs):
# inputs - (batch size, seq, dim)
e_values = tf.matmul(inputs, inputs, transpose_b=True) # (batch size, seq, dim)
weights = tf.math.softmax(e_values, axis=-1) # (batch size, seq, seq)
out = tf.matmul(weights, inputs) # (batch size, seq, dim)
return out
class HANLayer(tf.keras.layers.Layer):
def __init__(self):
pass
def call(self, inputs, **kwargs):
raise NotImplementedError("implement this first")
class EmbeddingLayer(tf.keras.layers.Layer):
def __init__(self,
input_dim,
output_dim,
trainable=True,
weights=None,
mask_zero=False
):
super(EmbeddingLayer, self).__init__()
if weights is None:
if trainable is False:
raise ValueError("If trainable is False then pretrained "
"weights are expected")
self.input_dim = input_dim
self.output_dim = output_dim
self.mask_zero = mask_zero
if input_dim is None:
raise ValueError("Expected input_dim as an integer but got {}".format(input_dim))
if output_dim is None:
raise ValueError("Expected output_dim as an integer but got {}".format(output_dim))
if trainable is True:
if weights is None:
self.embedding_weights = tf.Variable(
tf.random.uniform([self.input_dim, self.output_dim], dtype=tf.float32), name='embedding')
else:
self._verify_weight_dimension(weights)
self.embedding_weights = tf.constant(weights, dtype=tf.float32)
def compute_mask(self, inputs, mask=None):
pass
def _verify_weight_dimension(self, weights):
pass
def call(self, inputs):
# inputs - convert last axis numbers to embeddings
return tf.nn.embedding_lookup(params=self.embedding_weights, ids=inputs)
class MultiHeadAttention(tf.keras.layers.Layer):
def __init__(self):
pass
def call(self, inputs, **kwargs):
raise NotImplementedError("implement this first")
class Transformer(tf.keras.layers.Layer):
def __init__(self):
pass
def call(self, inputs, **kwargs):
raise NotImplementedError("implement this first")
class LayerNormalization(tf.keras.layers.Layer):
pass
class BatchNormalization(tf.keras.layers.Layer):
pass
class ResNet(tf.keras.layers.Layer):
pass
class VGG(tf.keras.layers.Layer):
pass