-
Notifications
You must be signed in to change notification settings - Fork 1
/
multihead_attention.py
75 lines (61 loc) · 3.73 KB
/
multihead_attention.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
from tensorflow import math, matmul, reshape, shape, transpose, cast, float32
from tensorflow.keras.layers import Dense, Layer
from keras.backend import softmax
# Implementing the Scaled-Dot Product Attention
class DotProductAttention(Layer):
def __init__(self, **kwargs):
super(DotProductAttention, self).__init__(**kwargs)
def call(self, queries, keys, values, key_dim, mask=None):
# Scoring the queries against the keys after transposing the latter, and scaling
scores = matmul(queries, keys, transpose_b=True) / math.sqrt(cast(key_dim, float32))
# Apply mask to the attention scores
if mask is not None:
scores += -1e9 * mask
# Computing the weights by a softmax operation
weights = softmax(scores)
# Computing the attention by a weighted sum of the value vectors
return matmul(weights, values)
# Implementing the Multi-Head Attention
class MultiHeadAttention(Layer):
def __init__(self, num_heads, key_dim, value_dim, embed_dim, **kwargs):
super(MultiHeadAttention, self).__init__(**kwargs)
self.attention = DotProductAttention() # Scaled dot product attention
self.num_heads = num_heads # Number of attention heads to use
self.key_dim = key_dim # Dimensionality of the linearly projected queries and keys
self.value_dim = value_dim # Dimensionality of the linearly projected values
self.embed_dim = embed_dim # Dimensionality of the model
self.W_q = Dense(key_dim) # Learned projection matrix for the queries
self.W_k = Dense(key_dim) # Learned projection matrix for the keys
self.W_v = Dense(value_dim) # Learned projection matrix for the values
self.W_o = Dense(
embed_dim
) # Learned projection matrix for the multi-head output
def reshape_tensor(self, x, num_heads, flag):
if flag:
# Tensor shape after reshaping and transposing: (batch_size, heads, seq_length, -1)
x = reshape(x, shape=(shape(x)[0], shape(x)[1], num_heads, -1))
x = transpose(x, perm=(0, 2, 1, 3))
else:
# Reverting the reshaping and transposing operations: (batch_size, seq_length, key_dim)
x = transpose(x, perm=(0, 2, 1, 3))
x = reshape(x, shape=(shape(x)[0], shape(x)[1], self.key_dim))
return x
def call(self, queries, keys, values, mask=None):
# Rearrange the queries to be able to compute all heads in parallel
q_reshaped = self.reshape_tensor(self.W_q(queries), self.num_heads, True)
# Resulting tensor shape: (batch_size, heads, input_seq_length, -1)
# Rearrange the keys to be able to compute all heads in parallel
k_reshaped = self.reshape_tensor(self.W_k(keys), self.num_heads, True)
# Resulting tensor shape: (batch_size, heads, input_seq_length, -1)
# Rearrange the values to be able to compute all heads in parallel
v_reshaped = self.reshape_tensor(self.W_v(values), self.num_heads, True)
# Resulting tensor shape: (batch_size, heads, input_seq_length, -1)
# Compute the multi-head attention output using the reshaped queries, keys and values
o_reshaped = self.attention(q_reshaped, k_reshaped, v_reshaped, self.key_dim, mask)
# Resulting tensor shape: (batch_size, heads, input_seq_length, -1)
# Rearrange back the output into concatenated form
output = self.reshape_tensor(o_reshaped, self.num_heads, False)
# Resulting tensor shape: (batch_size, input_seq_length, value_dim)
# Apply one final linear projection to the output to generate the multi-head attention
# Resulting tensor shape: (batch_size, input_seq_length, embed_dim)
return self.W_o(output)