forked from gmlyytt-YANG/gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
58 lines (41 loc) · 2.04 KB
/
decoder.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
from util import *
class DecoderLayer(layers.Layer):
def __init__(self, d_model, num_heads, dff, drop_rate=0.1):
super(DecoderLayer, self).__init__()
self.mha1 = MutilHeadAttention(d_model, num_heads)
self.ffn = point_wise_feed_forward_network(d_model, dff)
self.layernorm1 = LayerNormalization(epsilon=1e-6)
self.layernorm2 = LayerNormalization(epsilon=1e-6)
self.dropout1 = layers.Dropout(drop_rate)
self.dropout2 = layers.Dropout(drop_rate)
def call(self, inputs, training, look_ahead_mask):
# masked muti-head attention
att1, att_weight1 = self.mha1(inputs, inputs, inputs, look_ahead_mask)
att1 = self.dropout1(att1, training=training)
out1 = self.layernorm1(inputs + att1)
ffn_out = self.ffn(out1)
ffn_out = self.dropout2(ffn_out, training=training)
out2 = self.layernorm2(out1 + ffn_out)
return out2, att_weight1
class Decoder(layers.Layer):
def __init__(self, n_layers, d_model, n_heads, ddf,
target_vocab_size, max_seq_len, drop_rate=0.1):
super(Decoder, self).__init__()
self.d_model = d_model
self.n_layers = n_layers
self.embedding = layers.Embedding(target_vocab_size, d_model)
self.pos_embedding = positional_encoding(max_seq_len, d_model)
self.decoder_layers= [DecoderLayer(d_model, n_heads, ddf, drop_rate)
for _ in range(n_layers)]
self.dropout = layers.Dropout(drop_rate)
def call(self, inputs, training, look_ahead_mark):
seq_len = tf.shape(inputs)[1]
attention_weights = {}
h = self.embedding(inputs)
h *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
h += self.pos_embedding[:,:seq_len,:]
h = self.dropout(h, training=training)
for i in range(self.n_layers):
h, att_w1 = self.decoder_layers[i](h, training, look_ahead_mark)
attention_weights['decoder_layer{}_att_w1'.format(i+1)] = att_w1
return h, attention_weights