MCPcopy Create free account
hub / github.com/brightmart/text_classification / decoder_single_layer

Method decoder_single_layer

a07_Transformer/a2_decoder.py:54–89  ·  view source on GitHub ↗

singel layer for decoder. each layers has three sub-layers: the first is multi-head self-attention(mask) mechanism; the second is multi-head attention over output of encoder the third is position-wise fully connected feed-forward network. for each sublay

(self,Q,K_s,layer_index)

Source from the content-addressed store, hash-verified

52 return Q,K_s
53
54 def decoder_single_layer(self,Q,K_s,layer_index):
55 """
56 singel layer for decoder. each layers has three sub-layers:
57 the first is multi-head self-attention(mask) mechanism;
58 the second is multi-head attention over output of encoder
59 the third is position-wise fully connected feed-forward network.
60 for each sublayer. use LayerNorm(x+Sublayer(x)). input and output of last dimension: d_model=512s.
61 :param Q: shape should be: [batch_size,sequence_length,d_model]
62 :param K_s: shape should be: [batch_size,sequence_length,d_model]
63 :param layer_index: index of layer
64 :param mask: mask is a list. length is sequence_length. each element is a scaler value. e.g. [1,1,1,-1000000,-1000000,-1000000,....-1000000]
65 :return:output: shape should be:[batch_size*sequence_length,d_model]
66 """
67 print("#decoder#decoder_single_layer",layer_index,"====================================>")
68 # 1.1 the first is masked multi-head self-attention mechanism
69 multi_head_attention_output=self.sub_layer_multi_head_attention(layer_index,Q,K_s,self.type,is_training=self.is_training,mask=self.mask,dropout_keep_prob=self.dropout_keep_prob) #[batch_size*sequence_length,d_model]
70 #1.2 use LayerNorm(x+Sublayer(x)). all dimension=512.
71 multi_head_attention_output=self.sub_layer_layer_norm_residual_connection(K_s,multi_head_attention_output,layer_index,'decoder_multi_head_attention',dropout_keep_prob=self.dropout_keep_prob)
72
73
74 # 2.1 the second is multi-head attention over output of encoder
75 # IMPORTANT!!! check two parameters below: Q: should from decoder; K_s: should be the output of encoder
76 attention_enc_dec=AttentionEncoderDecoder(self.d_model,self.d_k,self.d_v,self.sequence_length,self.h,self.batch_size,
77 multi_head_attention_output,self.K_v_encoder,layer_index,self.decoder_sent_length,dropout_keep_prob=self.dropout_keep_prob)
78 attention_enc_dec_output=attention_enc_dec.attention_encoder_decoder_fn()
79 print("decoder.2.1.attention_enc_dec_output:",attention_enc_dec_output)
80 # 2.2 use LayerNorm(x+Sublayer(x)). all dimension=512.
81 attention_enc_dec_output=self.sub_layer_layer_norm_residual_connection(multi_head_attention_output,attention_enc_dec_output, layer_index,
82 'decoder_attention_encoder_decoder',dropout_keep_prob=self.dropout_keep_prob)
83
84 # 3.1 the second is position-wise fully connected feed-forward network.
85 postion_wise_feed_forward_output=self.sub_layer_postion_wise_feed_forward(attention_enc_dec_output,layer_index,self.type)
86 print("decoder.3.1.postion_wise_feed_forward_output:",postion_wise_feed_forward_output)
87 #3.2 use LayerNorm(x+Sublayer(x)). all dimension=512.
88 postion_wise_feed_forward_output=self.sub_layer_layer_norm_residual_connection(attention_enc_dec_output,postion_wise_feed_forward_output, layer_index, 'decoder_position_ff',dropout_keep_prob=self.dropout_keep_prob)
89 return postion_wise_feed_forward_output,postion_wise_feed_forward_output
90
91#####################BELOW IS TEST METHOD FOR DECODER: FROM SMALL FUNCTION TO WHOLE FUNCTION OF DECODER:################################################
92#test decoder for single layer

Callers 2

decoder_fnMethod · 0.95

Tested by 1