-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiheadattention.py
executable file
·74 lines (57 loc) · 2.38 KB
/
multiheadattention.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
# referencedf from: https://gist.github.com/ekreutz/160070126d5e2261a939c4ddf6afb642
import tensorflow as tf
import keras
from keras.layers import Dense
class DotProductAttention(keras.layers.Layer):
def __init__(self, use_scale=True, **kwargs):
super(DotProductAttention, self).__init__(**kwargs)
self.use_scale = use_scale
def build(self, input_shape):
query_shape = input_shape[0]
if self.use_scale:
dim_k = tf.cast(query_shape[-1], tf.float32)
self.scale = 1 / tf.sqrt(dim_k)
else:
self.scale = None
def call(self, input):
query, key, value = input
score = tf.matmul(query, key, transpose_b=True)
if self.scale is not None:
score *= self.scale
return tf.matmul(tf.nn.softmax(score), value)
class MultiHeadAttention(keras.layers.Layer):
def __init__(self, h=8, **kwargs):
super(MultiHeadAttention, self).__init__(**kwargs)
self.h = h
def build(self, input_shape):
query_shape, key_shape, value_shape = input_shape
d_model = query_shape[-1]
# Note: units can be anything, but this is what the paper does
units = d_model // self.h
self.layersQ = []
for _ in range(self.h):
layer = Dense(units, activation=None, use_bias=False)
layer.build(query_shape)
self.layersQ.append(layer)
self.layersK = []
for _ in range(self.h):
layer = Dense(units, activation=None, use_bias=False)
layer.build(key_shape)
self.layersK.append(layer)
self.layersV = []
for _ in range(self.h):
layer = Dense(units, activation=None, use_bias=False)
layer.build(value_shape)
self.layersV.append(layer)
self.attention = DotProductAttention(True)
self.out = Dense(d_model, activation=None, use_bias=False)
self.out.build((query_shape[0], query_shape[1], self.h * units))
def call(self, input):
query, key, value = input
q = [layer(query) for layer in self.layersQ]
k = [layer(key) for layer in self.layersK]
v = [layer(value) for layer in self.layersV]
# Head is in multi-head, just like the paper
head = [self.attention([q[i], k[i], v[i]]) for i in range(self.h)]
out = self.out(tf.concat(head, -1))
return out