-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb_layers.py
More file actions
264 lines (221 loc) · 7.55 KB
/
bb_layers.py
File metadata and controls
264 lines (221 loc) · 7.55 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
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
class Dense3dLayer(tf.keras.layers.Layer):
"""A dense layer with 3D kernel."""
def __init__(self,
num_attention_heads,
size_per_head,
initializer,
activation,
name=None,
head_first=False,
use_bias=True):
"""Constructor for dense layer with 3D kernel.
Args:
num_attention_heads: The size of output dimension.
size_per_head: The size per attention head.
initializer: Kernel initializer.
activation: Actication function.
name: The name scope of this layer.
head_first: Whether to output head dimension before or after sequence dim.
use_bias: Whether the layer uses a bias vector.
"""
super(Dense3dLayer, self).__init__(name=name)
self.num_attention_heads = num_attention_heads
self.size_per_head = size_per_head
self.initializer = initializer
self.activation = activation
self.head_first = head_first
self.use_bias = use_bias
with tf.compat.v1.variable_scope(name):
hidden_size = self.num_attention_heads * self.size_per_head
self.w = tf.compat.v1.get_variable(
name="kernel",
shape=[hidden_size, hidden_size],
initializer=self.initializer)
if self.use_bias:
self.b = tf.compat.v1.get_variable(
name="bias",
shape=[hidden_size],
initializer=tf.zeros_initializer())
else:
self.b = None
def call(self, input_tensor):
"""Constructor for dense layer with 3D kernel.
Args:
input_tensor: float Tensor of shape [batch, seq_length, hidden_size].
Returns:
float logits Tensor.
"""
hidden_size = self.num_attention_heads * self.size_per_head
reshape_w = tf.reshape(
self.w, [hidden_size, self.num_attention_heads, self.size_per_head])
if self.head_first:
ret = tf.einsum("abc,cde->adbe", input_tensor, reshape_w)
else:
ret = tf.einsum("abc,cde->abde", input_tensor, reshape_w)
if self.use_bias:
if self.head_first:
reshape_b = tf.reshape(
self.b, [1, self.num_attention_heads, 1, self.size_per_head])
else:
reshape_b = tf.reshape(
self.b, [self.num_attention_heads, self.size_per_head])
ret += reshape_b
if self.activation is not None:
return self.activation(ret)
else:
return ret
class Dense3dProjLayer(tf.keras.layers.Layer):
"""A dense layer with 3D kernel for projection."""
def __init__(self,
num_attention_heads,
size_per_head,
initializer,
activation,
name=None,
use_bias=True):
"""Constructor for dense layer with 3D kernel for projection.
Args:
num_attention_heads: The size of output dimension.
size_per_head: The size per attention head.
initializer: Kernel initializer.
activation: Actication function.
name: The name scope of this layer.
use_bias: Whether the layer uses a bias vector.
"""
super(Dense3dProjLayer, self).__init__(name=name)
self.num_attention_heads = num_attention_heads
self.size_per_head = size_per_head
self.initializer = initializer
self.activation = activation
self.use_bias = use_bias
with tf.compat.v1.variable_scope(name):
hidden_size = self.num_attention_heads * self.size_per_head
self.w = tf.compat.v1.get_variable(
name="kernel",
shape=[hidden_size, hidden_size],
initializer=self.initializer)
if self.use_bias:
self.b = tf.compat.v1.get_variable(
name="bias",
shape=[hidden_size],
initializer=tf.zeros_initializer())
else:
self.b = None
def call(self, input_tensor):
"""Constructor for dense layer with 3D kernel for projection.
Args:
input_tensor: float Tensor of shape [batch,from_seq_length,
num_attention_heads, size_per_head].
Returns:
float logits Tensor.
"""
hidden_size = self.num_attention_heads * self.size_per_head
reshape_w = tf.reshape(
self.w, [self.num_attention_heads, self.size_per_head, hidden_size])
ret = tf.einsum("BFNH,NHD->BFD", input_tensor, reshape_w)
if self.use_bias:
ret += self.b
if self.activation is not None:
return self.activation(ret)
else:
return ret
class Dense2dLayer(tf.keras.layers.Layer):
"""A dense layer with 2D kernel."""
def __init__(self,
input_size,
output_size,
initializer,
activation,
name=None,
use_bias=True):
"""Constructor for dense layer with 2D kernel.
Args:
input_size: The size of input dimension.
output_size: The size of output dimension.
initializer: Kernel initializer.
activation: Actication function.
name: The name scope of this layer.
use_bias: Whether the layer uses a bias vector.
"""
super(Dense2dLayer, self).__init__(name=name)
self.input_size = input_size
self.output_size = output_size
self.initializer = initializer
self.activation = activation
self.use_bias = use_bias
with tf.compat.v1.variable_scope(name):
self.w = tf.compat.v1.get_variable(
name="kernel",
shape=[self.input_size, self.output_size],
initializer=self.initializer)
if self.use_bias:
self.b = tf.compat.v1.get_variable(
name="bias",
shape=[self.output_size],
initializer=tf.zeros_initializer())
else:
self.b = None
def call(self, input_tensor):
"""Forward pass for dense layer with 2D kernel.
Args:
input_tensor: Float tensor with rank 3.
Returns:
float logits Tensor.
"""
ret = tf.einsum("acb,cd->abd", input_tensor, self.w)
if self.use_bias:
ret += self.b
if self.activation is not None:
return self.activation(ret)
else:
return ret
class Dense2dLayer_mod(tf.keras.layers.Layer):
"""A dense layer with 2D kernel."""
def __init__(self,
input_size,
output_size,
initializer,
activation,
name=None,
use_bias=True):
"""Constructor for dense layer with 2D kernel.
Args:
input_size: The size of input dimension.
output_size: The size of output dimension.
initializer: Kernel initializer.
activation: Actication function.
name: The name scope of this layer.
use_bias: Whether the layer uses a bias vector.
"""
super(Dense2dLayer_mod, self).__init__(name=name)
self.input_size = input_size
self.output_size = output_size
self.initializer = initializer
self.activation = activation
self.use_bias = use_bias
with tf.compat.v1.variable_scope(name):
self.w = tf.compat.v1.get_variable(
name="kernel",
shape=[self.input_size, self.output_size],
initializer=self.initializer)
if self.use_bias:
self.b = tf.compat.v1.get_variable(
name="bias",
shape=[self.output_size],
initializer=tf.zeros_initializer())
else:
self.b = None
def call(self, input_tensor):
"""Forward pass for dense layer with 2D kernel.
Args:
input_tensor: Float tensor with rank 3.
Returns:
float logits Tensor.
"""
ret = tf.einsum("acb,cd->acd", input_tensor, self.w)
if self.use_bias:
ret += self.b
if self.activation is not None:
return self.activation(ret)
else:
return ret