-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathMHA.py
52 lines (39 loc) · 1.85 KB
/
MHA.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
import tensorflow as tf
class MultiHeadSelfAttention(tf.keras.layers.Layer):
def __init__(self, embed_dim, num_heads):
super(MultiHeadSelfAttention, self).__init__()
self.num_heads = num_heads
self.embed_dim = embed_dim
assert embed_dim % self.num_heads == 0
self.depth = embed_dim // self.num_heads
self.wq = tf.keras.layers.Dense(embed_dim)
self.wk = tf.keras.layers.Dense(embed_dim)
self.wv = tf.keras.layers.Dense(embed_dim)
self.dense = tf.keras.layers.Dense(embed_dim)
def split_heads(self, x, batch_size):
"""
Split the last dimension into (num_heads, depth).
Transpose the result such that the shape is (batch_size, num_heads, seq_len, depth)
"""
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth))
return tf.transpose(x, perm=[0, 2, 1, 3])
def scaled_dot_product_attention(self, q, k, v):
matmul_qk = tf.matmul(q, k, transpose_b=True)
dk = tf.cast(tf.shape(k)[-1], tf.float32)
scaled_attention_logits = matmul_qk / tf.math.sqrt(dk)
attention_weights = tf.nn.softmax(scaled_attention_logits, axis=-1)
attention_output = tf.matmul(attention_weights, v)
return attention_output
def call(self, inputs):
batch_size = tf.shape(inputs)[0]
q = self.wq(inputs)
k = self.wk(inputs)
v = self.wv(inputs)
q = self.split_heads(q, batch_size)
k = self.split_heads(k, batch_size)
v = self.split_heads(v, batch_size)
scaled_attention = self.scaled_dot_product_attention(q, k, v)
scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3])
concat_attention = tf.reshape(scaled_attention,(batch_size, -1, self.embed_dim))
output = self.dense(concat_attention)
return output